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

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

Introduction

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

Prototype

String TOKEN_TYPE

To view the source code for org.springframework.security.oauth2.common OAuth2AccessToken TOKEN_TYPE.

Click Source Link

Document

The type of the token issued as described in <a href="https://tools.ietf.org/html/draft-ietf-oauth-v2-22#section-7.1">Section 7.1</a>.

Usage

From source file:com.acc.conv.Oauth2AccessTokenConverter.java

@Override
public void marshal(final Object source, final HierarchicalStreamWriter writerOrig,
        final MarshallingContext context) {
    final OAuth2AccessToken token = (OAuth2AccessToken) source;
    final ExtendedHierarchicalStreamWriter writer = (ExtendedHierarchicalStreamWriter) writerOrig
            .underlyingWriter();//from  w  w  w  .j  a va2s .  com

    writer.startNode(OAuth2AccessToken.ACCESS_TOKEN, String.class);
    writer.setValue(formattedValue(token.getValue()));
    writer.endNode();

    writer.startNode(OAuth2AccessToken.TOKEN_TYPE, String.class);
    writer.setValue(formattedValue(token.getTokenType()));
    writer.endNode();

    final OAuth2RefreshToken refreshToken = token.getRefreshToken();
    if (refreshToken != null) {
        writer.startNode(OAuth2AccessToken.REFRESH_TOKEN, String.class);
        writer.setValue(formattedValue(refreshToken.getValue()));
        writer.endNode();

    }
    final Date expiration = token.getExpiration();
    if (expiration != null) {
        final long now = System.currentTimeMillis();
        writer.startNode(OAuth2AccessToken.EXPIRES_IN, Integer.class);
        writer.setValue(String.valueOf((expiration.getTime() - now) / 1000));
        writer.endNode();
    }
    final Set<String> scope = token.getScope();
    if (scope != null && !scope.isEmpty()) {
        final StringBuffer scopes = new StringBuffer();
        for (final String s : scope) {
            Assert.hasLength(s, "Scopes cannot be null or empty. Got " + scope);
            scopes.append(s);
            scopes.append(' ');
        }

        writer.startNode(OAuth2AccessToken.SCOPE, String.class);
        writer.setValue(formattedValue(scopes.substring(0, scopes.length() - 1)));
        writer.endNode();
    }
    final Map<String, Object> additionalInformation = token.getAdditionalInformation();
    for (final String key : additionalInformation.keySet()) {
        writer.startNode(key, String.class);
        writer.setValue(formattedValue(String.valueOf(additionalInformation.get(key))));
        writer.endNode();
    }
}

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

@Override
public void serialize(OAuth2AccessToken token, JsonGenerator jgen, SerializerProvider provider)
        throws IOException, JsonGenerationException {
    jgen.writeStartObject();/*from   w  ww.  j  a  va 2s.c  o m*/
    jgen.writeStringField(OAuth2AccessToken.ACCESS_TOKEN, token.getValue());
    jgen.writeStringField(OAuth2AccessToken.TOKEN_TYPE, token.getTokenType());
    OAuth2RefreshToken refreshToken = token.getRefreshToken();
    if (refreshToken != null) {
        jgen.writeStringField(OAuth2AccessToken.REFRESH_TOKEN, refreshToken.getValue());
    }
    Date expiration = token.getExpiration();
    if (expiration != null) {
        long now = System.currentTimeMillis();
        jgen.writeNumberField(OAuth2AccessToken.EXPIRES_IN, (expiration.getTime() - now) / 1000);
    }
    Set<String> scope = token.getScope();
    if (scope != null && !scope.isEmpty()) {
        StringBuffer scopes = new StringBuffer();
        for (String s : scope) {
            Assert.hasLength(s, "Scopes cannot be null or empty. Got " + scope + "");
            scopes.append(s);
            scopes.append(" ");
        }
        jgen.writeStringField(OAuth2AccessToken.SCOPE, scopes.substring(0, scopes.length() - 1));
    }
    Map<String, Object> additionalInformation = token.getAdditionalInformation();
    for (String key : additionalInformation.keySet()) {
        jgen.writeObjectField(key, additionalInformation.get(key));
    }
    jgen.writeEndObject();
}

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 .  ja v a 2 s  .co  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;
}