Example usage for org.springframework.security.oauth2.common OAuth2AccessToken getValue

List of usage examples for org.springframework.security.oauth2.common OAuth2AccessToken getValue

Introduction

In this page you can find the example usage for org.springframework.security.oauth2.common OAuth2AccessToken getValue.

Prototype

String getValue();

Source Link

Usage

From source file:org.apigw.monitoring.svc.impl.MonitoredTokenServices.java

@Override
@Transactional/*from  w w w.  jav  a  2  s . co  m*/
public OAuth2AccessToken createAccessToken(String legalGuardianResidentIdentificationNumber,
        String citizenResidentIdentificationNumber, String clientId, Collection<String> scope)
        throws AuthenticationException, LegalGuardianValidationException {
    logger.debug("createAccessToken from trusted client for legalGuardian - start");
    String tokenValue = null;
    Set<String> scopeSet = new HashSet<>();
    String user = legalGuardianResidentIdentificationNumber + "/" + citizenResidentIdentificationNumber;
    try {
        OAuth2AccessToken accessToken = tokenServices.createAccessToken(
                legalGuardianResidentIdentificationNumber, citizenResidentIdentificationNumber, clientId,
                scope);
        tokenValue = accessToken.getValue();
        scopeSet = accessToken.getScope();
        monitorCreateAccessToken(clientId, scopeSet, tokenValue, RequestState.SUCCESS, "TRUSTED", user,
                "TRUSTED");
        logger.debug("createAccessToken from trusted client for legalGuardian - end");
        return accessToken;
    } catch (ApigwMonitoringException e) {
        throw e;
    } catch (RuntimeException e) {
        logger.error("error creating access token from trusted client for legalGuardian", e);
        monitorCreateAccessToken(clientId, scopeSet, tokenValue, RequestState.SERVER_FAILURE, e.getMessage(),
                user, "TRUSTED");
        throw e;
    }
}

From source file:it.smartcommunitylab.aac.apimanager.APIProviderManager.java

/**
 * //  w ww.  j  a  va  2 s  .co  m
 * @return
 * @throws Exception
 */
@Transactional(isolation = Isolation.SERIALIZABLE)
public String createToken(String username, String password) throws Exception {
    Map<String, String> requestParameters = new HashMap<>();

    User userObj = userRepository.findByUsername(username);

    if (userObj != null) {
        Long userId = userObj.getId();

        requestParameters.put("username", username);
        requestParameters.put("password", password);

        // USER
        org.springframework.security.core.userdetails.User user = new org.springframework.security.core.userdetails.User(
                userId.toString(), "", new ArrayList<GrantedAuthority>());

        ClientDetails clientDetails = getAPIMgmtClient();
        TokenRequest tokenRequest = new TokenRequest(requestParameters, clientDetails.getClientId(), scopes(),
                "password");
        OAuth2Request oAuth2Request = tokenRequest.createOAuth2Request(clientDetails);
        Collection<? extends GrantedAuthority> list = authorities(userObj);
        OAuth2Authentication oAuth2Authentication = new OAuth2Authentication(oAuth2Request,
                new UsernamePasswordAuthenticationToken(user, "", list));
        OAuth2AccessToken accessToken = tokenService.createAccessToken(oAuth2Authentication);
        return accessToken.getValue();
    }
    return null;
}

From source file:it.smartcommunitylab.aac.oauth.NonRemovingTokenServices.java

private OAuth2AccessToken refreshWithRepeat(String refreshTokenValue, TokenRequest request, boolean repeat) {
    OAuth2AccessToken accessToken = localtokenStore.readAccessTokenForRefreshToken(refreshTokenValue);
    if (accessToken == null) {
        throw new InvalidGrantException("Invalid refresh token: " + refreshTokenValue);
    }// w  w w  .  j a v a 2s .  c o  m

    if (accessToken.getExpiration().getTime() - System.currentTimeMillis() > tokenThreshold * 1000L) {
        return accessToken;
    }

    try {
        OAuth2AccessToken res = super.refreshAccessToken(refreshTokenValue, request);
        OAuth2Authentication auth = localtokenStore.readAuthentication(res);
        traceUserLogger.info(
                String.format("'type':'refresh','user':'%s','token':'%s'", auth.getName(), res.getValue()));
        return res;
    } catch (RuntimeException e) {
        // do retry: it may be the case of race condition so retry the operation but only once
        if (!repeat)
            return refreshWithRepeat(refreshTokenValue, request, true);
        throw e;
    }
}

From source file:org.cloudfoundry.identity.uaa.integration.ClientAdminEndpointsIntegrationTests.java

public HttpHeaders getAuthenticatedHeaders(OAuth2AccessToken token) {
    HttpHeaders headers = new HttpHeaders();
    headers.setAccept(Arrays.asList(MediaType.APPLICATION_JSON));
    headers.setContentType(MediaType.APPLICATION_JSON);
    headers.set("Authorization", "Bearer " + token.getValue());
    return headers;
}

From source file:org.cloudfoundry.client.lib.rest.CloudControllerClientV2.java

public String login() {
    OAuth2AccessToken token = oauthClient.getToken(cloudCredentials.getEmail(), cloudCredentials.getPassword());
    this.token = token.getTokenType() + " " + token.getValue();
    return this.token;
}

From source file:com.cedac.security.oauth2.provider.token.store.MongoTokenStore.java

public void removeAccessToken(OAuth2AccessToken token) {
    removeAccessToken(token.getValue());
}

From source file:com.cedac.security.oauth2.provider.token.store.MongoTokenStore.java

public OAuth2Authentication readAuthentication(OAuth2AccessToken token) {
    return readAuthentication(token.getValue());
}

From source file:org.apigw.authserver.web.controller.CertifiedClientsController.java

private CertifiedClientDetails retrieveUserDetailsForCertifiedClient(String clientID,
        Map<String, Collection<OAuth2AccessToken>> accessTokens) {
    SimpleDateFormat formatter = getTimestampFormatter();
    Date now = new Date();

    CertifiedClientDetails certifiedClientDetails = new CertifiedClientDetails();
    for (Map.Entry<String, Collection<OAuth2AccessToken>> entry : accessTokens.entrySet()) {
        //Find all users that match this client
        for (OAuth2AccessToken token : entry.getValue()) {
            if (token.getExpiration() == null || token.getExpiration().before(now)) {
                continue;
            }//from  ww w .j a v  a2 s  . c o  m
            String tokenValue = token.getValue();
            String userClientID = consumerTokenServices.getClientId(tokenValue);
            if (userClientID.equalsIgnoreCase(clientID)) {
                UserDetail userDetails = new UserDetail();
                userDetails.setResidentId(entry.getKey());

                if (token.getExpiration() != null) {
                    userDetails.setExpires(formatter.format(token.getExpiration()));
                }
                String scopes = getScopesString(token.getScope());
                userDetails.setScopes(scopes);

                Map<String, Object> addInfo = token.getAdditionalInformation();
                userDetails.setGrantId(addInfo.get("authorization_grant_id").toString());

                if (addInfo != null && addInfo.get("issue_date") != null
                        && addInfo.get("issue_date") instanceof Date) {
                    userDetails.setIssued(formatter.format(addInfo.get("issue_date")));
                }

                if (certifiedClientDetails.getClientId() != null) {
                    certifiedClientDetails.getUserDetails().add(userDetails);
                } else {
                    CertifiedClient client = (CertifiedClient) clientDetailsService
                            .loadClientByClientId(userClientID);
                    certifiedClientDetails.setClientId(clientID);
                    certifiedClientDetails.setClientName(client.getName());
                    certifiedClientDetails.setOrganization(client.getOrganization());
                    certifiedClientDetails.setDescription(client.getDescription());
                    certifiedClientDetails.getUserDetails().add(userDetails);
                }
            }
        }
    }

    return certifiedClientDetails;
}

From source file:com.github.biegleux.gae.oauth.tokenstore.GaeTokenStore.java

@Override
public void storeAccessToken(OAuth2AccessToken token, OAuth2Authentication authentication) {
    String refreshToken = null;//  w  w w. j av  a 2  s . c om
    if (token.getRefreshToken() != null) {
        refreshToken = token.getRefreshToken().getValue();
    }

    if (readAccessToken(token.getValue()) != null) {
        removeAccessToken(token.getValue());
    }

    GaeOAuthAccessToken gaeOAuthAccessToken = new GaeOAuthAccessToken();
    gaeOAuthAccessToken.setTokenId(extractTokenKey(token.getValue()));
    gaeOAuthAccessToken.setToken(token);
    gaeOAuthAccessToken.setAuthenticationId(authenticationKeyGenerator.extractKey(authentication));
    gaeOAuthAccessToken.setUsername(authentication.isClientOnly() ? null : authentication.getName());
    gaeOAuthAccessToken.setClientId(authentication.getOAuth2Request().getClientId());
    gaeOAuthAccessToken.setAuthentication(authentication);
    gaeOAuthAccessToken.setRefreshToken(extractTokenKey(refreshToken));
    accessTokens.save(gaeOAuthAccessToken);
}