Search in sources :

Example 1 with RestProfile

use of org.pac4j.http.profile.RestProfile in project pac4j by pac4j.

the class RestAuthenticator method internalInit.

@Override
protected void internalInit() {
    CommonHelper.assertNotBlank("url", url);
    defaultProfileDefinition(new CommonProfileDefinition<>(x -> new RestProfile()));
    if (mapper == null) {
        mapper = new ObjectMapper();
        mapper.configure(JsonParser.Feature.ALLOW_UNQUOTED_FIELD_NAMES, true);
        mapper.configure(JsonParser.Feature.ALLOW_SINGLE_QUOTES, true);
        mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
    }
}
Also used : UsernamePasswordCredentials(org.pac4j.core.credentials.UsernamePasswordCredentials) HttpURLConnection(java.net.HttpURLConnection) Logger(org.slf4j.Logger) JsonParser(com.fasterxml.jackson.core.JsonParser) URL(java.net.URL) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) LoggerFactory(org.slf4j.LoggerFactory) HttpConstants(org.pac4j.core.context.HttpConstants) IOException(java.io.IOException) HashMap(java.util.HashMap) ProfileDefinitionAware(org.pac4j.core.profile.definition.ProfileDefinitionAware) DeserializationFeature(com.fasterxml.jackson.databind.DeserializationFeature) StandardCharsets(java.nio.charset.StandardCharsets) WebContext(org.pac4j.core.context.WebContext) Base64(java.util.Base64) CommonProfileDefinition(org.pac4j.core.profile.definition.CommonProfileDefinition) RestProfile(org.pac4j.http.profile.RestProfile) Map(java.util.Map) Authenticator(org.pac4j.core.credentials.authenticator.Authenticator) CommonHelper(org.pac4j.core.util.CommonHelper) HttpUtils(org.pac4j.core.util.HttpUtils) TechnicalException(org.pac4j.core.exception.TechnicalException) RestProfile(org.pac4j.http.profile.RestProfile) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper)

Example 2 with RestProfile

use of org.pac4j.http.profile.RestProfile in project pac4j by pac4j.

the class RestAuthenticator method buildProfile.

protected void buildProfile(final UsernamePasswordCredentials credentials, final String body) {
    final var profileClass = (RestProfile) getProfileDefinition().newProfile();
    final RestProfile profile;
    try {
        profile = mapper.readValue(body, profileClass.getClass());
    } catch (final IOException e) {
        throw new TechnicalException(e);
    }
    logger.debug("profile: {}", profile);
    credentials.setUserProfile(profile);
}
Also used : TechnicalException(org.pac4j.core.exception.TechnicalException) IOException(java.io.IOException) RestProfile(org.pac4j.http.profile.RestProfile)

Example 3 with RestProfile

use of org.pac4j.http.profile.RestProfile in project pac4j by pac4j.

the class RestAuthenticator method internalInit.

@Override
protected void internalInit(final boolean forceReinit) {
    CommonHelper.assertNotBlank("url", url);
    defaultProfileDefinition(new CommonProfileDefinition(x -> new RestProfile()));
    if (mapper == null) {
        mapper = new ObjectMapper();
        mapper.configure(JsonParser.Feature.ALLOW_UNQUOTED_FIELD_NAMES, true);
        mapper.configure(JsonParser.Feature.ALLOW_SINGLE_QUOTES, true);
        mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
    }
}
Also used : UsernamePasswordCredentials(org.pac4j.core.credentials.UsernamePasswordCredentials) HttpURLConnection(java.net.HttpURLConnection) Logger(org.slf4j.Logger) JsonParser(com.fasterxml.jackson.core.JsonParser) URL(java.net.URL) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) LoggerFactory(org.slf4j.LoggerFactory) HttpConstants(org.pac4j.core.context.HttpConstants) IOException(java.io.IOException) HashMap(java.util.HashMap) ProfileDefinitionAware(org.pac4j.core.profile.definition.ProfileDefinitionAware) SessionStore(org.pac4j.core.context.session.SessionStore) DeserializationFeature(com.fasterxml.jackson.databind.DeserializationFeature) StandardCharsets(java.nio.charset.StandardCharsets) WebContext(org.pac4j.core.context.WebContext) Base64(java.util.Base64) CommonProfileDefinition(org.pac4j.core.profile.definition.CommonProfileDefinition) RestProfile(org.pac4j.http.profile.RestProfile) Map(java.util.Map) Authenticator(org.pac4j.core.credentials.authenticator.Authenticator) CommonHelper(org.pac4j.core.util.CommonHelper) HttpUtils(org.pac4j.core.util.HttpUtils) TechnicalException(org.pac4j.core.exception.TechnicalException) Credentials(org.pac4j.core.credentials.Credentials) RestProfile(org.pac4j.http.profile.RestProfile) CommonProfileDefinition(org.pac4j.core.profile.definition.CommonProfileDefinition) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper)

Example 4 with RestProfile

use of org.pac4j.http.profile.RestProfile in project pac4j by pac4j.

the class RestAuthenticatorIT method testProfileOk.

@Test
public void testProfileOk() {
    final var authenticator = new RestAuthenticator("http://localhost:" + PORT + "?r=ok");
    final var credentials = new UsernamePasswordCredentials(GOOD_USERNAME, PASSWORD);
    authenticator.validate(credentials, MockWebContext.create(), new MockSessionStore());
    final var profile = (RestProfile) credentials.getUserProfile();
    assertNotNull(profile);
    assertEquals(ID, profile.getId());
    assertEquals(1, profile.getRoles().size());
    assertEquals(ROLE, profile.getRoles().iterator().next());
}
Also used : MockSessionStore(org.pac4j.core.context.session.MockSessionStore) RestProfile(org.pac4j.http.profile.RestProfile) UsernamePasswordCredentials(org.pac4j.core.credentials.UsernamePasswordCredentials) Test(org.junit.Test)

Example 5 with RestProfile

use of org.pac4j.http.profile.RestProfile in project pac4j by pac4j.

the class RestAuthenticatorIT method testNotFound.

@Test
public void testNotFound() {
    final var authenticator = new RestAuthenticator("http://localhost:" + PORT + "?r=notfound");
    final var credentials = new UsernamePasswordCredentials(GOOD_USERNAME, PASSWORD);
    authenticator.validate(credentials, MockWebContext.create(), new MockSessionStore());
    final var profile = (RestProfile) credentials.getUserProfile();
    assertNull(profile);
}
Also used : MockSessionStore(org.pac4j.core.context.session.MockSessionStore) RestProfile(org.pac4j.http.profile.RestProfile) UsernamePasswordCredentials(org.pac4j.core.credentials.UsernamePasswordCredentials) Test(org.junit.Test)

Aggregations

RestProfile (org.pac4j.http.profile.RestProfile)5 UsernamePasswordCredentials (org.pac4j.core.credentials.UsernamePasswordCredentials)4 IOException (java.io.IOException)3 TechnicalException (org.pac4j.core.exception.TechnicalException)3 JsonParser (com.fasterxml.jackson.core.JsonParser)2 DeserializationFeature (com.fasterxml.jackson.databind.DeserializationFeature)2 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)2 HttpURLConnection (java.net.HttpURLConnection)2 URL (java.net.URL)2 StandardCharsets (java.nio.charset.StandardCharsets)2 Base64 (java.util.Base64)2 HashMap (java.util.HashMap)2 Map (java.util.Map)2 Test (org.junit.Test)2 HttpConstants (org.pac4j.core.context.HttpConstants)2 WebContext (org.pac4j.core.context.WebContext)2 MockSessionStore (org.pac4j.core.context.session.MockSessionStore)2 Authenticator (org.pac4j.core.credentials.authenticator.Authenticator)2 CommonProfileDefinition (org.pac4j.core.profile.definition.CommonProfileDefinition)2 ProfileDefinitionAware (org.pac4j.core.profile.definition.ProfileDefinitionAware)2