4
4
package com .topcoder .direct .services .view .util ;
5
5
6
6
import com .topcoder .direct .services .configs .ServerConfiguration ;
7
- import com .topcoder .direct .services .view .dto .my .SingleRestResult ;
8
- import com .topcoder .direct .services .view .dto .my .Token ;
9
7
import com .topcoder .direct .services .view .exception .JwtAuthenticationException ;
10
8
import org .apache .commons .codec .binary .Base64 ;
11
9
import org .apache .http .HttpEntity ;
16
14
import org .apache .http .entity .StringEntity ;
17
15
import org .apache .http .impl .client .DefaultHttpClient ;
18
16
import org .apache .log4j .Logger ;
17
+ import org .apache .struts2 .ServletActionContext ;
19
18
import org .codehaus .jackson .JsonNode ;
20
19
import org .codehaus .jackson .map .DeserializationConfig ;
21
20
import org .codehaus .jackson .map .ObjectMapper ;
26
25
import java .text .SimpleDateFormat ;
27
26
import java .util .Date ;
28
27
29
- import org .apache .struts2 .ServletActionContext ;
30
-
31
28
import static sun .security .krb5 .internal .Krb5 .getErrorMessage ;
32
29
33
30
/**
@@ -93,11 +90,10 @@ public JwtTokenUpdater check() throws Exception {
93
90
}
94
91
95
92
96
- private Token getRefreshTokenFromApi (String oldToken ) throws Exception {
93
+ private String getRefreshTokenFromApi (String oldToken ) throws Exception {
97
94
DefaultHttpClient httpClient = new DefaultHttpClient ();
98
- SingleRestResult <Token > resultToken = null ;
99
95
try {
100
- URI authorizationUri = new URI (getAuthorizationURL () );
96
+ URI authorizationUri = new URI (this . authorizationURL );
101
97
HttpPost httpPost = new HttpPost (authorizationUri );
102
98
httpPost .addHeader (HttpHeaders .CONTENT_TYPE , "application/json" );
103
99
@@ -112,57 +108,54 @@ private Token getRefreshTokenFromApi(String oldToken) throws Exception {
112
108
}
113
109
114
110
JsonNode result = objectMapper .readTree (entity .getContent ());
115
- resultToken = objectMapper . readValue ( result . get ( "result" ),
116
- objectMapper . getTypeFactory (). constructParametricType ( SingleRestResult . class , Token . class ) );
111
+
112
+ return result . path ( "result" ). path ( "content" ). path ( "token" ). asText ( );
117
113
} finally {
118
114
httpClient .getConnectionManager ().shutdown ();
119
115
}
120
- return resultToken .getContent ();
121
116
}
122
117
123
118
/**
124
- * Verify token.If token expired: refresh it
119
+ * Verify token. If token expired: refresh it
125
120
*
126
- * @param tokenV3
127
- * @param tokenV2
121
+ * @param v3token the v3 jwt token
122
+ * @param v2token the v2 jwt token
128
123
* @return
129
124
* @throws JwtAuthenticationException
130
125
*/
131
- private String getValidJwtToken (String tokenV3 , String tokenV2 ) throws JwtAuthenticationException {
132
- String [] tokenSplit = tokenV3 .split ("\\ ." );
133
- boolean valid = true ;
134
- if (tokenSplit .length < 2 ) valid = false ;
135
-
136
- JsonNode jsonNode = null ;
126
+ private String getValidJwtToken (String v3token , String v2token ) throws JwtAuthenticationException {
127
+ String [] tokenSplit = v3token .split ("\\ ." );
128
+ boolean valid = tokenSplit .length >= 2 ;
137
129
138
130
try {
139
131
if (valid ) {
140
- StringBuffer payloadStr = new StringBuffer (tokenSplit [1 ]);
132
+ StringBuilder payloadStr = new StringBuilder (tokenSplit [1 ]);
141
133
while (payloadStr .length () % 4 != 0 ) payloadStr .append ('=' );
142
134
String payload = new String (Base64 .decodeBase64 (payloadStr .toString ().getBytes (StandardCharsets .UTF_8 )));
143
135
144
- jsonNode = objectMapper .readValue (payload . toString () , JsonNode .class );
136
+ JsonNode jsonNode = objectMapper .readValue (payload , JsonNode .class );
145
137
146
138
long exp = jsonNode .get ("exp" ).getLongValue ();
147
139
Date expDate = new Date (exp * 1000 );
148
140
logger .info ("token expire at: " + expDate );
149
- if (expDate .before (new Date ())) valid = false ;
150
- }
151
-
152
- if (!valid ) {
153
- logger .info ("refresh new token for : " + tokenV2 );
154
- Token newToken = getRefreshTokenFromApi (tokenV2 );
155
- if (newToken == null || newToken .getToken ().isEmpty ()) {
156
- throw new JwtAuthenticationException ("Invalid refresh token" );
141
+ if (expDate .after (new Date ())) {
142
+ return v3token ;
157
143
}
144
+ }
158
145
159
- return newToken .getToken ();
146
+ logger .info ("refresh v3 token for : " + v2token );
147
+ String newToken = getRefreshTokenFromApi (v2token );
148
+ if (newToken == null || newToken .isEmpty ()) {
149
+ throw new JwtAuthenticationException ("Invalid refreshed token - " + newToken );
160
150
}
151
+
152
+ return newToken ;
153
+ } catch (JwtAuthenticationException e ) {
154
+ throw e ;
161
155
} catch (Exception e ) {
162
156
throw new JwtAuthenticationException ("Failed to refresh toke through api, Please go to sso login page : " +
163
- getSsoLoginUrl () );
157
+ this . ssoLoginUrl , e );
164
158
}
165
- return tokenV3 ;
166
159
}
167
160
168
161
/**
@@ -172,11 +165,11 @@ private String getValidJwtToken(String tokenV3, String tokenV2) throws JwtAuthen
172
165
* @param v3 cookie v3
173
166
* @throws Exception
174
167
*/
175
- private void validateCookieV2V3 (Cookie v2 , Cookie v3 ) throws Exception {
168
+ private void validateCookieV2V3 (Cookie v2 , Cookie v3 ) throws Exception {
176
169
String validToken ;
177
170
String v3Token = null ;
178
171
if (v3 == null ) {
179
- validToken = getRefreshTokenFromApi (v2 .getValue ()). getToken () ;
172
+ validToken = getRefreshTokenFromApi (v2 .getValue ());
180
173
} else {
181
174
validToken = getValidJwtToken (v3 .getValue (), v2 .getValue ());
182
175
v3Token = v3 .getValue ();
0 commit comments