Search in sources :

Example 16 with CredentialsException

use of org.pac4j.core.exception.CredentialsException in project pac4j by pac4j.

the class IpRegexpAuthenticator method validate.

@Override
public void validate(final TokenCredentials credentials, final WebContext context) {
    init();
    final String ip = credentials.getToken();
    if (!this.pattern.matcher(ip).matches()) {
        throw new CredentialsException("Unauthorized IP address: " + ip);
    }
    final IpProfile profile = getProfileDefinition().newProfile();
    profile.setId(ip);
    logger.debug("profile: {}", profile);
    credentials.setUserProfile(profile);
}
Also used : CredentialsException(org.pac4j.core.exception.CredentialsException) IpProfile(org.pac4j.http.profile.IpProfile)

Example 17 with CredentialsException

use of org.pac4j.core.exception.CredentialsException in project pac4j by pac4j.

the class SimpleTestTokenAuthenticator method validate.

@Override
public void validate(final TokenCredentials credentials, final WebContext context) {
    if (credentials == null) {
        throw new CredentialsException("credentials must not be null");
    }
    if (CommonHelper.isBlank(credentials.getToken())) {
        throw new CredentialsException("token must not be blank");
    }
    final String token = credentials.getToken();
    final CommonProfile profile = new CommonProfile();
    profile.setId(token);
    credentials.setUserProfile(profile);
}
Also used : CommonProfile(org.pac4j.core.profile.CommonProfile) CredentialsException(org.pac4j.core.exception.CredentialsException)

Example 18 with CredentialsException

use of org.pac4j.core.exception.CredentialsException in project pac4j by pac4j.

the class BasicAuthExtractor method extract.

@Override
public UsernamePasswordCredentials extract(WebContext context) {
    final TokenCredentials credentials = this.extractor.extract(context);
    if (credentials == null) {
        return null;
    }
    final byte[] decoded = Base64.getDecoder().decode(credentials.getToken());
    String token;
    try {
        token = new String(decoded, "UTF-8");
    } catch (final UnsupportedEncodingException e) {
        throw new CredentialsException("Bad format of the basic auth header");
    }
    final int delim = token.indexOf(":");
    if (delim < 0) {
        throw new CredentialsException("Bad format of the basic auth header");
    }
    return new UsernamePasswordCredentials(token.substring(0, delim), token.substring(delim + 1));
}
Also used : UnsupportedEncodingException(java.io.UnsupportedEncodingException) CredentialsException(org.pac4j.core.exception.CredentialsException) TokenCredentials(org.pac4j.core.credentials.TokenCredentials) UsernamePasswordCredentials(org.pac4j.core.credentials.UsernamePasswordCredentials)

Example 19 with CredentialsException

use of org.pac4j.core.exception.CredentialsException in project pac4j by pac4j.

the class ParameterExtractor method extract.

@Override
public TokenCredentials extract(WebContext context) {
    final String method = context.getRequestMethod();
    if (HTTP_METHOD.GET.name().equalsIgnoreCase(method) && !supportGetRequest) {
        throw new CredentialsException("GET requests not supported");
    } else if (HTTP_METHOD.POST.name().equalsIgnoreCase(method) && !supportPostRequest) {
        throw new CredentialsException("POST requests not supported");
    }
    final String value = context.getRequestParameter(this.parameterName);
    if (value == null) {
        return null;
    }
    return new TokenCredentials(value);
}
Also used : CredentialsException(org.pac4j.core.exception.CredentialsException) TokenCredentials(org.pac4j.core.credentials.TokenCredentials)

Example 20 with CredentialsException

use of org.pac4j.core.exception.CredentialsException in project cas by apereo.

the class BaseUmaTokenAuthenticator method validate.

@Override
public void validate(final Credentials creds, final WebContext webContext, final SessionStore sessionStore) {
    val credentials = (TokenCredentials) creds;
    val token = extractAccessTokenFrom(credentials.getToken().trim());
    val at = this.centralAuthenticationService.getTicket(token, OAuth20AccessToken.class);
    if (!at.getScopes().contains(getRequiredScope())) {
        val err = String.format("Missing scope [%s]. Unable to authenticate access token %s", getRequiredScope(), token);
        throw new CredentialsException(err);
    }
    val profile = new CommonProfile();
    val authentication = at.getAuthentication();
    val principal = authentication.getPrincipal();
    profile.setId(principal.getId());
    val attributes = new LinkedHashMap<String, Object>(authentication.getAttributes());
    attributes.putAll(principal.getAttributes());
    profile.addAttributes(attributes);
    profile.addPermissions(at.getScopes());
    profile.addAttribute(OAuth20AccessToken.class.getName(), at);
    LOGGER.debug("Authenticated access token [{}]", profile);
    credentials.setUserProfile(profile);
}
Also used : lombok.val(lombok.val) OAuth20AccessToken(org.apereo.cas.ticket.accesstoken.OAuth20AccessToken) CommonProfile(org.pac4j.core.profile.CommonProfile) CredentialsException(org.pac4j.core.exception.CredentialsException) TokenCredentials(org.pac4j.core.credentials.TokenCredentials) LinkedHashMap(java.util.LinkedHashMap)

Aggregations

CredentialsException (org.pac4j.core.exception.CredentialsException)20 TokenCredentials (org.pac4j.core.credentials.TokenCredentials)5 CommonProfile (org.pac4j.core.profile.CommonProfile)5 lombok.val (lombok.val)4 UsernamePasswordCredentials (org.pac4j.core.credentials.UsernamePasswordCredentials)4 Authentication (org.apereo.cas.authentication.Authentication)2 AuthenticationResult (org.apereo.cas.authentication.AuthenticationResult)2 UsernamePasswordCredential (org.apereo.cas.authentication.UsernamePasswordCredential)2 Principal (org.apereo.cas.authentication.principal.Principal)2 Service (org.apereo.cas.authentication.principal.Service)2 RegisteredService (org.apereo.cas.services.RegisteredService)2 OAuthClientProfile (org.apereo.cas.support.oauth.profile.OAuthClientProfile)2 OAuthUserProfile (org.apereo.cas.support.oauth.profile.OAuthUserProfile)2 OAuthRegisteredService (org.apereo.cas.support.oauth.services.OAuthRegisteredService)2 DigestCredentials (org.pac4j.http.credentials.DigestCredentials)2 EncryptionMethod (com.nimbusds.jose.EncryptionMethod)1 JOSEException (com.nimbusds.jose.JOSEException)1 JWEAlgorithm (com.nimbusds.jose.JWEAlgorithm)1 JWEHeader (com.nimbusds.jose.JWEHeader)1 JWSAlgorithm (com.nimbusds.jose.JWSAlgorithm)1