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);
}
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);
}
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));
}
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);
}
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);
}
Aggregations