Search in sources :

Example 1 with DefaultOAuthJwtAccessToken

use of com.yahoo.athenz.auth.oauth.token.DefaultOAuthJwtAccessToken in project athenz by yahoo.

the class DefaultOAuthJwtAccessTokenParserTest method testParse.

@Test
@SuppressWarnings("rawtypes")
public void testParse() throws Exception {
    // mock internal parser
    DefaultOAuthJwtAccessTokenParser parser = new DefaultOAuthJwtAccessTokenParser(baseKeyStore, this.classLoader.getResource("jwt_jwks.json").toString());
    JwtParser jwtParserMock = Mockito.mock(JwtParser.class);
    Field f = parser.getClass().getDeclaredField("parser");
    f.setAccessible(true);
    f.set(parser, jwtParserMock);
    // parse error
    Mockito.when(jwtParserMock.parseClaimsJws(null)).thenThrow(new NullPointerException());
    assertThrows(OAuthJwtAccessTokenException.class, () -> parser.parse(null));
    // parse success
    String jwtString = "dummy-jwt-string";
    Jws<Claims> jws = new Jws<Claims>() {

        public JwsHeader getHeader() {
            return null;
        }

        public Claims getBody() {
            return null;
        }

        @Override
        public String getSignature() {
            return "dummy-jwt-signature";
        }
    };
    Mockito.when(jwtParserMock.parseClaimsJws(jwtString)).thenReturn(jws);
    OAuthJwtAccessToken token = parser.parse(jwtString);
    assertNotNull(token);
    assertTrue(token instanceof DefaultOAuthJwtAccessToken);
    assertEquals(token.getSignature(), "dummy-jwt-signature");
}
Also used : JwtParser(io.jsonwebtoken.JwtParser) Field(java.lang.reflect.Field) Claims(io.jsonwebtoken.Claims) DefaultOAuthJwtAccessToken(com.yahoo.athenz.auth.oauth.token.DefaultOAuthJwtAccessToken) OAuthJwtAccessToken(com.yahoo.athenz.auth.oauth.token.OAuthJwtAccessToken) DefaultOAuthJwtAccessToken(com.yahoo.athenz.auth.oauth.token.DefaultOAuthJwtAccessToken) Jws(io.jsonwebtoken.Jws) Test(org.testng.annotations.Test)

Example 2 with DefaultOAuthJwtAccessToken

use of com.yahoo.athenz.auth.oauth.token.DefaultOAuthJwtAccessToken in project athenz by yahoo.

the class DefaultOAuthJwtAccessTokenParser method parse.

@Override
public OAuthJwtAccessToken parse(String jwtString) throws OAuthJwtAccessTokenException {
    OAuthJwtAccessToken accessToken = null;
    try {
        Jws<Claims> jws = this.parser.parseClaimsJws(jwtString);
        accessToken = new DefaultOAuthJwtAccessToken(jws);
    } catch (Exception ex) {
        throw new OAuthJwtAccessTokenException(ex);
    }
    return accessToken;
}
Also used : Claims(io.jsonwebtoken.Claims) DefaultOAuthJwtAccessToken(com.yahoo.athenz.auth.oauth.token.DefaultOAuthJwtAccessToken) OAuthJwtAccessToken(com.yahoo.athenz.auth.oauth.token.OAuthJwtAccessToken) DefaultOAuthJwtAccessToken(com.yahoo.athenz.auth.oauth.token.DefaultOAuthJwtAccessToken) OAuthJwtAccessTokenException(com.yahoo.athenz.auth.oauth.token.OAuthJwtAccessTokenException) OAuthJwtAccessTokenException(com.yahoo.athenz.auth.oauth.token.OAuthJwtAccessTokenException)

Aggregations

DefaultOAuthJwtAccessToken (com.yahoo.athenz.auth.oauth.token.DefaultOAuthJwtAccessToken)2 OAuthJwtAccessToken (com.yahoo.athenz.auth.oauth.token.OAuthJwtAccessToken)2 Claims (io.jsonwebtoken.Claims)2 OAuthJwtAccessTokenException (com.yahoo.athenz.auth.oauth.token.OAuthJwtAccessTokenException)1 Jws (io.jsonwebtoken.Jws)1 JwtParser (io.jsonwebtoken.JwtParser)1 Field (java.lang.reflect.Field)1 Test (org.testng.annotations.Test)1