Search in sources :

Example 11 with OAuthJwtAccessToken

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

the class OAuthJwtAccessTokenValidatorTest method testValidateCertificateBinding.

@Test
public void testValidateCertificateBinding() throws Exception {
    final OAuthJwtAccessTokenValidator mock = Mockito.mock(OAuthJwtAccessTokenValidator.class, Mockito.CALLS_REAL_METHODS);
    // on CertificateEncodingException
    Mockito.doThrow(new CertificateEncodingException()).when(mock).getX509CertificateThumbprint(null);
    assertThrows(OAuthJwtAccessTokenException.class, () -> mock.validateCertificateBinding(null, (X509Certificate) null));
    // on CryptoException
    Mockito.doThrow(new CryptoException()).when(mock).getX509CertificateThumbprint(null);
    assertThrows(OAuthJwtAccessTokenException.class, () -> mock.validateCertificateBinding(null, (X509Certificate) null));
    // actual call
    OAuthJwtAccessTokenValidator validator = Mockito.mock(OAuthJwtAccessTokenValidator.class, Mockito.CALLS_REAL_METHODS);
    X509Certificate cert = this.readCert("jwt_ui.athenz.io.pem");
    Mockito.doReturn("zlkxyoX95le-Nv7OI0BxcjTOogvy9PGH-v_CBr_DsEk").when(validator).getX509CertificateThumbprint(cert);
    ArgumentCaptor<OAuthJwtAccessToken> tokenArg = ArgumentCaptor.forClass(OAuthJwtAccessToken.class);
    ArgumentCaptor<String> thumbprintArg = ArgumentCaptor.forClass(String.class);
    validator.validateCertificateBinding(null, cert);
    Mockito.verify(validator, Mockito.times(1)).validateCertificateBinding(tokenArg.capture(), thumbprintArg.capture());
    assertNull(tokenArg.getValue());
    assertEquals(thumbprintArg.getValue(), "zlkxyoX95le-Nv7OI0BxcjTOogvy9PGH-v_CBr_DsEk");
}
Also used : OAuthJwtAccessToken(com.yahoo.athenz.auth.oauth.token.OAuthJwtAccessToken) CertificateEncodingException(java.security.cert.CertificateEncodingException) CryptoException(com.yahoo.athenz.auth.util.CryptoException) X509Certificate(java.security.cert.X509Certificate) Test(org.testng.annotations.Test)

Aggregations

OAuthJwtAccessToken (com.yahoo.athenz.auth.oauth.token.OAuthJwtAccessToken)11 Test (org.testng.annotations.Test)9 ThrowingRunnable (org.testng.Assert.ThrowingRunnable)6 DefaultOAuthJwtAccessToken (com.yahoo.athenz.auth.oauth.token.DefaultOAuthJwtAccessToken)2 OAuthJwtAccessTokenException (com.yahoo.athenz.auth.oauth.token.OAuthJwtAccessTokenException)2 CryptoException (com.yahoo.athenz.auth.util.CryptoException)2 Claims (io.jsonwebtoken.Claims)2 CertificateEncodingException (java.security.cert.CertificateEncodingException)2 X509Certificate (java.security.cert.X509Certificate)2 CertificateIdentity (com.yahoo.athenz.auth.impl.CertificateIdentity)1 CertificateIdentityException (com.yahoo.athenz.auth.impl.CertificateIdentityException)1 SimplePrincipal (com.yahoo.athenz.auth.impl.SimplePrincipal)1 Jws (io.jsonwebtoken.Jws)1 JwtParser (io.jsonwebtoken.JwtParser)1 Field (java.lang.reflect.Field)1