-
Notifications
You must be signed in to change notification settings - Fork 159
Expand file tree
/
Copy pathOAuth2AuthCodeTest.java
More file actions
223 lines (198 loc) · 7.97 KB
/
OAuth2AuthCodeTest.java
File metadata and controls
223 lines (198 loc) · 7.97 KB
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
223
package io.vertx.tests;
import io.vertx.core.Future;
import io.vertx.core.http.HttpMethod;
import io.vertx.core.http.HttpServer;
import io.vertx.core.json.JsonObject;
import io.vertx.ext.auth.authentication.Credentials;
import io.vertx.ext.auth.impl.http.SimpleHttpClient;
import io.vertx.ext.auth.oauth2.*;
import io.vertx.ext.unit.Async;
import io.vertx.ext.unit.TestContext;
import io.vertx.ext.unit.junit.RunTestOnContext;
import io.vertx.ext.unit.junit.VertxUnitRunner;
import org.junit.After;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
@RunWith(VertxUnitRunner.class)
public class OAuth2AuthCodeTest {
@Rule
public final RunTestOnContext rule = new RunTestOnContext();
private static final JsonObject fixtureTokens = new JsonObject(
"{" +
" \"access_token\": \"4adc339e0\"," +
" \"refresh_token\": \"ec1a59d298\"," +
" \"token_type\": \"bearer\"," +
" \"expires_in\": 7200" +
"}");
private static final JsonObject fixtureJwks = new JsonObject(
"{\"keys\":" +
" [ " +
" {" +
" \"kty\":\"RSA\"," +
" \"n\": \"0vx7agoebGcQSuuPiLJXZptN9nndrQmbXEps2aiAFbWhM78LhWx4cbbfAAtVT86zwu1RK7aPFFxuhDR1L6tSoc_BJECPebWKRXjBZCiFV4n3oknjhMstn64tZ_2W-5JsGY4Hc5n9yBXArwl93lqt7_RN5w6Cf0h4QyQ5v-65YGjQR0_FDW2QvzqY368QQMicAtaSqzs8KJZgnYb9c7d0zgdAZHzu6qMQvRL5hajrn1n91CbOpbISD08qNLyrdkt-bFTWhAI4vMQFh6WeZu0fM4lFd2NcRwr3XPksINHaQ-G_xBniIqbw0Ls1jF44-csFCur-kEgU8awapJzKnqDKgw\"," +
" \"e\":\"AQAB\"," +
" \"alg\":\"RS256\"," +
" \"kid\":\"1\"" +
" }" +
" ]" +
"}");
private static final Credentials tokenConfig = new Oauth2Credentials()
.setFlow(OAuth2FlowType.AUTH_CODE)
.setCode("code")
.setRedirectUri("http://callback.com");
private static final JsonObject oauthConfig = new JsonObject()
.put("code", "code")
.put("redirect_uri", "http://callback.com")
.put("grant_type", "authorization_code");
private static final Credentials tokenConfigSecretJwt = new Oauth2Credentials()
.setFlow(OAuth2FlowType.AUTH_CODE)
.setCode("code")
.setRedirectUri("http://callback.com")
.setAssertion("eyJhb");
private static final JsonObject oauthConfigSecretJwt = new JsonObject()
.put("code", "code")
.put("redirect_uri", "http://callback.com")
.put("client_assertion", "eyJhb")
.put("grant_type", "authorization_code")
.put("client_id", "client-id")
.put("client_assertion_type", "urn:ietf:params:oauth:client-assertion-type:jwt-bearer");
private static final OAuth2AuthorizationURL authorizeConfig = new OAuth2AuthorizationURL()
.setRedirectUri("http://localhost:3000/callback")
.addScope("user")
.setState("02afe928b");
protected OAuth2Auth oauth2;
private HttpServer server;
private JsonObject config;
private int connectionCounter;
private int currentPort;
@Before
public void setUp(TestContext should) throws Exception {
final Async setup = should.async();
server = rule.vertx().createHttpServer()
.connectionHandler(c -> connectionCounter++)
.requestHandler(req -> {
if (req.method() == HttpMethod.POST && "/oauth/token".equals(req.path())) {
if (req.getHeader("Authorization") != null) {
should.assertEquals("Basic Y2xpZW50LWlkOmNsaWVudC1zZWNyZXQ=", req.getHeader("Authorization"));
req.setExpectMultipart(true).bodyHandler(buffer -> {
try {
should.assertEquals(config, SimpleHttpClient.queryToJson(buffer));
req.response().putHeader("Content-Type", "application/json").end(fixtureTokens.encode());
} catch (UnsupportedEncodingException e) {
should.fail(e);
}
});
} else {
req.setExpectMultipart(true).bodyHandler(buffer -> {
try {
should.assertEquals(config, SimpleHttpClient.queryToJson(buffer));
req.response().putHeader("Content-Type", "application/json").end(fixtureTokens.encode());
} catch (UnsupportedEncodingException e) {
should.fail(e);
}
});
}
} else if (req.method() == HttpMethod.GET && "/oauth/jwks".equals(req.path())) {
req.bodyHandler(buffer -> {
req.response().putHeader("Content-Type", "application/json").end(fixtureJwks.encode());
});
} else {
req.response().setStatusCode(400).end();
}
});
server.listen(0).onComplete(ready -> {
if (ready.failed()) {
throw new RuntimeException(ready.cause());
}
oauth2 = OAuth2Auth.create(rule.vertx(), new OAuth2Options()
.setClientId("client-id")
.setClientSecret("client-secret")
.setJwkPath("/oauth/jwks")
.setSite("http://localhost:" + ready.result().actualPort()));
currentPort = ready.result().actualPort();
// ready
setup.complete();
});
connectionCounter = 0;
}
@After
public void tearDown(TestContext should) throws Exception {
final Async after = should.async();
server.close()
.onFailure(should::fail)
.onSuccess(v -> after.complete());
}
@Test
public void generateAuthorizeURL(TestContext should) throws Exception {
String expected = "http://localhost:" + currentPort + "/oauth/authorize?redirect_uri=" + URLEncoder.encode("http://localhost:3000/callback", "UTF-8") + "&state=02afe928b&scope=user&response_type=code&client_id=client-id";
should.assertEquals(expected, oauth2.authorizeURL(authorizeConfig));
}
@Test
public void generateAuthorizeURLTypeSafe(TestContext should) throws Exception {
String expected = "http://localhost:" + currentPort + "/oauth/authorize?prompt=none+login+consent&login_hint=my-username&redirect_uri=" + URLEncoder.encode("http://localhost:3000/callback", "UTF-8") + "&state=02afe928b&scope=user&response_type=code&client_id=client-id";
should.assertEquals(expected, oauth2.authorizeURL(new OAuth2AuthorizationURL(authorizeConfig)
.setLoginHint("my-username")
.setPrompt("none login consent")));
}
@Test
public void getToken(TestContext should) {
final Async test = should.async();
config = oauthConfig;
oauth2.jWKSet()
.onFailure(should::fail)
.onSuccess(v -> {
oauth2.authenticate(tokenConfig)
.onFailure(should::fail)
.onSuccess(token -> {
should.assertNotNull(token);
should.assertNotNull(token.principal());
should.assertNotNull(token.principal().getString("access_token"));
test.complete();
});
});
}
@Test
public void getTokenWithClientSecretJwt(TestContext should) {
final Async test = should.async();
config = oauthConfigSecretJwt;
oauth2 = OAuth2Auth.create(rule.vertx(), new OAuth2Options()
.setClientId(oauthConfigSecretJwt.getString("client_id"))
.setClientAssertionType(oauthConfigSecretJwt.getString("client_assertion_type"))
.setSite("http://localhost:" + currentPort));
oauth2.authenticate(tokenConfigSecretJwt)
.onFailure(should::fail)
.onSuccess(token -> {
should.assertNotNull(token);
should.assertNotNull(token.principal());
should.assertNotNull(token.principal().getString("access_token"));
test.complete();
});
}
@Test
public void testConnectionReuse(TestContext should) {
final Async test = should.async();
auth()
.compose(x -> auth())
.compose(x -> auth())
.compose(x -> auth())
.onComplete(r -> {
if (r.failed()) {
should.fail(r.cause());
} else {
// on slow environments multiple connections may be used
should.assertTrue(connectionCounter < 3);
test.complete();
}
});
}
Future<Void> auth() {
config = oauthConfig;
return oauth2
.authenticate(tokenConfig)
.mapEmpty();
}
}