15
15
16
16
package com .rabbitmq .client .impl ;
17
17
18
+ import com .google .gson .Gson ;
18
19
import com .rabbitmq .client .test .TestUtils ;
19
20
import org .bouncycastle .asn1 .x500 .X500NameBuilder ;
20
21
import org .bouncycastle .asn1 .x500 .style .BCStyle ;
@@ -195,7 +196,7 @@ public void handle(String target, Request baseRequest, HttpServletRequest reques
195
196
}
196
197
197
198
@ Test
198
- public void parseToken () {
199
+ public void parseTokenDefault () {
199
200
OAuth2ClientCredentialsGrantCredentialsProvider provider = new OAuth2ClientCredentialsGrantCredentialsProvider (
200
201
"http://localhost:8080/uaa/oauth/token/" ,
201
202
"rabbit_client" , "rabbit_secret" ,
@@ -211,6 +212,36 @@ public void parseToken() {
211
212
assertThat (token .getTimeBeforeExpiration ()).isBetween (Duration .ofSeconds (expiresIn - 10 ), Duration .ofSeconds (expiresIn + 1 ));
212
213
}
213
214
215
+ @ Test
216
+ public void parseTokenGson () {
217
+ Gson gson = new Gson ();
218
+ OAuth2ClientCredentialsGrantCredentialsProvider provider = new OAuth2ClientCredentialsGrantCredentialsProvider (
219
+ "http://localhost:8080/uaa/oauth/token/" ,
220
+ "rabbit_client" , "rabbit_secret" ,
221
+ "client_credentials"
222
+ ) {
223
+ @ Override
224
+ protected Token parseToken (String response ) {
225
+ try {
226
+ Map <?, ?> map = gson .fromJson (response , Map .class );
227
+ int expiresIn = ((Number ) map .get ("expires_in" )).intValue ();
228
+ Instant receivedAt = Instant .now ();
229
+ return new Token (map .get ("access_token" ).toString (), expiresIn , receivedAt );
230
+ } catch (Exception e ) {
231
+ throw new OAuthTokenManagementException ("Error while parsing OAuth 2 token" , e );
232
+ }
233
+ }
234
+ };
235
+
236
+ String accessToken = "18c1b1dfdda04382a8bcc14d077b71dd" ;
237
+ int expiresIn = 43199 ;
238
+ String response = sampleJsonToken (accessToken , expiresIn );
239
+
240
+ OAuth2ClientCredentialsGrantCredentialsProvider .Token token = provider .parseToken (response );
241
+ assertThat (token .getAccess ()).isEqualTo ("18c1b1dfdda04382a8bcc14d077b71dd" );
242
+ assertThat (token .getTimeBeforeExpiration ()).isBetween (Duration .ofSeconds (expiresIn - 10 ), Duration .ofSeconds (expiresIn + 1 ));
243
+ }
244
+
214
245
String sampleJsonToken (String accessToken , int expiresIn ) {
215
246
String json = "{\n " +
216
247
" \" access_token\" : \" {accessToken}\" ,\n " +
0 commit comments