Search in sources :

Example 1 with JsonParser

use of org.springframework.security.oauth2.common.util.JsonParser in project spring-security-oauth by spring-projects.

the class DefaultTokenServicesWithJwtTests method testRefreshedTokenHasIdThatMatchesAccessToken.

@Test
public void testRefreshedTokenHasIdThatMatchesAccessToken() throws Exception {
    JsonParser parser = JsonParserFactory.create();
    OAuth2Authentication authentication = createAuthentication();
    OAuth2AccessToken initialToken = getTokenServices().createAccessToken(authentication);
    ExpiringOAuth2RefreshToken expectedExpiringRefreshToken = (ExpiringOAuth2RefreshToken) initialToken.getRefreshToken();
    TokenRequest tokenRequest = new TokenRequest(Collections.singletonMap("client_id", "id"), "id", null, null);
    OAuth2AccessToken refreshedAccessToken = getTokenServices().refreshAccessToken(expectedExpiringRefreshToken.getValue(), tokenRequest);
    Map<String, ?> accessTokenInfo = parser.parseMap(JwtHelper.decode(refreshedAccessToken.getValue()).getClaims());
    Map<String, ?> refreshTokenInfo = parser.parseMap(JwtHelper.decode(refreshedAccessToken.getRefreshToken().getValue()).getClaims());
    assertEquals("Access token ID does not match refresh token ATI", accessTokenInfo.get(AccessTokenConverter.JTI), refreshTokenInfo.get(AccessTokenConverter.ATI));
    assertNotSame("Refresh token re-used", expectedExpiringRefreshToken.getValue(), refreshedAccessToken.getRefreshToken().getValue());
}
Also used : OAuth2AccessToken(org.springframework.security.oauth2.common.OAuth2AccessToken) OAuth2Authentication(org.springframework.security.oauth2.provider.OAuth2Authentication) TokenRequest(org.springframework.security.oauth2.provider.TokenRequest) JsonParser(org.springframework.security.oauth2.common.util.JsonParser) ExpiringOAuth2RefreshToken(org.springframework.security.oauth2.common.ExpiringOAuth2RefreshToken) Test(org.junit.Test)

Example 2 with JsonParser

use of org.springframework.security.oauth2.common.util.JsonParser in project spring-security-oauth by spring-projects.

the class JwtAccessTokenConverterTests method testExpiringRefreshTokenAdded.

@Test
public void testExpiringRefreshTokenAdded() throws Exception {
    OAuth2Authentication authentication = new OAuth2Authentication(createOAuth2Request("foo", Collections.singleton("read")), userAuthentication);
    DefaultOAuth2AccessToken original = new DefaultOAuth2AccessToken("FOO");
    original.setScope(authentication.getOAuth2Request().getScope());
    original.setRefreshToken(new DefaultExpiringOAuth2RefreshToken("BAR", new Date(0)));
    original.setExpiration(new Date());
    OAuth2AccessToken token = tokenEnhancer.enhance(original, authentication);
    assertNotNull(token.getValue());
    assertNotNull(token.getRefreshToken());
    JsonParser parser = JsonParserFactory.create();
    Map<String, Object> claims = parser.parseMap(JwtHelper.decode(token.getRefreshToken().getValue()).getClaims());
    assertEquals(Arrays.asList("read"), claims.get(AccessTokenConverter.SCOPE));
    assertEquals("FOO", claims.get(AccessTokenConverter.ATI));
    assertEquals("BAR", claims.get(AccessTokenConverter.JTI));
    assertEquals(0, claims.get(AccessTokenConverter.EXP));
    tokenEnhancer.afterPropertiesSet();
    assertTrue(tokenEnhancer.isRefreshToken(tokenEnhancer.extractAccessToken(token.getRefreshToken().getValue(), tokenEnhancer.decode(token.getRefreshToken().getValue()))));
}
Also used : DefaultOAuth2AccessToken(org.springframework.security.oauth2.common.DefaultOAuth2AccessToken) OAuth2AccessToken(org.springframework.security.oauth2.common.OAuth2AccessToken) OAuth2Authentication(org.springframework.security.oauth2.provider.OAuth2Authentication) DefaultExpiringOAuth2RefreshToken(org.springframework.security.oauth2.common.DefaultExpiringOAuth2RefreshToken) DefaultOAuth2AccessToken(org.springframework.security.oauth2.common.DefaultOAuth2AccessToken) Date(java.util.Date) JsonParser(org.springframework.security.oauth2.common.util.JsonParser) Test(org.junit.Test)

Example 3 with JsonParser

use of org.springframework.security.oauth2.common.util.JsonParser 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)

Example 4 with JsonParser

use of org.springframework.security.oauth2.common.util.JsonParser in project spring-security-oauth by spring-projects.

the class JwtAccessTokenConverterTests method testRefreshTokenAdded.

@Test
public void testRefreshTokenAdded() throws Exception {
    OAuth2Authentication authentication = new OAuth2Authentication(createOAuth2Request("foo", Collections.singleton("read")), userAuthentication);
    DefaultOAuth2AccessToken original = new DefaultOAuth2AccessToken("FOO");
    original.setScope(authentication.getOAuth2Request().getScope());
    original.setRefreshToken(new DefaultOAuth2RefreshToken("BAR"));
    original.setExpiration(new Date());
    OAuth2AccessToken token = tokenEnhancer.enhance(original, authentication);
    assertNotNull(token.getValue());
    assertNotNull(token.getRefreshToken());
    JsonParser parser = JsonParserFactory.create();
    Map<String, Object> claims = parser.parseMap(JwtHelper.decode(token.getRefreshToken().getValue()).getClaims());
    assertEquals(Arrays.asList("read"), claims.get(AccessTokenConverter.SCOPE));
    assertEquals("FOO", claims.get(AccessTokenConverter.ATI));
    assertEquals("BAR", claims.get(AccessTokenConverter.JTI));
    assertNull(claims.get(AccessTokenConverter.EXP));
    tokenEnhancer.afterPropertiesSet();
    assertTrue(tokenEnhancer.isRefreshToken(tokenEnhancer.extractAccessToken(token.getRefreshToken().getValue(), tokenEnhancer.decode(token.getRefreshToken().getValue()))));
}
Also used : DefaultOAuth2RefreshToken(org.springframework.security.oauth2.common.DefaultOAuth2RefreshToken) DefaultOAuth2AccessToken(org.springframework.security.oauth2.common.DefaultOAuth2AccessToken) OAuth2AccessToken(org.springframework.security.oauth2.common.OAuth2AccessToken) OAuth2Authentication(org.springframework.security.oauth2.provider.OAuth2Authentication) DefaultOAuth2AccessToken(org.springframework.security.oauth2.common.DefaultOAuth2AccessToken) Date(java.util.Date) JsonParser(org.springframework.security.oauth2.common.util.JsonParser) Test(org.junit.Test)

Example 5 with JsonParser

use of org.springframework.security.oauth2.common.util.JsonParser in project spring-security-oauth by spring-projects.

the class JwtAccessTokenConverterTests method testRefreshTokenAccessTokenIdWhenDoubleEnhanced.

@Test
public void testRefreshTokenAccessTokenIdWhenDoubleEnhanced() throws Exception {
    OAuth2Authentication authentication = new OAuth2Authentication(createOAuth2Request("foo", Collections.singleton("read")), userAuthentication);
    DefaultOAuth2AccessToken original = new DefaultOAuth2AccessToken("FOO");
    original.setScope(authentication.getOAuth2Request().getScope());
    original.setRefreshToken(new DefaultOAuth2RefreshToken("BAR"));
    OAuth2AccessToken token = tokenEnhancer.enhance(original, authentication);
    token = tokenEnhancer.enhance(token, authentication);
    assertNotNull(token.getValue());
    assertNotNull(token.getRefreshToken());
    JsonParser parser = JsonParserFactory.create();
    Map<String, Object> claims = parser.parseMap(JwtHelper.decode(token.getRefreshToken().getValue()).getClaims());
    assertEquals(Arrays.asList("read"), claims.get(AccessTokenConverter.SCOPE));
    assertEquals("FOO", claims.get(AccessTokenConverter.ATI));
    assertEquals("Wrong claims: " + claims, "BAR", claims.get(AccessTokenConverter.JTI));
}
Also used : DefaultOAuth2RefreshToken(org.springframework.security.oauth2.common.DefaultOAuth2RefreshToken) DefaultOAuth2AccessToken(org.springframework.security.oauth2.common.DefaultOAuth2AccessToken) OAuth2AccessToken(org.springframework.security.oauth2.common.OAuth2AccessToken) OAuth2Authentication(org.springframework.security.oauth2.provider.OAuth2Authentication) DefaultOAuth2AccessToken(org.springframework.security.oauth2.common.DefaultOAuth2AccessToken) JsonParser(org.springframework.security.oauth2.common.util.JsonParser) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)5 OAuth2AccessToken (org.springframework.security.oauth2.common.OAuth2AccessToken)5 JsonParser (org.springframework.security.oauth2.common.util.JsonParser)5 OAuth2Authentication (org.springframework.security.oauth2.provider.OAuth2Authentication)5 DefaultOAuth2AccessToken (org.springframework.security.oauth2.common.DefaultOAuth2AccessToken)3 Date (java.util.Date)2 DefaultOAuth2RefreshToken (org.springframework.security.oauth2.common.DefaultOAuth2RefreshToken)2 TokenRequest (org.springframework.security.oauth2.provider.TokenRequest)2 JsonParser (com.fasterxml.jackson.core.JsonParser)1 IOException (java.io.IOException)1 DefaultExpiringOAuth2RefreshToken (org.springframework.security.oauth2.common.DefaultExpiringOAuth2RefreshToken)1 ExpiringOAuth2RefreshToken (org.springframework.security.oauth2.common.ExpiringOAuth2RefreshToken)1 InvalidTokenException (org.springframework.security.oauth2.common.exceptions.InvalidTokenException)1