5
5
import com .faforever .client .login .NoRefreshTokenException ;
6
6
import com .faforever .client .login .TokenRetrievalException ;
7
7
import com .faforever .client .preferences .LoginPrefs ;
8
+ import com .nimbusds .jwt .JWTParser ;
8
9
import javafx .beans .property .SimpleStringProperty ;
9
10
import javafx .beans .property .StringProperty ;
10
11
import lombok .RequiredArgsConstructor ;
@@ -41,9 +42,8 @@ public class TokenRetriever implements InitializingBean {
41
42
private final Flux <Long > invalidateFlux = invalidateSink .asFlux ().publish ().autoConnect ();
42
43
private final StringProperty refreshTokenValue = new SimpleStringProperty ();
43
44
44
- private final Mono <String > refreshedTokenMono = Mono .defer (this ::refreshAccess )
45
- .cacheInvalidateWhen (this ::getExpirationMono )
46
- .map (OAuth2AccessToken ::getTokenValue );
45
+ private final Mono <OAuth2AccessToken > refreshedTokenMono = Mono .defer (this ::refreshAccess )
46
+ .cacheInvalidateWhen (this ::getExpirationMono );
47
47
48
48
@ Override
49
49
public void afterPropertiesSet () throws Exception {
@@ -59,7 +59,17 @@ private Mono<Void> getExpirationMono(OAuth2AccessToken token) {
59
59
}
60
60
61
61
public Mono <String > getRefreshedTokenValue () {
62
- return refreshedTokenMono .doOnError (this ::onTokenError );
62
+ return refreshedTokenMono .map (OAuth2AccessToken ::getTokenValue ).doOnError (this ::onTokenError );
63
+ }
64
+
65
+ public Mono <String > getRefreshedHmacValue () {
66
+ return getRefreshedTokenValue ().flatMap (tokenValue -> {
67
+ try {
68
+ return Mono .just (JWTParser .parse (tokenValue ).getJWTClaimsSet ().getJSONObjectClaim ("ext" ).get ("hmac" ).toString ());
69
+ } catch (Exception e ) {
70
+ return Mono .error (e );
71
+ }
72
+ });
63
73
}
64
74
65
75
public Mono <Void > loginWithAuthorizationCode (String code , String codeVerifier , URI redirectUri ) {
@@ -99,26 +109,26 @@ private Mono<OAuth2AccessToken> refreshAccess() {
99
109
100
110
private Mono <OAuth2AccessToken > retrieveToken (MultiValueMap <String , String > properties ) {
101
111
return defaultWebClient .post ()
102
- .uri (String .format ("%s/oauth2/token" , clientProperties .getOauth ().getBaseUrl ()))
103
- .contentType (MediaType .APPLICATION_FORM_URLENCODED )
104
- .accept (MediaType .APPLICATION_JSON )
105
- .bodyValue (properties )
106
- .exchangeToMono (response -> {
107
- if (response .statusCode ().isError ()) {
108
- return response .bodyToMono (String .class )
109
- .switchIfEmpty (Mono .just (response .statusCode ().toString ()))
110
- .flatMap (body -> Mono .error (new TokenRetrievalException (body )));
111
- }
112
-
113
- return response .body (OAuth2BodyExtractors .oauth2AccessTokenResponse ());
114
- })
115
- .doOnSubscribe (subscription -> log .debug ("Retrieving OAuth token" ))
116
- .doOnNext (tokenResponse -> {
117
- OAuth2RefreshToken refreshToken = tokenResponse .getRefreshToken ();
118
- refreshTokenValue .set (refreshToken != null ? refreshToken .getTokenValue () : null );
119
- })
120
- .map (OAuth2AccessTokenResponse ::getAccessToken )
121
- .doOnNext (token -> log .info ("Token valid until {}" , token .getExpiresAt ()));
112
+ .uri (String .format ("%s/oauth2/token" , clientProperties .getOauth ().getBaseUrl ()))
113
+ .contentType (MediaType .APPLICATION_FORM_URLENCODED )
114
+ .accept (MediaType .APPLICATION_JSON )
115
+ .bodyValue (properties )
116
+ .exchangeToMono (response -> {
117
+ if (response .statusCode ().isError ()) {
118
+ return response .bodyToMono (String .class )
119
+ .switchIfEmpty (Mono .just (response .statusCode ().toString ()))
120
+ .flatMap (body -> Mono .error (new TokenRetrievalException (body )));
121
+ }
122
+
123
+ return response .body (OAuth2BodyExtractors .oauth2AccessTokenResponse ());
124
+ })
125
+ .doOnSubscribe (subscription -> log .debug ("Retrieving OAuth token" ))
126
+ .doOnNext (tokenResponse -> {
127
+ OAuth2RefreshToken refreshToken = tokenResponse .getRefreshToken ();
128
+ refreshTokenValue .set (refreshToken != null ? refreshToken .getTokenValue () : null );
129
+ })
130
+ .map (OAuth2AccessTokenResponse ::getAccessToken )
131
+ .doOnNext (token -> log .info ("Token valid until {}" , token .getExpiresAt ()));
122
132
}
123
133
124
134
public void invalidateToken () {
@@ -129,8 +139,4 @@ public void invalidateToken() {
129
139
public Flux <Long > invalidationFlux () {
130
140
return invalidateFlux ;
131
141
}
132
-
133
- public Mono <String > getAccessToken () {
134
- return refreshedTokenMono ;
135
- }
136
142
}
0 commit comments