Example usage for org.springframework.security.jwt JwtHelper encode

List of usage examples for org.springframework.security.jwt JwtHelper encode

Introduction

In this page you can find the example usage for org.springframework.security.jwt JwtHelper encode.

Prototype

public static Jwt encode(CharSequence content, Signer signer) 

Source Link

Usage

From source file:org.springframework.security.jwt.filter.DefaultJwtTokenService.java

@Override
public String sign(Map<String, Object> claims) {
    try {/*  w  ww .  j  a  v  a2 s  .c  o m*/
        Jwt jwt = JwtHelper.encode(objectMapper.writeValueAsString(claims), signerVerifier);
        return jwt.getEncoded();
    } catch (JsonProcessingException e) {
        e.printStackTrace();
        // TODO
        return null;
    }
}

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

protected String createAccessTokenValue(OAuth2AccessToken accessToken, OAuth2Authentication authentication) {
    String content;//from  www . j  ava  2  s .  c  o  m
    try {
        content = objectMapper
                .writeValueAsString(tokenConverter.convertAccessToken(accessToken, authentication));
    } catch (Exception e) {
        throw new IllegalStateException("Cannot convert access token to JSON", e);
    }
    String token = JwtHelper.encode(content, signer).getEncoded();
    return token;
}

From source file:com.ge.predix.uaa.token.lib.TestTokenUtil.java

private DefaultOAuth2AccessToken createAccessToken(final String issuerId, final String userId,
        final String username, final String userEmail, final int validitySeconds,
        final Collection<GrantedAuthority> clientScopes, final Set<String> requestedScopes,
        final String clientId, final Set<String> resourceIds, final String grantType, final String refreshToken,
        final Map<String, String> additionalAuthorizationAttributes, final Set<String> responseTypes,
        final String revocableHashSignature, final long issuedAtMillis, final String zoneId) {

    String tokenId = UUID.randomUUID().toString();
    DefaultOAuth2AccessToken accessToken = new DefaultOAuth2AccessToken(tokenId);
    if (validitySeconds > 0) {
        accessToken.setExpiration(new Date(issuedAtMillis + (validitySeconds * 1000L)));
    }/*from  ww  w.  j  a  va2 s .  c o  m*/
    accessToken.setRefreshToken(refreshToken == null ? null : new DefaultOAuth2RefreshToken(refreshToken));

    if (null == requestedScopes || requestedScopes.size() == 0) {
        // logger.debug("No scopes were granted");
        throw new InvalidTokenException("No scopes were granted");
    }

    accessToken.setScope(requestedScopes);

    Map<String, Object> info = new HashMap<String, Object>();
    info.put(JTI, accessToken.getValue());
    if (null != additionalAuthorizationAttributes) {
        info.put(ADDITIONAL_AZ_ATTR, additionalAuthorizationAttributes);
    }
    accessToken.setAdditionalInformation(info);

    String content;
    try {
        content = JsonUtils.writeValueAsString(createJWTAccessToken(accessToken, issuerId, userId, username,
                userEmail, clientScopes, requestedScopes, clientId, resourceIds, grantType, refreshToken,
                revocableHashSignature, issuedAtMillis, zoneId));
    } catch (JsonUtils.JsonUtilException e) {
        throw new IllegalStateException("Cannot convert access token to JSON", e);
    }
    String token = JwtHelper.encode(content, this.signer).getEncoded();

    // This setter copies the value and returns. Don't change.
    accessToken.setValue(token);

    return accessToken;

}

From source file:org.cloudfoundry.identity.uaa.oauth.token.UaaTokenServices.java

private OAuth2AccessToken createAccessToken(String userId, String username, String userEmail,
        int validitySeconds, Collection<GrantedAuthority> clientScopes, Set<String> requestedScopes,
        String clientId, Set<String> resourceIds, String grantType, String refreshToken,
        Map<String, String> additionalAuthorizationAttributes, Set<String> responseTypes)
        throws AuthenticationException {
    String tokenId = UUID.randomUUID().toString();
    OpenIdToken accessToken = new OpenIdToken(tokenId);
    if (validitySeconds > 0) {
        accessToken.setExpiration(new Date(System.currentTimeMillis() + (validitySeconds * 1000L)));
    }//from   www  .jav a  2s  . c o m
    accessToken.setRefreshToken(refreshToken == null ? null : new DefaultOAuth2RefreshToken(refreshToken));

    if (null == requestedScopes || requestedScopes.size() == 0) {
        logger.debug("No scopes were granted");
        throw new InvalidTokenException("No scopes were granted");
    }

    accessToken.setScope(requestedScopes);

    Map<String, Object> info = new HashMap<String, Object>();
    info.put(JTI, accessToken.getValue());
    if (null != additionalAuthorizationAttributes) {
        info.put(ADDITIONAL_AZ_ATTR, additionalAuthorizationAttributes);
    }
    accessToken.setAdditionalInformation(info);

    String content;
    try {
        content = mapper.writeValueAsString(createJWTAccessToken(accessToken, userId, username, userEmail,
                clientScopes, requestedScopes, clientId, resourceIds, grantType, refreshToken));
    } catch (Exception e) {
        throw new IllegalStateException("Cannot convert access token to JSON", e);
    }
    String token = JwtHelper.encode(content, signerProvider.getSigner()).getEncoded();

    // This setter copies the value and returns. Don't change.
    accessToken.setValue(token);
    populateIdToken(accessToken, requestedScopes, responseTypes);
    publish(new TokenIssuedEvent(accessToken, SecurityContextHolder.getContext().getAuthentication()));

    return accessToken;
}

From source file:org.cloudfoundry.identity.uaa.oauth.token.UaaTokenServices.java

private ExpiringOAuth2RefreshToken createRefreshToken(OAuth2Authentication authentication) {

    String grantType = authentication.getOAuth2Request().getRequestParameters().get("grant_type");
    if (!isRefreshTokenSupported(grantType)) {
        return null;
    }// w w  w  .  j a v  a  2  s  .co  m

    Map<String, String> additionalAuthorizationAttributes = getAdditionalAuthorizationAttributes(
            authentication.getOAuth2Request().getRequestParameters().get("authorities"));

    int validitySeconds = getRefreshTokenValiditySeconds(authentication.getOAuth2Request());
    ExpiringOAuth2RefreshToken token = new DefaultExpiringOAuth2RefreshToken(UUID.randomUUID().toString(),
            new Date(System.currentTimeMillis() + (validitySeconds * 1000L)));

    String userId = getUserId(authentication);
    UaaUser user = userDatabase.retrieveUserById(userId);

    String content;
    try {
        content = mapper.writeValueAsString(
                createJWTRefreshToken(token, user, authentication.getOAuth2Request().getScope(),
                        authentication.getOAuth2Request().getClientId(), grantType,
                        additionalAuthorizationAttributes, authentication.getOAuth2Request().getResourceIds()));
    } catch (Exception e) {
        throw new IllegalStateException("Cannot convert access token to JSON", e);
    }
    String jwtToken = JwtHelper.encode(content, signerProvider.getSigner()).getEncoded();

    ExpiringOAuth2RefreshToken refreshToken = new DefaultExpiringOAuth2RefreshToken(jwtToken,
            token.getExpiration());

    return refreshToken;
}

From source file:org.springframework.security.oauth2.provider.token.JwtTokenEnhancer.java

protected String encode(OAuth2AccessToken accessToken, OAuth2Authentication authentication) {
    String content;//w w  w  .j a  v  a  2s . co  m
    try {
        content = objectMapper
                .writeValueAsString(tokenConverter.convertAccessToken(accessToken, authentication));
    } catch (Exception e) {
        throw new IllegalStateException("Cannot convert access token to JSON", e);
    }
    String token = JwtHelper.encode(content, signer).getEncoded();
    return token;
}