/
TestKeycloakAuthenticationPlugin.java
191 lines (150 loc) · 8.38 KB
/
TestKeycloakAuthenticationPlugin.java
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
/*
* (C) Copyright 2015 Nuxeo SA (http://nuxeo.com/) and contributors.
*
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the GNU Lesser General Public License
* (LGPL) version 2.1 which accompanies this distribution, and is available at
* http://www.gnu.org/licenses/lgpl-2.1.html
*
* This library 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
* Lesser General Public License for more details.
*
* Contributors:
* François Maturel
*/
package org.nuxeo.ecm.platform.ui.web.keycloak;
import static junit.framework.TestCase.assertNull;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.mockito.Matchers.any;
import static org.nuxeo.ecm.platform.ui.web.keycloak.KeycloakRequestAuthenticator.KEYCLOAK_ACCESS_TOKEN;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.catalina.connector.Connector;
import org.apache.catalina.connector.Request;
import org.apache.catalina.connector.RequestFacade;
import org.apache.catalina.connector.Response;
import org.apache.catalina.connector.ResponseFacade;
import org.apache.catalina.core.StandardContext;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.keycloak.adapters.AuthOutcome;
import org.keycloak.adapters.KeycloakDeployment;
import org.keycloak.representations.AccessToken;
import org.mockito.Matchers;
import org.mockito.Mockito;
import org.nuxeo.ecm.platform.api.login.UserIdentificationInfo;
import org.nuxeo.runtime.test.runner.Deploy;
import org.nuxeo.runtime.test.runner.Features;
import org.nuxeo.runtime.test.runner.FeaturesRunner;
import org.nuxeo.runtime.test.runner.LocalDeploy;
import org.nuxeo.runtime.test.runner.RuntimeFeature;
@RunWith(FeaturesRunner.class)
@Features(RuntimeFeature.class)
@Deploy({ "org.nuxeo.usermapper" })
@LocalDeploy({ "org.nuxeo.ecm.platform.login.keycloak.test:OSGI-INF/keycloak-descriptor-bundle.xml" })
public class TestKeycloakAuthenticationPlugin {
private KeycloakRequestAuthenticator authenticatorMock = Mockito.mock(KeycloakRequestAuthenticator.class);
private KeycloakAuthenticatorProvider providerMock = Mockito.mock(KeycloakAuthenticatorProvider.class);
private Request requestMock = Mockito.mock(Request.class);
private Response responseMock = Mockito.mock(Response.class);
private RequestFacade requestFacade = new RequestFacade(requestMock);
private ResponseFacade responseFacade = new ResponseFacade(responseMock);
private Connector connectorMock = Mockito.mock(Connector.class);
private org.apache.coyote.Response coyoteResponseMock = new org.apache.coyote.Response();
private static final String INVALID_BEARER_TOKEN = "Bearer invalid";
@Before
public void setUp() throws Exception {
Mockito.when(requestMock.getConnector()).thenReturn(connectorMock);
Mockito.when(requestMock.getMethod()).thenReturn("GET");
Mockito.when(requestMock.getRequestURI()).thenReturn("/foo/path/to/resource");
Mockito.when(requestMock.getRequestURL()).thenReturn(
new StringBuffer().append("https://example.com:443/foo/path/to/resource"));
Mockito.when(requestMock.getScheme()).thenReturn("https");
Mockito.when(requestMock.getServerName()).thenReturn("example.com");
Mockito.when(requestMock.getServerPort()).thenReturn(443);
Mockito.when(requestMock.getContextPath()).thenReturn("/foo");
Mockito.when(requestMock.getContext()).thenReturn(new StandardContext());
Mockito.when(connectorMock.getRedirectPort()).thenReturn(8080);
}
@Test
public void testKeycloakBearerAuthenticationSucceeding() throws Exception {
KeycloakAuthenticationPlugin keycloakAuthenticationPlugin = new KeycloakAuthenticationPlugin();
initPlugin(keycloakAuthenticationPlugin);
AccessToken accessToken = new AccessToken();
accessToken.setEmail("username@example.com");
AccessToken.Access realmAccess = new AccessToken.Access();
realmAccess.addRole("user");
accessToken.setRealmAccess(realmAccess);
Mockito.when(requestMock.getAttribute(KEYCLOAK_ACCESS_TOKEN)).thenReturn(accessToken);
Mockito.when(authenticatorMock.authenticate()).thenReturn(AuthOutcome.AUTHENTICATED);
Mockito.when(providerMock.provide(any(HttpServletRequest.class), any(HttpServletResponse.class))).thenReturn(
authenticatorMock);
KeycloakDeployment deployment = new KeycloakDeployment();
deployment.setResourceName("test");
Mockito.when(providerMock.getResolvedDeployment()).thenReturn(deployment);
keycloakAuthenticationPlugin.setKeycloakAuthenticatorProvider(providerMock);
UserIdentificationInfo identity = keycloakAuthenticationPlugin.handleRetrieveIdentity(requestFacade,
responseMock);
assertNotNull(identity);
assertEquals("username@example.com", identity.getUserName());
}
@Test
public void testKeycloakBearerAuthenticationFailing() throws Exception {
KeycloakAuthenticationPlugin keycloakAuthenticationPlugin = new KeycloakAuthenticationPlugin();
initPlugin(keycloakAuthenticationPlugin);
// We'll check the response is marked committed
Mockito.when(responseMock.getCoyoteResponse()).thenReturn(coyoteResponseMock);
// No need to mock, just try the invalid bearer token
Mockito.when(requestMock.getHeaders(Matchers.matches("Authorization"))).thenReturn(
Collections.enumeration(Collections.singletonList(INVALID_BEARER_TOKEN)));
UserIdentificationInfo identity = keycloakAuthenticationPlugin.handleRetrieveIdentity(requestFacade,
responseFacade);
assertNull(identity);
Mockito.verify(responseMock).setStatus(401);
}
@Test
public void testKeycloakSiteAuthenticationFailing() throws Exception {
KeycloakAuthenticationPlugin keycloakAuthenticationPlugin = new KeycloakAuthenticationPlugin();
initPlugin(keycloakAuthenticationPlugin);
// We'll check the response is marked committed
Mockito.when(responseMock.getCoyoteResponse()).thenReturn(coyoteResponseMock);
// No need to mock, just try with NO bearer token
UserIdentificationInfo identity = keycloakAuthenticationPlugin.handleRetrieveIdentity(requestFacade,
responseFacade);
assertNull(identity);
Mockito.verify(responseMock).setStatus(302);
Mockito.verify(responseMock).setHeader(
Matchers.matches("Location"),
Matchers.startsWith("https://127.0.0.1:8443/auth/realms/demo/protocol/openid-connect/auth?"
+ "response_type=code&" + "client_id=customer-portal&"
+ "redirect_uri=https%3A%2F%2Fexample.com%3A443%2Ffoo%2Fpath%2Fto%2Fresource"));
}
@Test
public void testKeycloakSiteLogout() throws Exception {
KeycloakAuthenticationPlugin keycloakAuthenticationPlugin = new KeycloakAuthenticationPlugin();
initPlugin(keycloakAuthenticationPlugin);
// We'll check the response is marked committed
Mockito.when(responseMock.getCoyoteResponse()).thenReturn(coyoteResponseMock);
// No need to mock, just try with NO bearer token
Boolean result = keycloakAuthenticationPlugin.handleLogout(requestFacade, responseFacade);
assertNotNull(result);
assertEquals(true, result);
Mockito.verify(responseMock).sendRedirect(
"https://127.0.0.1:8443/auth/realms/demo/protocol/openid-connect/logout?redirect_uri=https://example.com:443/foo/nxstartup.faces");
}
private KeycloakAuthenticationPlugin initPlugin(KeycloakAuthenticationPlugin keycloakAuthenticationPlugin) {
Map<String, String> parameters = new HashMap<>();
// Add more configuration parameters in a future version
parameters.put(KeycloakAuthenticationPlugin.KEYCLOAK_CONFIG_FILE_KEY, "keycloak.json");
parameters.put(KeycloakAuthenticationPlugin.KEYCLOAK_MAPPING_NAME_KEY, "keycloakTest");
keycloakAuthenticationPlugin.initPlugin(parameters);
return keycloakAuthenticationPlugin;
}
}