Search in sources :

Example 36 with UsernamePasswordCredentials

use of org.pac4j.core.credentials.UsernamePasswordCredentials in project cas by apereo.

the class OAuth20AccessTokenSecurityLogicTests method verifyOperation.

@Test
public void verifyOperation() throws Exception {
    val request = new MockHttpServletRequest();
    val response = new MockHttpServletResponse();
    request.addParameter(OAuth20Constants.CLIENT_ID, CLIENT_ID);
    val logic = new DefaultSecurityLogic();
    logic.setLoadProfilesFromSession(false);
    val mockClient = mock(DirectClient.class);
    when(mockClient.getName()).thenReturn("MockIndirectClient");
    when(mockClient.isInitialized()).thenReturn(true);
    when(mockClient.getCredentials(any(), any())).thenReturn(Optional.of(new UsernamePasswordCredentials("casuser", "Mellon")));
    val profile = new CommonProfile();
    profile.setId(UUID.randomUUID().toString());
    when(mockClient.getUserProfile(any(), any(), any())).thenReturn(Optional.of(profile));
    val context = new JEEContext(request, response);
    val profileManager = new ProfileManager(context, JEESessionStore.INSTANCE);
    profileManager.save(true, profile, false);
    val result = (UserProfile) logic.perform(context, JEESessionStore.INSTANCE, new Config(mockClient), (webContext, sessionStore, collection, objects) -> collection.iterator().next(), JEEHttpActionAdapter.INSTANCE, "MockIndirectClient", DefaultAuthorizers.IS_FULLY_AUTHENTICATED, DefaultMatchers.SECURITYHEADERS);
    assertNotNull(result);
    assertEquals(1, profileManager.getProfiles().size());
}
Also used : lombok.val(lombok.val) ProfileManager(org.pac4j.core.profile.ProfileManager) UsernamePasswordCredentials(org.pac4j.core.credentials.UsernamePasswordCredentials) DefaultAuthorizers(org.pac4j.core.authorization.authorizer.DefaultAuthorizers) OAuth20Constants(org.apereo.cas.support.oauth.OAuth20Constants) CommonProfile(org.pac4j.core.profile.CommonProfile) DirectClient(org.pac4j.core.client.DirectClient) lombok.val(lombok.val) DefaultSecurityLogic(org.pac4j.core.engine.DefaultSecurityLogic) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) UUID(java.util.UUID) ProfileManager(org.pac4j.core.profile.ProfileManager) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) Test(org.junit.jupiter.api.Test) Mockito(org.mockito.Mockito) JEEHttpActionAdapter(org.pac4j.core.http.adapter.JEEHttpActionAdapter) Assertions(org.junit.jupiter.api.Assertions) Config(org.pac4j.core.config.Config) Optional(java.util.Optional) Tag(org.junit.jupiter.api.Tag) DefaultMatchers(org.pac4j.core.matching.matcher.DefaultMatchers) UserProfile(org.pac4j.core.profile.UserProfile) AbstractOAuth20Tests(org.apereo.cas.AbstractOAuth20Tests) JEEContext(org.pac4j.core.context.JEEContext) JEESessionStore(org.pac4j.core.context.session.JEESessionStore) UserProfile(org.pac4j.core.profile.UserProfile) CommonProfile(org.pac4j.core.profile.CommonProfile) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) Config(org.pac4j.core.config.Config) DefaultSecurityLogic(org.pac4j.core.engine.DefaultSecurityLogic) JEEContext(org.pac4j.core.context.JEEContext) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) UsernamePasswordCredentials(org.pac4j.core.credentials.UsernamePasswordCredentials) Test(org.junit.jupiter.api.Test)

Example 37 with UsernamePasswordCredentials

use of org.pac4j.core.credentials.UsernamePasswordCredentials in project cas by apereo.

the class ClientAuthenticationMetaDataPopulatorTests method verifySupports.

@Test
public void verifySupports() {
    val populator = new ClientAuthenticationMetaDataPopulator();
    val clintCreds = new ClientCredential(new UsernamePasswordCredentials("casuser", "pa$$"), "FacebookClient");
    assertTrue(populator.supports(clintCreds));
}
Also used : lombok.val(lombok.val) ClientCredential(org.apereo.cas.authentication.principal.ClientCredential) UsernamePasswordCredentials(org.pac4j.core.credentials.UsernamePasswordCredentials) Test(org.junit.jupiter.api.Test)

Example 38 with UsernamePasswordCredentials

use of org.pac4j.core.credentials.UsernamePasswordCredentials in project cas by apereo.

the class ClientAuthenticationMetaDataPopulatorTests method verifyAttribute.

@Test
public void verifyAttribute() {
    val populator = new ClientAuthenticationMetaDataPopulator();
    val credentials = new ClientCredential(new UsernamePasswordCredentials("casuser", "pa$$"), "FacebookClient");
    val builder = CoreAuthenticationTestUtils.getAuthenticationBuilder();
    populator.populateAttributes(builder, new DefaultAuthenticationTransactionFactory().newTransaction(credentials));
    val auth = builder.build();
    assertNotNull(auth.getAttributes().get(ClientCredential.AUTHENTICATION_ATTRIBUTE_CLIENT_NAME));
}
Also used : lombok.val(lombok.val) ClientCredential(org.apereo.cas.authentication.principal.ClientCredential) DefaultAuthenticationTransactionFactory(org.apereo.cas.authentication.DefaultAuthenticationTransactionFactory) UsernamePasswordCredentials(org.pac4j.core.credentials.UsernamePasswordCredentials) Test(org.junit.jupiter.api.Test)

Example 39 with UsernamePasswordCredentials

use of org.pac4j.core.credentials.UsernamePasswordCredentials in project ArachneCentralAPI by OHDSI.

the class AuthenticationServiceImpl method authenticateAndGetAuthToken.

@Transactional(rollbackFor = Exception.class, readOnly = false)
public String authenticateAndGetAuthToken(CommonAuthenticationRequest authenticationRequest) {
    String username = authenticationRequest.getUsername();
    String password = authenticationRequest.getPassword();
    try {
        UserInfo userInfo = authenticator.authenticate(authMethodName, new UsernamePasswordCredentials(username, password));
        authenticate(userInfo.getUsername(), password);
        return userInfo.getToken();
    } catch (Exception e) {
        SecurityContextHolder.clearContext();
        throw e;
    }
}
Also used : UserInfo(org.ohdsi.authenticator.model.UserInfo) UsernamePasswordCredentials(org.pac4j.core.credentials.UsernamePasswordCredentials) Transactional(org.springframework.transaction.annotation.Transactional)

Example 40 with UsernamePasswordCredentials

use of org.pac4j.core.credentials.UsernamePasswordCredentials in project cas by apereo.

the class ECPProfileHandlerController method extractBasicAuthenticationCredential.

private Credential extractBasicAuthenticationCredential(final HttpServletRequest request, final HttpServletResponse response) {
    try {
        final BasicAuthExtractor extractor = new BasicAuthExtractor(this.getClass().getSimpleName());
        final WebContext webContext = WebUtils.getPac4jJ2EContext(request, response);
        final UsernamePasswordCredentials credentials = extractor.extract(webContext);
        if (credentials != null) {
            LOGGER.debug("Received basic authentication ECP request from credentials [{}]", credentials);
            return new UsernamePasswordCredential(credentials.getUsername(), credentials.getPassword());
        }
    } catch (final Exception e) {
        LOGGER.warn(e.getMessage(), e);
    }
    return null;
}
Also used : BasicAuthExtractor(org.pac4j.core.credentials.extractor.BasicAuthExtractor) WebContext(org.pac4j.core.context.WebContext) UsernamePasswordCredential(org.apereo.cas.authentication.UsernamePasswordCredential) AuthenticationException(org.apereo.cas.authentication.AuthenticationException) UsernamePasswordCredentials(org.pac4j.core.credentials.UsernamePasswordCredentials)

Aggregations

UsernamePasswordCredentials (org.pac4j.core.credentials.UsernamePasswordCredentials)80 lombok.val (lombok.val)34 JEEContext (org.pac4j.core.context.JEEContext)24 CommonProfile (org.pac4j.core.profile.CommonProfile)22 Test (org.junit.Test)21 Test (org.junit.jupiter.api.Test)21 MockHttpServletRequest (org.springframework.mock.web.MockHttpServletRequest)20 MockHttpServletResponse (org.springframework.mock.web.MockHttpServletResponse)20 MockTicketGrantingTicket (org.apereo.cas.mock.MockTicketGrantingTicket)10 BasicAuthExtractor (org.pac4j.core.credentials.extractor.BasicAuthExtractor)9 OAuth20DefaultCode (org.apereo.cas.ticket.code.OAuth20DefaultCode)8 HardTimeoutExpirationPolicy (org.apereo.cas.ticket.expiration.HardTimeoutExpirationPolicy)8 HashMap (java.util.HashMap)7 SimpleTestUsernamePasswordAuthenticator (org.pac4j.http.credentials.authenticator.test.SimpleTestUsernamePasswordAuthenticator)6 Map (java.util.Map)5 MockWebContext (org.pac4j.core.context.MockWebContext)5 UsernamePasswordCredential (org.apereo.cas.authentication.credential.UsernamePasswordCredential)4 CredentialsException (org.pac4j.core.exception.CredentialsException)4 ArrayList (java.util.ArrayList)3 WebContext (org.pac4j.core.context.WebContext)3