1515
1616package com .rabbitmq .client .impl ;
1717
18+ import com .google .gson .Gson ;
1819import com .rabbitmq .client .test .TestUtils ;
1920import org .bouncycastle .asn1 .x500 .X500NameBuilder ;
2021import org .bouncycastle .asn1 .x500 .style .BCStyle ;
@@ -195,7 +196,7 @@ public void handle(String target, Request baseRequest, HttpServletRequest reques
195196 }
196197
197198 @ Test
198- public void parseToken () {
199+ public void parseTokenDefault () {
199200 OAuth2ClientCredentialsGrantCredentialsProvider provider = new OAuth2ClientCredentialsGrantCredentialsProvider (
200201 "http://localhost:8080/uaa/oauth/token/" ,
201202 "rabbit_client" , "rabbit_secret" ,
@@ -211,6 +212,36 @@ public void parseToken() {
211212 assertThat (token .getTimeBeforeExpiration ()).isBetween (Duration .ofSeconds (expiresIn - 10 ), Duration .ofSeconds (expiresIn + 1 ));
212213 }
213214
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+
214245 String sampleJsonToken (String accessToken , int expiresIn ) {
215246 String json = "{\n " +
216247 " \" access_token\" : \" {accessToken}\" ,\n " +
0 commit comments