List of usage examples for org.springframework.security.oauth2.common OAuth2AccessToken isExpired
boolean isExpired();
From source file:com.example.ProxyAuthorizationServerTokenServices.java
@Override public OAuth2AccessToken createAccessToken(OAuth2Authentication authentication) throws AuthenticationException { Authentication user = authentication.getUserAuthentication(); if (user instanceof CloudFoundryAuthentication) { CloudFoundryAuthentication cfuser = (CloudFoundryAuthentication) user; OAuth2AccessToken token = cfuser.getToken(); if (token.isExpired()) { CloudCredentials credentials = new CloudCredentials(token); CloudFoundryClient client = new CloudFoundryClient(credentials, properties.getApi()); token = client.login();/*w ww . ja va2 s . c o m*/ cfuser.setToken(token); } return token; } throw new AuthenticationCredentialsNotFoundException("No Cloud Foundy authentication found"); }
From source file:com.companyname.filters.Oauth2ReAuthenticationFilter.java
private String refreshAccesTokenIfExpired(String accessTokenValue, Authentication authentication) { OAuth2AccessToken accessToken = getTokenService().readAccessToken(accessTokenValue); if (accessToken != null && accessToken.isExpired() && authentication != null) { logger.info("access token is expired. will refresh"); accessToken = getTokenService().createAccessToken((OAuth2Authentication) authentication); } else if (accessToken != null && !accessToken.isExpired()) { logger.info("access token is not expired"); }//from w ww . j av a 2 s . c o m return (accessToken == null) ? null : accessToken.getValue(); }
From source file:eu.trentorise.smartcampus.permissionprovider.oauth.NonRemovingTokenServices.java
/** * Do not remove access token if expired *///from w w w .jav a 2 s .com @Override public OAuth2Authentication loadAuthentication(String accessTokenValue) throws AuthenticationException { OAuth2AccessToken accessToken = localtokenStore.readAccessToken(accessTokenValue); if (accessToken == null) { throw new InvalidTokenException("Invalid access token: " + accessTokenValue); } else if (accessToken.isExpired()) { logger.error("Accessing expired token: " + accessTokenValue); throw new InvalidTokenException("Access token expired: " + accessTokenValue); } OAuth2Authentication result = localtokenStore.readAuthentication(accessToken); return result; }
From source file:eu.trentorise.smartcampus.resourceprovider.filter.ResourceAuthenticationManager.java
private OAuth2Authentication loadAuthentication(String token) { OAuth2AccessToken accessToken = tokenStore.readAccessToken(token); if (accessToken == null) { throw new InvalidTokenException("Invalid access token: " + token); } else if (accessToken.isExpired()) { // tokenStore.removeAccessToken(accessToken); throw new InvalidTokenException("Access token expired: " + token); }/*from ww w. ja v a 2 s .c o m*/ OAuth2Authentication result = tokenStore.readAuthentication(accessToken); return result; }
From source file:org.eclipse.cft.server.core.internal.client.ClientRequest.java
/** * Attempts to execute the client request by first checking proxy settings, * and if unauthorised/forbidden exceptions thrown the first time, will * attempt to log in. If that succeeds, it will attempt one more time. * Otherwise it will fail and not attempt the request any further. * @param client//from w ww . j a v a2 s . com * @param cloudServer * @param subProgress * @return * @throws CoreException if attempt to execute failed, even after a second * attempt after a client login. */ @Override protected T runAndWait(CloudFoundryOperations client, SubMonitor subProgress) throws CoreException { try { return super.runAndWait(client, subProgress); } catch (CoreException ce) { CloudFoundryServer server = null; if (this instanceof BehaviourRequest) { // Optionally, child requests may provide a cloud server for use by the login handler BehaviourRequest<?> br = (BehaviourRequest<T>) this; server = br.getCloudServer(); } CloudFoundryLoginHandler handler = new CloudFoundryLoginHandler(client, server); CoreException accessError = null; String accessErrorMessage = null; if (handler.shouldAttemptClientLogin(ce)) { CloudFoundryPlugin .logWarning(NLS.bind(Messages.ClientRequest_RETRY_REQUEST, getTokenAccessErrorLabel())); accessError = ce; int attempts = 3; OAuth2AccessToken token = handler.login(subProgress, attempts, CloudOperationsConstants.LOGIN_INTERVAL); if (token == null) { accessErrorMessage = Messages.ClientRequest_NO_TOKEN; } else if (token.isExpired()) { accessErrorMessage = Messages.ClientRequest_TOKEN_EXPIRED; } else { try { return super.runAndWait(client, subProgress); } catch (CoreException e) { accessError = e; } } } if (accessError != null) { Throwable cause = accessError.getCause() != null ? accessError.getCause() : accessError; if (accessErrorMessage == null) { accessErrorMessage = accessError.getMessage(); } accessErrorMessage = NLS.bind(Messages.ClientRequest_SECOND_ATTEMPT_FAILED, getTokenAccessErrorLabel(), accessErrorMessage); throw CloudErrorUtil.toCoreException(accessErrorMessage, cause); } throw ce; } }
From source file:it.smartcommunitylab.aac.oauth.NonRemovingTokenServices.java
@Transactional(isolation = Isolation.SERIALIZABLE) public OAuth2AccessToken createAccessToken(OAuth2Authentication authentication) throws AuthenticationException { OAuth2AccessToken existingAccessToken = localtokenStore.getAccessToken(authentication); OAuth2RefreshToken refreshToken = null; if (existingAccessToken != null) { if (existingAccessToken.isExpired()) { if (existingAccessToken.getRefreshToken() != null) { refreshToken = existingAccessToken.getRefreshToken(); // The token store could remove the refresh token when the access token is removed, but we want to // be sure... localtokenStore.removeRefreshToken(refreshToken); }//from ww w.ja va 2 s .co m localtokenStore.removeAccessToken(existingAccessToken); } else { return tokenEnhancer != null ? tokenEnhancer.enhance(existingAccessToken, authentication) : existingAccessToken; } } // Only create a new refresh token if there wasn't an existing one associated with an expired access token. // Clients might be holding existing refresh tokens, so we re-use it in the case that the old access token // expired. if (refreshToken == null) { refreshToken = createRefreshToken(authentication); } // But the refresh token itself might need to be re-issued if it has expired. else if (refreshToken instanceof ExpiringOAuth2RefreshToken) { ExpiringOAuth2RefreshToken expiring = (ExpiringOAuth2RefreshToken) refreshToken; if (isExpired(expiring)) { refreshToken = createRefreshToken(authentication); } } OAuth2AccessToken accessToken = createAccessToken(authentication, refreshToken); localtokenStore.storeAccessToken(accessToken, authentication); if (refreshToken != null) { localtokenStore.storeRefreshToken(refreshToken, authentication); } traceUserLogger.info(String.format("'type':'new','user':'%s','token':'%s'", authentication.getName(), accessToken.getValue())); return accessToken; }
From source file:org.zalando.stups.oauth2.spring.client.StupsTokensAccessTokenProviderTest.java
@Test public void testObtainAccessToken() throws Exception { when(mockAccessTokens.getAccessToken(anyString())) .thenReturn(new AccessToken("12345", "bearer", 3600, tomorrow())); final OAuth2AccessToken accessToken = accessTokenProvider .obtainAccessToken(new BaseOAuth2ProtectedResourceDetails(), new DefaultAccessTokenRequest()); assertThat(accessToken).isNotNull(); assertThat(accessToken.getValue()).isEqualTo("12345"); assertThat(accessToken.getTokenType()).isEqualTo("Bearer"); assertThat(accessToken.isExpired()).isFalse(); verify(mockAccessTokens).getAccessToken(eq(TOKEN_ID)); }
From source file:org.zalando.stups.oauth2.spring.client.StupsTokensAccessTokenProviderTest.java
@Test public void testObtainExpiredAccessToken() throws Exception { when(mockAccessTokens.getAccessToken(anyString())) .thenReturn(new AccessToken("12345", "bearer", 3600, yesterday())); final OAuth2AccessToken accessToken = accessTokenProvider .obtainAccessToken(new BaseOAuth2ProtectedResourceDetails(), new DefaultAccessTokenRequest()); assertThat(accessToken).isNotNull(); assertThat(accessToken.getValue()).isEqualTo("12345"); assertThat(accessToken.getTokenType()).isEqualTo("Bearer"); assertThat(accessToken.isExpired()).isTrue(); verify(mockAccessTokens).getAccessToken(eq(TOKEN_ID)); }