44package com .topcoder .direct .services .view .util ;
55
66import 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 ;
97import com .topcoder .direct .services .view .exception .JwtAuthenticationException ;
108import org .apache .commons .codec .binary .Base64 ;
119import org .apache .http .HttpEntity ;
1614import org .apache .http .entity .StringEntity ;
1715import org .apache .http .impl .client .DefaultHttpClient ;
1816import org .apache .log4j .Logger ;
17+ import org .apache .struts2 .ServletActionContext ;
1918import org .codehaus .jackson .JsonNode ;
2019import org .codehaus .jackson .map .DeserializationConfig ;
2120import org .codehaus .jackson .map .ObjectMapper ;
2625import java .text .SimpleDateFormat ;
2726import java .util .Date ;
2827
29- import org .apache .struts2 .ServletActionContext ;
30-
3128import static sun .security .krb5 .internal .Krb5 .getErrorMessage ;
3229
3330/**
@@ -93,11 +90,10 @@ public JwtTokenUpdater check() throws Exception {
9390 }
9491
9592
96- private Token getRefreshTokenFromApi (String oldToken ) throws Exception {
93+ private String getRefreshTokenFromApi (String oldToken ) throws Exception {
9794 DefaultHttpClient httpClient = new DefaultHttpClient ();
98- SingleRestResult <Token > resultToken = null ;
9995 try {
100- URI authorizationUri = new URI (getAuthorizationURL () );
96+ URI authorizationUri = new URI (this . authorizationURL );
10197 HttpPost httpPost = new HttpPost (authorizationUri );
10298 httpPost .addHeader (HttpHeaders .CONTENT_TYPE , "application/json" );
10399
@@ -112,57 +108,54 @@ private Token getRefreshTokenFromApi(String oldToken) throws Exception {
112108 }
113109
114110 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 ( );
117113 } finally {
118114 httpClient .getConnectionManager ().shutdown ();
119115 }
120- return resultToken .getContent ();
121116 }
122117
123118 /**
124- * Verify token.If token expired: refresh it
119+ * Verify token. If token expired: refresh it
125120 *
126- * @param tokenV3
127- * @param tokenV2
121+ * @param v3token the v3 jwt token
122+ * @param v2token the v2 jwt token
128123 * @return
129124 * @throws JwtAuthenticationException
130125 */
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 ;
137129
138130 try {
139131 if (valid ) {
140- StringBuffer payloadStr = new StringBuffer (tokenSplit [1 ]);
132+ StringBuilder payloadStr = new StringBuilder (tokenSplit [1 ]);
141133 while (payloadStr .length () % 4 != 0 ) payloadStr .append ('=' );
142134 String payload = new String (Base64 .decodeBase64 (payloadStr .toString ().getBytes (StandardCharsets .UTF_8 )));
143135
144- jsonNode = objectMapper .readValue (payload . toString () , JsonNode .class );
136+ JsonNode jsonNode = objectMapper .readValue (payload , JsonNode .class );
145137
146138 long exp = jsonNode .get ("exp" ).getLongValue ();
147139 Date expDate = new Date (exp * 1000 );
148140 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 ;
157143 }
144+ }
158145
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 );
160150 }
151+
152+ return newToken ;
153+ } catch (JwtAuthenticationException e ) {
154+ throw e ;
161155 } catch (Exception e ) {
162156 throw new JwtAuthenticationException ("Failed to refresh toke through api, Please go to sso login page : " +
163- getSsoLoginUrl () );
157+ this . ssoLoginUrl , e );
164158 }
165- return tokenV3 ;
166159 }
167160
168161 /**
@@ -172,11 +165,11 @@ private String getValidJwtToken(String tokenV3, String tokenV2) throws JwtAuthen
172165 * @param v3 cookie v3
173166 * @throws Exception
174167 */
175- private void validateCookieV2V3 (Cookie v2 , Cookie v3 ) throws Exception {
168+ private void validateCookieV2V3 (Cookie v2 , Cookie v3 ) throws Exception {
176169 String validToken ;
177170 String v3Token = null ;
178171 if (v3 == null ) {
179- validToken = getRefreshTokenFromApi (v2 .getValue ()). getToken () ;
172+ validToken = getRefreshTokenFromApi (v2 .getValue ());
180173 } else {
181174 validToken = getValidJwtToken (v3 .getValue (), v2 .getValue ());
182175 v3Token = v3 .getValue ();
0 commit comments