Example usage for org.springframework.security.oauth2.provider OAuth2Authentication getOAuth2Request

List of usage examples for org.springframework.security.oauth2.provider OAuth2Authentication getOAuth2Request

Introduction

In this page you can find the example usage for org.springframework.security.oauth2.provider OAuth2Authentication getOAuth2Request.

Prototype

public OAuth2Request getOAuth2Request() 

Source Link

Document

The authorization request containing details of the client application.

Usage

From source file:org.mitre.oauth2.web.AuthenticationUtilities.java

/**
 * Makes sure the authentication contains the given scope, throws an exception otherwise
 * @param auth the authentication object to check
 * @param scope the scope to look for/*from   ww w. j  a  v a  2s .  com*/
 * @throws InsufficientScopeException if the authentication does not contain that scope
 */
public static void ensureOAuthScope(Authentication auth, String scope) {
    // if auth is OAuth, make sure we've got the right scope
    if (auth instanceof OAuth2Authentication) {
        OAuth2Authentication oAuth2Authentication = (OAuth2Authentication) auth;
        if (oAuth2Authentication.getOAuth2Request().getScope() == null
                || !oAuth2Authentication.getOAuth2Request().getScope().contains(scope)) {
            throw new InsufficientScopeException("Insufficient scope", ImmutableSet.of(scope));
        }
    }
}

From source file:com.epam.reportportal.auth.integration.github.GithubEndpoint.java

@RequestMapping(value = { "/sso/me/github/synchronize" }, method = RequestMethod.POST)
public OperationCompletionRS synchronize(OAuth2Authentication user) {
    Serializable upstreamToken = user.getOAuth2Request().getExtensions().get("upstream_token");
    BusinessRule.expect(upstreamToken, Objects::nonNull).verify(ErrorType.INCORRECT_AUTHENTICATION_TYPE,
            "Cannot synchronize GitHub User");
    this.replicator.synchronizeUser(upstreamToken.toString());
    return new OperationCompletionRS("User info successfully synchronized");
}

From source file:it.reply.orchestrator.dto.security.IndigoOAuth2Authentication.java

/**
 * Generate an {@link IndigoOAuth2Authentication}.
 * //from   w  w w. java2s . c  om
 * @param authentication
 *          the {@link OAuth2Authentication}.
 * @param token
 *          the {@link OAuth2AccessToken}.
 * @param userInfo
 *          the {@link UserInfo}. Can be null (i.e. the client is authenticating with its own
 *          credentials).
 */
public IndigoOAuth2Authentication(OAuth2Authentication authentication, OAuth2AccessToken token,
        UserInfo userInfo) {
    super(authentication.getOAuth2Request(), authentication.getUserAuthentication());
    this.setToken(token);
    this.setUserInfo(userInfo);
}

From source file:org.springsecurity.oauth2.oauth.OAuth2TokenEnhancer.java

@Override
public OAuth2AccessToken enhance(OAuth2AccessToken accessToken, OAuth2Authentication authentication) {
    DefaultOAuth2AccessToken result = new DefaultOAuth2AccessToken(accessToken);
    result.setAdditionalInformation(//w  w  w  .j a v  a2 s .c o  m
            Collections.singletonMap("client_id", (Object) authentication.getOAuth2Request().getClientId()));
    return result;
}

From source file:com.create.security.oauth2.provider.token.SpringCacheTokenStoreImpl.java

private synchronized void storeTokensByClientId(final OAuth2AccessToken token,
        final OAuth2Authentication authentication) {
    tokenRepository.storeTokensByClientId(authentication.getOAuth2Request().getClientId(), addToCollection(
            tokenRepository.findTokensByClientId(authentication.getOAuth2Request().getClientId()), token));
}

From source file:org.osiam.auth.token.TokenService.java

public AccessToken validateToken(final String token) {
    OAuth2Authentication auth = tokenStore.readAuthentication(token);
    OAuth2AccessToken accessToken = tokenStore.getAccessToken(auth);
    OAuth2Request authReq = auth.getOAuth2Request();

    AccessToken.Builder tokenBuilder = new AccessToken.Builder(token).setClientId(authReq.getClientId());

    if (auth.getUserAuthentication() != null && auth.getPrincipal() instanceof User) {
        User user = (User) auth.getPrincipal();
        tokenBuilder.setUserName(user.getUserName());
        tokenBuilder.setUserId(user.getId());
    }//w  w w . j  av a  2s.  c o m

    tokenBuilder.setExpiresAt(accessToken.getExpiration());
    for (String scopeString : authReq.getScope()) {
        tokenBuilder.addScope(new Scope(scopeString));
    }

    return tokenBuilder.build();
}

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

private ExpiringOAuth2RefreshToken createRefreshToken(OAuth2Authentication authentication) {
    if (!isSupportRefreshToken(authentication.getOAuth2Request())) {
        return null;
    }//w ww . ja  va2  s.c o  m
    int validitySeconds = getRefreshTokenValiditySeconds(authentication.getOAuth2Request());
    ExpiringOAuth2RefreshToken refreshToken = new DefaultExpiringOAuth2RefreshToken(
            UUID.randomUUID().toString(), new Date(System.currentTimeMillis() + (validitySeconds * 1000L)));
    return refreshToken;
}

From source file:com.create.security.oauth2.provider.token.SpringCacheTokenStoreImpl.java

private synchronized void storeTokensByClientIdAndUserName(final OAuth2AccessToken token,
        final OAuth2Authentication authentication, final String userName) {
    tokenRepository.storeTokensByClientIdAndUserName(authentication.getOAuth2Request().getClientId(), userName,
            addToCollection(tokenRepository.findTokensByClientIdAndUserName(
                    authentication.getOAuth2Request().getClientId(), userName), token));
}

From source file:com.companyname.controller.OAuth2AdminController.java

private Collection<OAuth2AccessToken> enhance(Collection<OAuth2AccessToken> tokens) {
    Collection<OAuth2AccessToken> result = new ArrayList<OAuth2AccessToken>();
    for (OAuth2AccessToken prototype : tokens) {
        DefaultOAuth2AccessToken token = new DefaultOAuth2AccessToken(prototype);
        OAuth2Authentication authentication = tokenStore.readAuthentication(token);
        if (authentication == null) {
            continue;
        }/*from  w ww.j  a va  2  s . co  m*/
        String clientId = authentication.getOAuth2Request().getClientId();
        if (clientId != null) {
            Map<String, Object> map = new HashMap<String, Object>(token.getAdditionalInformation());
            map.put("client_id", clientId);
            token.setAdditionalInformation(map);
            result.add(token);
        }
    }
    return result;
}