use of com.yahoo.athenz.auth.oauth.token.OAuthJwtAccessToken in project athenz by yahoo.
the class DefaultOAuthJwtAccessTokenValidatorTest method testValidateVerifyScopes.
@Test
public void testValidateVerifyScopes() {
final DefaultOAuthJwtAccessTokenValidator validator = this.baseValidator;
final OAuthJwtAccessToken mock = Mockito.spy(baseJwt);
Mockito.doReturn(this.trustedIssuer).when(mock).getIssuer();
Mockito.doReturn(new ArrayList<>(this.requiredAudiences)).when(mock).getAudiences();
Mockito.doReturn(1L).when(mock).getExpiration();
// null JWT issuer
Mockito.doReturn(null).when(mock).getScope();
assertThrowable.accept(new ThrowingRunnable() {
public void run() throws Throwable {
validator.validate(mock);
}
}, "required scope not found: got=null");
// empty
Mockito.doReturn("").when(mock).getScope();
assertThrowable.accept(new ThrowingRunnable() {
public void run() throws Throwable {
validator.validate(mock);
}
}, "required scope not found: got=");
// not match
Mockito.doReturn("scope_1 unknown_scope").when(mock).getScope();
assertThrowable.accept(new ThrowingRunnable() {
public void run() throws Throwable {
validator.validate(mock);
}
}, "required scope not found: got=scope_1 unknown_scope");
// match
Mockito.doReturn("scope_3 scope_2 scope_1").when(mock).getScope();
assertDoesNotThrow.accept(new ThrowingRunnable() {
public void run() throws Throwable {
validator.validate(mock);
}
});
}
use of com.yahoo.athenz.auth.oauth.token.OAuthJwtAccessToken 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;
}
use of com.yahoo.athenz.auth.oauth.token.OAuthJwtAccessToken in project athenz by yahoo.
the class DefaultOAuthJwtAccessTokenParserFactoryTest method testCreate.
@Test
public void testCreate() throws OAuthJwtAccessTokenException {
OAuthJwtAccessTokenParser parser = null;
DefaultOAuthJwtAccessTokenParserFactory factory = new DefaultOAuthJwtAccessTokenParserFactory();
// check internal
assertThrows(IllegalArgumentException.class, () -> factory.create(null));
// check default
parser = factory.create(baseKeyStore);
assertNotNull(parser);
// check custom property
String jwtString = "eyJraWQiOiJjOTk4NmVlMy03YjJhLTRkMjAtYjg2YS0wODM5ODU2ZjI1NDEiLCJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiJ9.eyJpc3MiOiJEZWZhdWx0T0F1dGhKd3RBY2Nlc3NUb2tlblBhcnNlckZhY3RvcnlUZXN0In0.UalqjyBTDNnEqA0NaOeOhTn_H96vFo9TsCTq58r1YT2p5Hf3xjZLn25puWjcoGZOp1N2xBrvKwmcysHtib5Gq70ulBV7zQXHVzoLB56Ey2LDJJ3QH5sejPCXuapu2i21hSp4PEVvqndULdMipcFYngN97uISrwj-cki8XVgEQDs3OiuHNpyLPYHbCOrbncU9cy29K7l1wYS9gG_OYUB_gy0vdQDhbdbtWs6iwYWQZ3UWJcLp_j1hZyeRhmrSeAmHEBUa8mZs8EuySd3cxUYtV5qje_GPQ47BP2sFWSM6an4Gw6llSWp395O9zJPHRwcqSeIop_wV9Lb7C7v1pRDQDGDsSXH4UbxvEw-Yb0fg4jos3z2cLtk8NR4qzLCVnzHt1uD9QpzB3dXNB22nn8coZ0ay78lMahje6xw162pyjWZUD2YrRpPxUgngdsVJEN-DBQzKQyieHWTWMEgZ2uUsXtPKTKYcW9XfHSXE7gEQwNP9Qz03oP4bz9oP1aLpeQIMQ790NsMfSOv3yRpH5RswZ5rd9NJZgH-n57AlS8Oqz1-wIwTehGdnRlEveU0xoVfuQOonooPHACXA0DR2pV-zo6VT4BOLUMmhU8-TDvP05VXC-maNljjjtL4H7pX6ob9eLTAbj96RqHOkey89WwgKlS1a6LnoMRxcuVJPPmcerdY";
System.setProperty("athenz.auth.oauth.jwt.parser.jwks_url", this.classLoader.getResource("jwt_jwks.json").toString());
parser = factory.create(baseKeyStore);
System.clearProperty("athenz.auth.oauth.jwt.parser.jwks_url");
OAuthJwtAccessToken token = parser.parse(jwtString);
assertEquals(token.getIssuer(), "DefaultOAuthJwtAccessTokenParserFactoryTest");
}
use of com.yahoo.athenz.auth.oauth.token.OAuthJwtAccessToken in project athenz by yahoo.
the class DefaultOAuthJwtAccessTokenValidatorTest method testValidateVerifyIssuer.
@Test
public void testValidateVerifyIssuer() {
final DefaultOAuthJwtAccessTokenValidator validator = this.baseValidator;
final OAuthJwtAccessToken mock = Mockito.spy(baseJwt);
Mockito.doReturn(new ArrayList<>(this.requiredAudiences)).when(mock).getAudiences();
Mockito.doReturn(new ArrayList<>(this.requiredScopes)).when(mock).getScopes();
Mockito.doReturn(1L).when(mock).getExpiration();
// null JWT issuer
Mockito.doReturn(null).when(mock).getIssuer();
assertThrowable.accept(new ThrowingRunnable() {
public void run() throws Throwable {
validator.validate(mock);
}
}, "iss not trusted: got=null");
// empty
Mockito.doReturn("").when(mock).getIssuer();
assertThrowable.accept(new ThrowingRunnable() {
public void run() throws Throwable {
validator.validate(mock);
}
}, "iss not trusted: got=");
// not match
Mockito.doReturn("untrusty_issuer").when(mock).getIssuer();
assertThrowable.accept(new ThrowingRunnable() {
public void run() throws Throwable {
validator.validate(mock);
}
}, "iss not trusted: got=untrusty_issuer");
// match
Mockito.doReturn("trustedIssuer").when(mock).getIssuer();
assertDoesNotThrow.accept(new ThrowingRunnable() {
public void run() throws Throwable {
validator.validate(mock);
}
});
}
use of com.yahoo.athenz.auth.oauth.token.OAuthJwtAccessToken in project athenz by yahoo.
the class DefaultOAuthJwtAccessTokenValidatorTest method testValidateExpiration.
@Test
public void testValidateExpiration() {
final DefaultOAuthJwtAccessTokenValidator validator = new DefaultOAuthJwtAccessTokenValidator(this.trustedIssuer, this.requiredAudiences, this.requiredScopes, this.authorizedClientIds);
final OAuthJwtAccessToken mock = Mockito.spy(baseJwt);
Mockito.doReturn("trustedIssuer").when(mock).getIssuer();
Mockito.doReturn(Arrays.asList("aud_1", "aud_2")).when(mock).getAudiences();
Mockito.doReturn(Arrays.asList("scope_1", "scope_2")).when(mock).getScopes();
// zero exp
Mockito.doReturn(0L).when(mock).getExpiration();
assertThrowable.accept(new ThrowingRunnable() {
public void run() throws Throwable {
validator.validate(mock);
}
}, "exp is empty");
// -ve exp
Mockito.doReturn(-1L).when(mock).getExpiration();
assertThrowable.accept(new ThrowingRunnable() {
public void run() throws Throwable {
validator.validate(mock);
}
}, "exp is empty");
// +ve exp
Mockito.doReturn(1L).when(mock).getExpiration();
assertDoesNotThrow.accept(new ThrowingRunnable() {
public void run() throws Throwable {
validator.validate(mock);
}
});
}
Aggregations