Search in sources :

Example 71 with MockTicketGrantingTicket

use of org.apereo.cas.mock.MockTicketGrantingTicket in project cas by apereo.

the class BaseOAuth20AuthenticatorTests method getAccessToken.

protected static OAuth20AccessToken getAccessToken() {
    val tgt = new MockTicketGrantingTicket("casuser");
    val service = RegisteredServiceTestUtils.getService();
    val accessToken = mock(OAuth20AccessToken.class);
    when(accessToken.getId()).thenReturn("ABCD");
    when(accessToken.getTicketGrantingTicket()).thenReturn(tgt);
    when(accessToken.getAuthentication()).thenReturn(tgt.getAuthentication());
    when(accessToken.getService()).thenReturn(service);
    when(accessToken.getExpirationPolicy()).thenReturn(NeverExpiresExpirationPolicy.INSTANCE);
    return accessToken;
}
Also used : lombok.val(lombok.val) MockTicketGrantingTicket(org.apereo.cas.mock.MockTicketGrantingTicket)

Example 72 with MockTicketGrantingTicket

use of org.apereo.cas.mock.MockTicketGrantingTicket in project cas by apereo.

the class OAuth20ProofKeyCodeExchangeAuthenticatorTests method verifyAuthenticationPlainWithSecretTransmittedByBasicAuthn.

@Test
public void verifyAuthenticationPlainWithSecretTransmittedByBasicAuthn() throws Exception {
    val credentials = new UsernamePasswordCredentials("client", "secret");
    val request = new MockHttpServletRequest();
    ticketRegistry.addTicket(new OAuth20DefaultCode("CODE-1234567890", RegisteredServiceTestUtils.getService(), RegisteredServiceTestUtils.getAuthentication(), new HardTimeoutExpirationPolicy(10), new MockTicketGrantingTicket("casuser"), new ArrayList<>(), "ABCD123", "plain", "clientid12345", new HashMap<>(), OAuth20ResponseTypes.CODE, OAuth20GrantTypes.AUTHORIZATION_CODE));
    request.addHeader("Authorization", "Basic " + EncodingUtils.encodeBase64("client:secret"));
    request.addParameter(OAuth20Constants.CODE_VERIFIER, "ABCD123");
    request.addParameter(OAuth20Constants.CODE, "CODE-1234567890");
    val ctx = new JEEContext(request, new MockHttpServletResponse());
    authenticator.validate(credentials, ctx, JEESessionStore.INSTANCE);
    assertNotNull(credentials.getUserProfile());
    assertEquals("client", credentials.getUserProfile().getId());
}
Also used : lombok.val(lombok.val) MockTicketGrantingTicket(org.apereo.cas.mock.MockTicketGrantingTicket) HashMap(java.util.HashMap) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) HardTimeoutExpirationPolicy(org.apereo.cas.ticket.expiration.HardTimeoutExpirationPolicy) ArrayList(java.util.ArrayList) JEEContext(org.pac4j.core.context.JEEContext) OAuth20DefaultCode(org.apereo.cas.ticket.code.OAuth20DefaultCode) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) UsernamePasswordCredentials(org.pac4j.core.credentials.UsernamePasswordCredentials) Test(org.junit.jupiter.api.Test)

Example 73 with MockTicketGrantingTicket

use of org.apereo.cas.mock.MockTicketGrantingTicket in project cas by apereo.

the class OAuth20ProofKeyCodeExchangeAuthenticatorTests method verifyAuthenticationHashedWithoutSecret.

@Test
public void verifyAuthenticationHashedWithoutSecret() throws Exception {
    val hash = EncodingUtils.encodeUrlSafeBase64(DigestUtils.rawDigestSha256("ABCD123"));
    val credentials = new UsernamePasswordCredentials("clientWithoutSecret", "ABCD123");
    val request = new MockHttpServletRequest();
    val ticket = new OAuth20DefaultCode("CODE-1234567890", RegisteredServiceTestUtils.getService(), RegisteredServiceTestUtils.getAuthentication(), new HardTimeoutExpirationPolicy(10), new MockTicketGrantingTicket("casuser"), new ArrayList<>(), hash, "s256", "clientid12345", new HashMap<>(), OAuth20ResponseTypes.CODE, OAuth20GrantTypes.AUTHORIZATION_CODE);
    ticketRegistry.addTicket(ticket);
    request.addParameter(OAuth20Constants.CLIENT_ID, "clientWithoutSecret");
    request.addParameter(OAuth20Constants.CODE_VERIFIER, "ABCD123");
    request.addParameter(OAuth20Constants.CODE, ticket.getId());
    val ctx = new JEEContext(request, new MockHttpServletResponse());
    authenticator.validate(credentials, ctx, JEESessionStore.INSTANCE);
    assertNotNull(credentials.getUserProfile());
    assertEquals("clientWithoutSecret", credentials.getUserProfile().getId());
}
Also used : lombok.val(lombok.val) MockTicketGrantingTicket(org.apereo.cas.mock.MockTicketGrantingTicket) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) HardTimeoutExpirationPolicy(org.apereo.cas.ticket.expiration.HardTimeoutExpirationPolicy) JEEContext(org.pac4j.core.context.JEEContext) OAuth20DefaultCode(org.apereo.cas.ticket.code.OAuth20DefaultCode) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) UsernamePasswordCredentials(org.pac4j.core.credentials.UsernamePasswordCredentials) Test(org.junit.jupiter.api.Test)

Example 74 with MockTicketGrantingTicket

use of org.apereo.cas.mock.MockTicketGrantingTicket in project cas by apereo.

the class OAuth20ProofKeyCodeExchangeAuthenticatorTests method verifyAuthenticationNotHashedCorrectly.

@Test
public void verifyAuthenticationNotHashedCorrectly() throws Exception {
    val credentials = new UsernamePasswordCredentials("client", "ABCD123");
    val request = new MockHttpServletRequest();
    val ticket = new OAuth20DefaultCode("CODE-1234567890", RegisteredServiceTestUtils.getService(), RegisteredServiceTestUtils.getAuthentication(), new HardTimeoutExpirationPolicy(10), new MockTicketGrantingTicket("casuser"), new ArrayList<>(), "something-else", "s256", "clientid12345", new HashMap<>(), OAuth20ResponseTypes.CODE, OAuth20GrantTypes.AUTHORIZATION_CODE);
    ticketRegistry.addTicket(ticket);
    request.addParameter(OAuth20Constants.CLIENT_ID, "client");
    request.addParameter(OAuth20Constants.CODE_VERIFIER, "ABCD123");
    request.addParameter(OAuth20Constants.CLIENT_SECRET, "secret");
    request.addParameter(OAuth20Constants.CODE, ticket.getId());
    val ctx = new JEEContext(request, new MockHttpServletResponse());
    assertThrows(CredentialsException.class, () -> authenticator.validate(credentials, ctx, JEESessionStore.INSTANCE));
}
Also used : lombok.val(lombok.val) MockTicketGrantingTicket(org.apereo.cas.mock.MockTicketGrantingTicket) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) HardTimeoutExpirationPolicy(org.apereo.cas.ticket.expiration.HardTimeoutExpirationPolicy) JEEContext(org.pac4j.core.context.JEEContext) OAuth20DefaultCode(org.apereo.cas.ticket.code.OAuth20DefaultCode) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) UsernamePasswordCredentials(org.pac4j.core.credentials.UsernamePasswordCredentials) Test(org.junit.jupiter.api.Test)

Example 75 with MockTicketGrantingTicket

use of org.apereo.cas.mock.MockTicketGrantingTicket in project cas by apereo.

the class OAuth20AuthorizationCodeGrantTypeTokenRequestValidatorTests method registerTicket.

private OAuth20Code registerTicket(final OAuthRegisteredService service) throws Exception {
    val builder = new OAuth20DefaultCasAuthenticationBuilder(PrincipalFactoryUtils.newPrincipalFactory(), new WebApplicationServiceFactory(), new DefaultOAuth20ProfileScopeToAttributesFilter(), new CasConfigurationProperties());
    val oauthCasAuthenticationBuilderService = builder.buildService(service, null, false);
    val expirationPolicy = new ExpirationPolicyBuilder() {

        private static final long serialVersionUID = 3911344031977989503L;

        @Override
        public ExpirationPolicy buildTicketExpirationPolicy() {
            return new OAuth20CodeExpirationPolicy(1, 60);
        }

        @Override
        public Class getTicketType() {
            return OAuth20Code.class;
        }
    };
    val oauthCode = new OAuth20DefaultOAuthCodeFactory(expirationPolicy, mock(ServicesManager.class)).create(oauthCasAuthenticationBuilderService, RegisteredServiceTestUtils.getAuthentication(), new MockTicketGrantingTicket("casuser"), new HashSet<>(), null, null, "clientid12345", new HashMap<>(), OAuth20ResponseTypes.CODE, OAuth20GrantTypes.AUTHORIZATION_CODE);
    this.ticketRegistry.addTicket(oauthCode);
    return oauthCode;
}
Also used : lombok.val(lombok.val) MockTicketGrantingTicket(org.apereo.cas.mock.MockTicketGrantingTicket) OAuth20Code(org.apereo.cas.ticket.code.OAuth20Code) WebApplicationServiceFactory(org.apereo.cas.authentication.principal.WebApplicationServiceFactory) CasConfigurationProperties(org.apereo.cas.configuration.CasConfigurationProperties) OAuth20DefaultCasAuthenticationBuilder(org.apereo.cas.support.oauth.authenticator.OAuth20DefaultCasAuthenticationBuilder) OAuth20DefaultOAuthCodeFactory(org.apereo.cas.ticket.code.OAuth20DefaultOAuthCodeFactory) ExpirationPolicyBuilder(org.apereo.cas.ticket.ExpirationPolicyBuilder) OAuth20CodeExpirationPolicy(org.apereo.cas.ticket.code.OAuth20CodeExpirationPolicy) DefaultOAuth20ProfileScopeToAttributesFilter(org.apereo.cas.support.oauth.profile.DefaultOAuth20ProfileScopeToAttributesFilter)

Aggregations

MockTicketGrantingTicket (org.apereo.cas.mock.MockTicketGrantingTicket)224 lombok.val (lombok.val)199 Test (org.junit.jupiter.api.Test)164 MockHttpServletResponse (org.springframework.mock.web.MockHttpServletResponse)93 MockHttpServletRequest (org.springframework.mock.web.MockHttpServletRequest)92 MockRequestContext (org.springframework.webflow.test.MockRequestContext)42 ServletExternalContext (org.springframework.webflow.context.servlet.ServletExternalContext)39 SpringBootTest (org.springframework.boot.test.context.SpringBootTest)36 JEEContext (org.pac4j.core.context.JEEContext)33 MockServletContext (org.springframework.mock.web.MockServletContext)33 HashMap (java.util.HashMap)31 MockServiceTicket (org.apereo.cas.mock.MockServiceTicket)18 Test (org.junit.Test)18 WebApplicationServiceFactory (org.apereo.cas.authentication.principal.WebApplicationServiceFactory)13 TicketGrantingTicket (org.apereo.cas.ticket.TicketGrantingTicket)12 CasProfile (org.pac4j.cas.profile.CasProfile)11 Authentication (org.apereo.cas.authentication.Authentication)10 HardTimeoutExpirationPolicy (org.apereo.cas.ticket.expiration.HardTimeoutExpirationPolicy)10 UsernamePasswordCredentials (org.pac4j.core.credentials.UsernamePasswordCredentials)10 RedirectView (org.springframework.web.servlet.view.RedirectView)10