2121import static org .eclipse .che .security .oauth .OAuthAuthenticator .SSL_ERROR_CODE ;
2222import static org .eclipse .che .security .oauth1 .OAuthAuthenticationService .ERROR_QUERY_NAME ;
2323
24+ import com .google .api .client .auth .oauth2 .TokenResponse ;
2425import jakarta .servlet .http .HttpServletRequest ;
2526import jakarta .ws .rs .HttpMethod ;
2627import jakarta .ws .rs .core .Response ;
6263 * @author Mykhailo Kuznietsov
6364 */
6465@ Singleton
65- public class EmbeddedOAuthAPI implements OAuthAPI {
66+ public class EmbeddedOAuthAPI implements OAuthAPI {
6667 private static final Logger LOG = LoggerFactory .getLogger (EmbeddedOAuthAPI .class );
6768
6869 @ Inject
@@ -106,7 +107,8 @@ public Response callback(UriInfo uriInfo, @Nullable List<String> errorValues)
106107 OAuthAuthenticator oauth = getAuthenticator (providerName );
107108 final List <String > scopes = params .get ("scope" );
108109 try {
109- String token = oauth .callback (requestUrl , scopes == null ? emptyList () : scopes );
110+ TokenResponse tokenResponse =
111+ oauth .callback (requestUrl , scopes == null ? emptyList () : scopes );
110112 personalAccessTokenManager .store (
111113 new PersonalAccessToken (
112114 oauth .getEndpointUrl (),
@@ -116,7 +118,9 @@ public Response callback(UriInfo uriInfo, @Nullable List<String> errorValues)
116118 null ,
117119 NameGenerator .generate (OAUTH_2_PREFIX , 5 ),
118120 NameGenerator .generate ("id-" , 5 ),
119- token ));
121+ tokenResponse .getAccessToken (),
122+ tokenResponse .getRefreshToken (),
123+ tokenResponse .getExpiresInSeconds ()));
120124 } catch (OAuthAuthenticationException e ) {
121125 return Response .temporaryRedirect (
122126 URI .create (
@@ -260,23 +264,44 @@ public OAuthToken refreshToken(String oauthProvider)
260264 throws NotFoundException , UnauthorizedException , ServerException {
261265 OAuthAuthenticator provider = getAuthenticator (oauthProvider );
262266 Subject subject = EnvironmentContext .getCurrent ().getSubject ();
267+ String userId = subject .getUserId ();
268+ String userName = subject .getUserName ();
263269 try {
264- OAuthToken token = provider .refreshToken (subject . getUserId () );
265- if (token == null ) {
266- token = provider .refreshToken (subject . getUserName () );
270+ OAuthToken storedToken = provider .refreshToken (userId );
271+ if (storedToken == null ) {
272+ storedToken = provider .refreshToken (userName );
267273 }
268274
269- if (token != null ) {
270- return token ;
275+ if (storedToken != null ) {
276+ return storedToken ;
271277 } else {
272- throw new UnauthorizedException (
273- "OAuth token for user " + subject .getUserId () + " was not found" );
278+ Optional <PersonalAccessToken > tokenOptional =
279+ personalAccessTokenManager .get (subject , oauthProvider , null , null );
280+ if (tokenOptional .isPresent ()) {
281+ PersonalAccessToken token = tokenOptional .get ();
282+ if (isNullOrEmpty (token .getRefreshToken ())) {
283+ throw getUnauthorizedException (userId );
284+ }
285+ TokenResponse tokenResponse =
286+ new TokenResponse ()
287+ .setAccessToken (token .getToken ())
288+ .setRefreshToken (token .getRefreshToken ())
289+ .setExpiresInSeconds (token .getExpiresIn ());
290+ provider .flow .createAndStoreCredential (tokenResponse , userId );
291+ return provider .refreshToken (userId );
292+ } else {
293+ throw getUnauthorizedException (userId );
294+ }
274295 }
275- } catch (IOException e ) {
296+ } catch (IOException | ScmConfigurationPersistenceException | ScmCommunicationException e ) {
276297 throw new ServerException (e .getLocalizedMessage (), e );
277298 }
278299 }
279300
301+ private UnauthorizedException getUnauthorizedException (String userId ) {
302+ return new UnauthorizedException ("OAuth token for user " + userId + " was not found" );
303+ }
304+
280305 @ Override
281306 public void invalidateToken (String oauthProvider )
282307 throws NotFoundException , UnauthorizedException , ServerException {
0 commit comments