use of org.pac4j.core.exception.TechnicalException in project ddf by codice.
the class OidcCredentialsResolver method trySendingGrantAndPopulatingCredentials.
private void trySendingGrantAndPopulatingCredentials(AuthorizationGrant grant, OidcCredentials credentials, WebContext webContext) throws IOException, ParseException {
final OIDCTokens oidcTokens = getOidcTokens(grant);
try {
JWT idToken = oidcTokens.getIDToken();
if (idToken != null) {
OidcTokenValidator.validateIdTokens(idToken, webContext, configuration, client);
}
AccessToken accessToken = oidcTokens.getAccessToken();
if (accessToken != null) {
OidcTokenValidator.validateAccessToken(accessToken, idToken, resourceRetriever, metadata, configuration);
}
credentials.setAccessToken(accessToken);
credentials.setIdToken(idToken);
credentials.setRefreshToken(oidcTokens.getRefreshToken());
} catch (OidcValidationException e) {
throw new TechnicalException(e);
}
}
use of org.pac4j.core.exception.TechnicalException in project ddf by codice.
the class OidcRealm method doGetAuthenticationInfo.
@Override
protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {
// token is guaranteed to be of type OidcAuthenticationToken by the supports() method
OidcAuthenticationToken oidcAuthenticationToken = (OidcAuthenticationToken) authenticationToken;
OidcCredentials credentials = (OidcCredentials) oidcAuthenticationToken.getCredentials();
OidcConfiguration oidcConfiguration = oidcHandlerConfiguration.getOidcConfiguration();
OIDCProviderMetadata oidcProviderMetadata = oidcConfiguration.findProviderMetadata();
WebContext webContext = (WebContext) oidcAuthenticationToken.getContext();
OidcClient<OidcConfiguration> oidcClient = oidcHandlerConfiguration.getOidcClient(webContext.getFullRequestURL());
int connectTimeout = oidcHandlerConfiguration.getConnectTimeout();
int readTimeout = oidcHandlerConfiguration.getReadTimeout();
try {
OidcCredentialsResolver oidcCredentialsResolver = new OidcCredentialsResolver(oidcConfiguration, oidcClient, oidcProviderMetadata, connectTimeout, readTimeout);
oidcCredentialsResolver.resolveIdToken(credentials, webContext);
} catch (TechnicalException e) {
throw new AuthenticationException(e);
}
// problem getting id token, invalidate credentials
if (credentials.getIdToken() == null) {
webContext.getSessionStore().destroySession(webContext);
String msg = String.format("Could not fetch id token with Oidc credentials (%s). " + "This may be due to the credentials expiring. " + "Invalidating session in order to acquire valid credentials.", credentials);
LOGGER.warn(msg);
throw new AuthenticationException(msg);
}
OidcProfileCreator oidcProfileCreator = new CustomOidcProfileCreator(oidcConfiguration, oidcClient);
Optional<UserProfile> userProfile = oidcProfileCreator.create(credentials, webContext);
SimpleAuthenticationInfo simpleAuthenticationInfo = new SimpleAuthenticationInfo();
simpleAuthenticationInfo.setCredentials(credentials);
if (userProfile.isPresent()) {
OidcProfile oidcProfile = (OidcProfile) userProfile.get();
simpleAuthenticationInfo.setPrincipals(createPrincipalCollectionFromCredentials(oidcProfile));
} else {
simpleAuthenticationInfo.setPrincipals(new SimplePrincipalCollection());
}
return simpleAuthenticationInfo;
}
use of org.pac4j.core.exception.TechnicalException in project pac4j by pac4j.
the class JwtAuthenticator method createJwtProfile.
@SuppressWarnings("unchecked")
protected void createJwtProfile(final TokenCredentials credentials, final JWT jwt) throws ParseException {
final JWTClaimsSet claimSet = jwt.getJWTClaimsSet();
String subject = claimSet.getSubject();
if (subject == null) {
throw new TechnicalException("JWT must contain a subject ('sub' claim)");
}
final Date expirationTime = claimSet.getExpirationTime();
if (expirationTime != null) {
final Date now = new Date();
if (expirationTime.before(now)) {
logger.error("The JWT is expired: no profile is built");
return;
}
}
final Map<String, Object> attributes = new HashMap<>(claimSet.getClaims());
attributes.remove(JwtClaims.SUBJECT);
final List<String> roles = (List<String>) attributes.get(JwtGenerator.INTERNAL_ROLES);
attributes.remove(JwtGenerator.INTERNAL_ROLES);
final List<String> permissions = (List<String>) attributes.get(JwtGenerator.INTERNAL_PERMISSIONS);
attributes.remove(JwtGenerator.INTERNAL_PERMISSIONS);
final CommonProfile profile = ProfileHelper.restoreOrBuildProfile(getProfileDefinition(), subject, attributes, null);
if (roles != null) {
profile.addRoles(roles);
}
if (permissions != null) {
profile.addPermissions(permissions);
}
credentials.setUserProfile(profile);
}
use of org.pac4j.core.exception.TechnicalException in project pac4j by pac4j.
the class OAuth20RedirectActionBuilder method redirect.
@Override
public RedirectAction redirect(final WebContext context) {
try {
final OAuth20Service service;
// with state: generate a state, save it in session and build a new service with this state
if (this.configuration.isWithState()) {
final String state = getStateParameter();
logger.debug("save sessionState: {}", state);
context.getSessionStore().set(context, this.configuration.getStateSessionAttributeName(client.getName()), state);
service = this.configuration.buildService(context, client, state);
} else {
service = this.configuration.buildService(context, client, null);
}
final String authorizationUrl = service.getAuthorizationUrl(this.configuration.getCustomParams());
logger.debug("authorizationUrl: {}", authorizationUrl);
return RedirectAction.redirect(authorizationUrl);
} catch (final OAuthException e) {
throw new TechnicalException(e);
}
}
use of org.pac4j.core.exception.TechnicalException in project pac4j by pac4j.
the class SAML2Client method initChainingMetadataResolver.
protected ChainingMetadataResolver initChainingMetadataResolver(final MetadataResolver idpMetadataProvider, final MetadataResolver spMetadataProvider) {
final ChainingMetadataResolver metadataManager = new ChainingMetadataResolver();
metadataManager.setId(ChainingMetadataResolver.class.getCanonicalName());
try {
final List<MetadataResolver> list = new ArrayList<>();
list.add(idpMetadataProvider);
list.add(spMetadataProvider);
metadataManager.setResolvers(list);
metadataManager.initialize();
} catch (final ResolverException e) {
throw new TechnicalException("Error adding idp or sp metadatas to manager", e);
} catch (final ComponentInitializationException e) {
throw new TechnicalException("Error initializing manager", e);
}
return metadataManager;
}
Aggregations