summaryrefslogtreecommitdiffstats
path: root/java/tests/LoginTest.java
blob: 8e14bb11b3f277e6b2354e7937a43bd7fc232d7b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
/*
 * JLasso -- Java bindings for Lasso library
 *
 * Copyright (C) 2004 Entr'ouvert
 * http://lasso.entrouvert.org
 *
 * Authors: Benjamin Poussin <poussin@codelutin.com>
 *          Emmanuel Raviart <eraviart@entrouvert.com>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

// To run it:
// $ export LD_LIBRARY_PATH=../target/
// $ javac -classpath /usr/share/java/junit.jar:../target/lasso.jar:. LoginTest.java
// $ java -classpath /usr/share/java/junit.jar:../target/lasso.jar:. LoginTest
// or for gcj:
// $ export LD_LIBRARY_PATH=../target/
// $ gcj -C -classpath /usr/share/java/junit.jar:../target/lasso.jar:. LoginTest.java
// $ gij -classpath /usr/share/java/junit.jar:../target/lasso.jar:. LoginTest

import junit.framework.TestCase;
import junit.framework.Test;
import junit.framework.TestSuite;

import com.entrouvert.lasso.*;


public class LoginTest extends TestCase {
    public String generateIdentityProviderContextDump() {
	LassoServer serverContext = new LassoServer(
            "../../examples/data/idp-metadata.xml",
            "../../examples/data/idp-public-key.pem",
            "../../examples/data/idp-private-key.pem",
            "../../examples/data/idp-crt.pem",
           Lasso.signatureMethodRsaSha1);
        serverContext.addProvider(
            "../../examples/data/sp-metadata.xml",
            "../../examples/data/sp-public-key.pem",
            "../../examples/data/ca-crt.pem");
	String serverContextDump = serverContext.dump();
        return serverContextDump;
    }

    public String generateServiceProviderContextDump() {
        LassoServer serverContext = new LassoServer(
            "../../examples/data/sp-metadata.xml",
            "../../examples/data/sp-public-key.pem",
            "../../examples/data/sp-private-key.pem",
            "../../examples/data/sp-crt.pem",
            Lasso.signatureMethodRsaSha1);
        serverContext.addProvider(
            "../../examples/data/idp-metadata.xml",
            "../../examples/data/idp-public-key.pem",
            "../../examples/data/ca-crt.pem");
        String serverContextDump = serverContext.dump();
        return serverContextDump;
    }

    public void test01_generateServersContextDumps() {
        String identityProviderContextDump = generateIdentityProviderContextDump();
        assertNotNull(identityProviderContextDump);
        String serviceProviderContextDump = generateServiceProviderContextDump();
        assertNotNull(serviceProviderContextDump);
    }

    public void test02_serviceProviderLogin() {
	boolean userAuthenticated;
	int method, requestType;
	LassoAuthnRequest authnRequest;
	LassoIdentity idpIdentityContext, spIdentityContext;
	LassoLogin idpLoginContext, spLoginContext;
	LassoLogout idpLogoutContext, spLogoutContext;
	LassoResponse response;
	LassoServer idpContext, spContext;
	LassoSession idpSessionContext, spSessionContext;
	String artifact, authenticationMethod, authnRequestQuery, authnRequestUrl, idpContextDump,
	    idpIdentityContextDump, idpSessionContextDump, nameIdentifier, relayState,
	    responseQuery, responseUrl, soapEndpoint, soapResponseMsg, soapRequestMsg,
	    spContextDump, spIdentityContextDump, spSessionContextDump;

	// Service provider login using HTTP redirect.
        spContextDump = generateServiceProviderContextDump();
	assertNotNull(spContextDump);
        spContext = new LassoServer(spContextDump);
        spLoginContext = new LassoLogin(spContext);
        assertEquals(0, spLoginContext.initAuthnRequest(
            "https://identity-provider:1998/liberty-alliance/metadata"));
        assertEquals(Lasso.messageTypeAuthnRequest, spLoginContext.getRequestType());
	authnRequest = (LassoAuthnRequest) spLoginContext.getRequest();
        authnRequest.setPassive(false);
        authnRequest.setNameIdPolicy(Lasso.libNameIdPolicyTypeFederated);
        authnRequest.setConsent(Lasso.libConsentObtained);
	relayState = "fake";
        authnRequest.setRelayState(relayState);
        assertEquals(0, spLoginContext.buildAuthnRequestMsg());
        authnRequestUrl = spLoginContext.getMsgUrl();
        authnRequestQuery = authnRequestUrl.substring(authnRequestUrl.indexOf("?") + 1);
        method = Lasso.httpMethodRedirect;

	// Identity provider singleSignOn, for a user having no federation.
        idpContextDump = generateIdentityProviderContextDump();
        assertNotNull(idpContextDump);
        idpContext = new LassoServer(idpContextDump);
        idpLoginContext = new LassoLogin(idpContext);
        assertEquals(0, idpLoginContext.initFromAuthnRequestMsg(authnRequestQuery, method));
        assertTrue(idpLoginContext.mustAuthenticate());

        userAuthenticated = true;
        authenticationMethod = Lasso.samlAuthenticationMethodPassword;
        assertEquals(Lasso.loginProtocolProfileBrwsArt, idpLoginContext.getProtocolProfile());
        assertEquals(0, idpLoginContext.buildArtifactMsg(
            userAuthenticated, authenticationMethod, "FIXME: reauthenticateOnOrAfter",
            Lasso.httpMethodRedirect));
	idpIdentityContextDump = idpLoginContext.getIdentity().dump();
        assertNotNull(idpIdentityContextDump);
	idpSessionContextDump = idpLoginContext.getSession().dump();
        assertNotNull(idpSessionContextDump);
        responseUrl = idpLoginContext.getMsgUrl();
        responseQuery = responseUrl.substring(responseUrl.indexOf("?") + 1);
        soapResponseMsg = idpLoginContext.getResponseDump();
        artifact = idpLoginContext.getAssertionArtifact();
	nameIdentifier = idpLoginContext.getNameIdentifier();
        method = Lasso.httpMethodRedirect;

        // Service provider assertion consumer.
        spContextDump = generateServiceProviderContextDump();
	assertNotNull(spContextDump);
        spContext = new LassoServer(spContextDump);
        spLoginContext = new LassoLogin(spContext);
        assertEquals(0, spLoginContext.initRequest(responseQuery, method));
        assertEquals(0, spLoginContext.buildRequestMsg());
        soapEndpoint = spLoginContext.getMsgUrl();
	soapRequestMsg = spLoginContext.getMsgBody();

        // Identity provider SOAP endpoint.
        requestType = Lasso.getRequestTypeFromSoapMsg(soapRequestMsg);
        assertEquals(Lasso.requestTypeLogin, requestType);

	// Service provider assertion consumer (step 2: process SOAP response).
        assertEquals(0, spLoginContext.processResponseMsg(soapResponseMsg));
        assertEquals(nameIdentifier, spLoginContext.getNameIdentifier());
	// The user doesn't have any federation yet.
        assertEquals(0, spLoginContext.acceptSso());
        spIdentityContext = spLoginContext.getIdentity();
        assertNotNull(spIdentityContext);
        spIdentityContextDump = spIdentityContext.dump();
        assertNotNull(spIdentityContextDump);
        spSessionContext = spLoginContext.getSession();
        assertNotNull(spSessionContext);
        spSessionContextDump = spSessionContext.dump();
        assertNotNull(spSessionContextDump);
	authenticationMethod = spSessionContext.getAuthenticationMethod(null);
        assertEquals(Lasso.samlAuthenticationMethodPassword, authenticationMethod);

        // Service provider logout.
        spContextDump = generateServiceProviderContextDump();
        assertNotNull(spContextDump);
        spContext = new LassoServer(spContextDump);
        assertNotNull(spContext);
        spLogoutContext = new LassoLogout(spContext, Lasso.providerTypeSp);
        assertNotNull(spIdentityContextDump);
	spLogoutContext.setIdentityFromDump(spIdentityContextDump);
        assertNotNull(spSessionContextDump);
	spLogoutContext.setSessionFromDump(spSessionContextDump);
        assertEquals(0, spLogoutContext.initRequest(null));
        assertEquals(0, spLogoutContext.buildRequestMsg());
        soapEndpoint = spLogoutContext.getMsgUrl();
        soapRequestMsg = spLogoutContext.getMsgBody();

	// Identity provider SOAP endpoint.
	requestType = Lasso.getRequestTypeFromSoapMsg(soapRequestMsg);
        assertEquals(Lasso.requestTypeLogout, requestType);
        idpContextDump = generateIdentityProviderContextDump();
        assertNotNull(idpContextDump);
        idpContext = new LassoServer(idpContextDump);
        assertNotNull(idpContext);
        idpLogoutContext = new LassoLogout(idpContext, Lasso.providerTypeIdp);
	assertEquals(0, idpLogoutContext.loadRequestMsg(soapRequestMsg, Lasso.httpMethodSoap));
        assertEquals(nameIdentifier, idpLogoutContext.getNameIdentifier());
        assertNotNull(idpIdentityContextDump);
        assertEquals(0, idpLogoutContext.setIdentityFromDump(idpIdentityContextDump));
        assertNotNull(idpSessionContextDump);
        assertEquals(0, idpLogoutContext.setSessionFromDump(idpSessionContextDump));
	assertEquals(0, idpLogoutContext.processRequest());
        idpIdentityContext = idpLogoutContext.getIdentity();
        assertNotNull(idpIdentityContext);
        idpIdentityContextDump = idpIdentityContext.dump();
        assertNotNull(idpIdentityContextDump);
	// There is no other service provider from which the user must be logged out.
        assertEquals(null, idpLogoutContext.getNextProviderId());
        assertEquals(0, idpLogoutContext.buildResponseMsg());
        soapResponseMsg = idpLogoutContext.getMsgBody();

	// Service provider logout (step 2: process SOAP response).
        assertEquals(0, spLogoutContext.processResponseMsg(soapResponseMsg, Lasso.httpMethodSoap));
        spIdentityContextDump = spLogoutContext.getIdentity().dump();
        assertNotNull(spIdentityContextDump);
    }

    public static Test suite() { 
	return new TestSuite(LoginTest.class); 
    }

    public static void main(String args[]) { 
	Lasso.init();
	junit.textui.TestRunner.run(suite());
	Lasso.shutdown();
    }
}