Search in sources :

Example 21 with InvalidTokenException

use of org.springframework.security.oauth2.common.exceptions.InvalidTokenException in project spring-security-oauth by spring-projects.

the class JwtHeaderConverter method convert.

/**
	 * Converts the supplied JSON Web Token to a <code>Map</code> of JWT Header Parameters.
	 *
	 * @param token the JSON Web Token
	 * @return a <code>Map</code> of JWT Header Parameters
	 * @throws JwkException if the JWT is invalid
	 */
@Override
public Map<String, String> convert(String token) {
    Map<String, String> headers;
    int headerEndIndex = token.indexOf('.');
    if (headerEndIndex == -1) {
        throw new InvalidTokenException("Invalid JWT. Missing JOSE Header.");
    }
    byte[] decodedHeader = Codecs.b64UrlDecode(token.substring(0, headerEndIndex));
    JsonParser parser = null;
    try {
        parser = this.factory.createParser(decodedHeader);
        headers = new HashMap<String, String>();
        if (parser.nextToken() == JsonToken.START_OBJECT) {
            while (parser.nextToken() == JsonToken.FIELD_NAME) {
                String headerName = parser.getCurrentName();
                parser.nextToken();
                String headerValue = parser.getValueAsString();
                headers.put(headerName, headerValue);
            }
        }
    } catch (IOException ex) {
        throw new InvalidTokenException("An I/O error occurred while reading the JWT: " + ex.getMessage(), ex);
    } finally {
        try {
            if (parser != null)
                parser.close();
        } catch (IOException ex) {
        }
    }
    return headers;
}
Also used : InvalidTokenException(org.springframework.security.oauth2.common.exceptions.InvalidTokenException) IOException(java.io.IOException) JsonParser(com.fasterxml.jackson.core.JsonParser)

Aggregations

InvalidTokenException (org.springframework.security.oauth2.common.exceptions.InvalidTokenException)21 OAuth2Authentication (org.springframework.security.oauth2.provider.OAuth2Authentication)8 OAuth2AccessToken (org.springframework.security.oauth2.common.OAuth2AccessToken)6 Date (java.util.Date)4 Test (org.junit.Test)4 OrcidOauth2TokenDetail (org.orcid.persistence.jpa.entities.OrcidOauth2TokenDetail)4 DBUnitTest (org.orcid.test.DBUnitTest)4 DefaultOAuth2AccessToken (org.springframework.security.oauth2.common.DefaultOAuth2AccessToken)4 ClientDetailsEntity (org.orcid.persistence.jpa.entities.ClientDetailsEntity)3 OAuth2AccessDeniedException (org.springframework.security.oauth2.client.resource.OAuth2AccessDeniedException)3 InvalidScopeException (org.springframework.security.oauth2.common.exceptions.InvalidScopeException)3 OAuth2Exception (org.springframework.security.oauth2.common.exceptions.OAuth2Exception)3 HashSet (java.util.HashSet)2 NoResultException (javax.persistence.NoResultException)2 OrcidOAuth2Authentication (org.orcid.core.oauth.OrcidOAuth2Authentication)2 ProfileEntity (org.orcid.persistence.jpa.entities.ProfileEntity)2 Authentication (org.springframework.security.core.Authentication)2 Jwt (org.springframework.security.jwt.Jwt)2 JsonParser (com.fasterxml.jackson.core.JsonParser)1 MultivaluedMapImpl (com.sun.jersey.core.util.MultivaluedMapImpl)1