Example usage for org.springframework.security.oauth2.common DefaultOAuth2AccessToken setAdditionalInformation

List of usage examples for org.springframework.security.oauth2.common DefaultOAuth2AccessToken setAdditionalInformation

Introduction

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

Prototype

public void setAdditionalInformation(Map<String, Object> additionalInformation) 

Source Link

Document

Additional information that token granters would like to add to the token, e.g.

Usage

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

@Override
public OAuth2AccessToken enhance(OAuth2AccessToken accessToken, OAuth2Authentication authentication) {
    DefaultOAuth2AccessToken result = new DefaultOAuth2AccessToken(accessToken);
    result.setAdditionalInformation(
            Collections.singletonMap("client_id", (Object) authentication.getOAuth2Request().getClientId()));
    return result;
}

From source file:ch.hortis.mongodb.training.blog.oauth.BlogTokenEnhancer.java

@Override
public OAuth2AccessToken enhance(OAuth2AccessToken accessToken, OAuth2Authentication authentication) {
    DefaultOAuth2AccessToken result = new DefaultOAuth2AccessToken(accessToken);
    result.setAdditionalInformation(Collections.singletonMap("client_id",
            (Object) authentication.getAuthorizationRequest().getClientId()));
    return result;
}

From source file:org.cloudfoundry.identity.uaa.oauth.TokenAdminEndpointsTests.java

@Test
public void testRevokeTokenForUserWithTokenId() throws Exception {
    DefaultOAuth2AccessToken token = new DefaultOAuth2AccessToken("FOO");
    token.setAdditionalInformation(Collections.<String, Object>singletonMap(JwtTokenEnhancer.TOKEN_ID, "BAR"));
    Mockito.when(tokenServices.findTokensByUserName("marissa"))
            .thenReturn(Collections.<OAuth2AccessToken>singleton(token));
    Mockito.when(tokenServices.revokeToken("FOO")).thenReturn(true);
    SimpleMessage result = endpoints.revokeUserToken("marissa", "BAR",
            new TestingAuthenticationToken("marissa", ""), false);
    assertEquals("ok", result.getStatus());
}

From source file:am.ik.categolj2.domain.service.token.Categolj2TokenEnhancer.java

@Override
public OAuth2AccessToken enhance(OAuth2AccessToken accessToken, OAuth2Authentication authentication) {
    if (authentication == null || authentication.getPrincipal() == null) {
        return accessToken;
    }/*  w  w w.j a v a 2  s . c  o m*/
    Categolj2UserDetails userDetails = (Categolj2UserDetails) authentication.getPrincipal();
    DefaultOAuth2AccessToken defaultOAuth2AccessToken = (DefaultOAuth2AccessToken) accessToken;
    Map<String, Object> additionalInformation = new HashMap<>();
    User user = userDetails.getUser();
    user.setPassword(null);
    additionalInformation.put("user", user);
    defaultOAuth2AccessToken.setAdditionalInformation(additionalInformation);
    return defaultOAuth2AccessToken;
}

From source file:org.springframework.security.oauth2.common.OAuth2AccessTokenJackson2Deserializer.java

@Override
public OAuth2AccessToken deserialize(JsonParser jp, DeserializationContext ctxt)
        throws IOException, JsonProcessingException {

    String tokenValue = null;//  ww w . j  ava 2 s  .c o m
    String tokenType = null;
    String refreshToken = null;
    Long expiresIn = null;
    Set<String> scope = null;
    Map<String, Object> additionalInformation = new LinkedHashMap<String, Object>();

    // TODO What should occur if a parameter exists twice
    while (jp.nextToken() != JsonToken.END_OBJECT) {
        String name = jp.getCurrentName();
        jp.nextToken();
        if (OAuth2AccessToken.ACCESS_TOKEN.equals(name)) {
            tokenValue = jp.getText();
        } else if (OAuth2AccessToken.TOKEN_TYPE.equals(name)) {
            tokenType = jp.getText();
        } else if (OAuth2AccessToken.REFRESH_TOKEN.equals(name)) {
            refreshToken = jp.getText();
        } else if (OAuth2AccessToken.EXPIRES_IN.equals(name)) {
            try {
                expiresIn = jp.getLongValue();
            } catch (JsonParseException e) {
                expiresIn = Long.valueOf(jp.getText());
            }
        } else if (OAuth2AccessToken.SCOPE.equals(name)) {
            String text = jp.getText();
            scope = OAuth2Utils.parseParameterList(text);
        } else {
            additionalInformation.put(name, jp.readValueAs(Object.class));
        }
    }

    // TODO What should occur if a required parameter (tokenValue or tokenType) is missing?

    DefaultOAuth2AccessToken accessToken = new DefaultOAuth2AccessToken(tokenValue);
    accessToken.setTokenType(tokenType);
    if (expiresIn != null) {
        accessToken.setExpiration(new Date(System.currentTimeMillis() + (expiresIn * 1000)));
    }
    if (refreshToken != null) {
        accessToken.setRefreshToken(new DefaultOAuth2RefreshToken(refreshToken));
    }
    accessToken.setScope(scope);
    accessToken.setAdditionalInformation(additionalInformation);

    return accessToken;
}

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;
        }//  ww w. j  av  a  2s  .c  om
        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;
}

From source file:no.imr.common.security.jwt.DefaultAccessTokenConverter.java

public OAuth2AccessToken extractAccessToken(String value, Map<String, ?> map) {
    DefaultOAuth2AccessToken token = new DefaultOAuth2AccessToken(value);
    Map<String, Object> info = new HashMap<String, Object>(map);
    info.remove(EXP);/*from w  w w . j a  v  a 2  s .c o m*/
    info.remove(AUD);
    info.remove(CLIENT_ID);
    info.remove(SCOPE);
    if (map.containsKey(EXP)) {
        token.setExpiration(new Date((Long) map.get(EXP) * 1000L));
    }
    if (map.containsKey(JTI)) {
        info.put(JTI, map.get(JTI));
    }
    @SuppressWarnings("unchecked")
    Collection<String> scope = (Collection<String>) map.get(SCOPE);
    if (scope != null) {
        token.setScope(new HashSet<String>(scope));
    }
    token.setAdditionalInformation(info);
    return token;
}

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

private void commonMocks() {
    //Collection of Oauthtokens to return when asked for in the controller
    Collection<OAuth2AccessToken> tokens = new ArrayList<OAuth2AccessToken>();
    DefaultOAuth2AccessToken accessToken = new DefaultOAuth2AccessToken("MOCK_TOKEN_VALUE");
    //Add some scopes
    Set<String> scope = new HashSet<String>();
    scope.add("MOCK_SCOPE_ITEM_1");
    scope.add("MOCK_SCOPE_ITEM_2");

    //Placeholder for additionalInformation
    Map<String, Object> addInfo = new HashMap<String, Object>();
    addInfo.put("authorization_grant_id", "MOCK_GRANT_ID");

    //Set all relevant things on this accessToken
    accessToken.setScope(scope);/* w  ww  . j ava 2s  .  c o m*/
    accessToken.setExpiration(new DateTime().plusDays(1).toDate());
    accessToken.setAdditionalInformation(addInfo);

    tokens.add(accessToken);

    //Mock encryptedConsumerTokenService interactions
    Mockito.when(encryptedConsumerTokenService.findTokensByUserName(anyString())).thenReturn(tokens);
    Mockito.when(encryptedConsumerTokenService.getClientId("MOCK_TOKEN_VALUE"))
            .thenReturn("INSTALLED_CLIENT_ID");

    //CertifiedClients to return from clientDetailsServices mock
    List<CertifiedClient> clients = new ArrayList<CertifiedClient>();
    CertifiedClient installedClient = new CertifiedClient();
    CertifiedClient availableClient = new CertifiedClient();
    installedClient.setName("INSTALLED_CLIENT_NAME");
    installedClient.setClientId("INSTALLED_CLIENT_ID");
    installedClient.setOrganization("INSTALLED_CLIENT_ORGANIZATION");
    installedClient.setDescription("A description");

    availableClient.setName("AVAILABLE_CLIENT_NAME");
    availableClient.setClientId("AVAILABLE_CLIENT_ID");
    availableClient.setOrganization("AVAILABLE_CLIENT_ORGANIZATION");
    availableClient.setDescription("A description");
    availableClient.setClientUrl("http://somewhere");

    clients.add(installedClient);
    clients.add(availableClient);

    //Mock clientDetailsService interactions
    Mockito.when(clientDetailsService.findAllClients()).thenReturn(clients);
    Mockito.when(clientDetailsService.loadClientByClientId("INSTALLED_CLIENT_ID")).thenReturn(installedClient);

    //Permission to return from permissionServices mock
    Permission mockPermission = new Permission();
    mockPermission.setDescription("MOCK_ROLE_DESCRIPTION");

    //Mock scopeservices
    Mockito.when(permissionServices.getPermissionByName(anyString())).thenReturn(mockPermission);
}

From source file:org.cloudfoundry.identity.uaa.oauth.TokenAdminEndpoints.java

private Collection<OAuth2AccessToken> enhance(Collection<OAuth2AccessToken> tokens) {
    Collection<OAuth2AccessToken> result = new ArrayList<OAuth2AccessToken>();
    for (OAuth2AccessToken prototype : tokens) {
        DefaultOAuth2AccessToken token = new DefaultOAuth2AccessToken(prototype);
        Map<String, Object> map = new HashMap<String, Object>(token.getAdditionalInformation());
        if (!map.containsKey(JwtTokenEnhancer.TOKEN_ID)) {
            // The token doesn't have an ID in the token service, but we need one for the endpoint, so add one here
            map.put(JwtTokenEnhancer.TOKEN_ID, encoder.encode(token.getValue()));
        }/*  w w  w .j a  v a  2s. c  om*/
        try {
            String clientId = tokenServices.getClientId(token.getValue());
            if (clientId != null) {
                map.put("client_id", clientId);
            }
        } catch (InvalidTokenException e) {
            // Ignore defensively in case of bugs in token services
        }
        token.setAdditionalInformation(map);
        result.add(token);
    }
    return result;
}