Java Code Examples for org.wso2.carbon.identity.application.authentication.framework.util.FrameworkUtils#getFederatedSubjectFromClaims()

The following examples show how to use org.wso2.carbon.identity.application.authentication.framework.util.FrameworkUtils#getFederatedSubjectFromClaims() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: OpenIDAuthenticator.java    From carbon-identity with Apache License 2.0 5 votes vote down vote up
/**
 * @subject
 */
protected String getSubjectFromUserIDClaimURI(AuthenticationContext context) {
    String subject = null;
    try {
        subject = FrameworkUtils.getFederatedSubjectFromClaims(context, getClaimDialectURI());
    } catch (Exception e) {
        if(log.isDebugEnabled()) {
            log.debug("Couldn't find the subject claim from claim mappings ", e);
        }
    }
    return subject;
}
 
Example 2
Source File: OpenIDConnectAuthenticator.java    From carbon-identity with Apache License 2.0 5 votes vote down vote up
/**
 * @subject
 */
protected String getSubjectFromUserIDClaimURI(AuthenticationContext context) {
    String subject = null;
    try {
        subject = FrameworkUtils.getFederatedSubjectFromClaims(context, getClaimDialectURI());
    } catch (Exception e) {
        if(log.isDebugEnabled()) {
            log.debug("Couldn't find the subject claim from claim mappings ", e);
        }
    }
    return subject;
}
 
Example 3
Source File: FacebookAuthenticator.java    From carbon-identity with Apache License 2.0 5 votes vote down vote up
public void buildClaims(AuthenticationContext context, Map<String, Object> jsonObject)
        throws ApplicationAuthenticatorException {
    if (jsonObject != null) {
        Map<ClaimMapping, String> claims = new HashMap<ClaimMapping, String>();

        for (Map.Entry<String, Object> entry : jsonObject.entrySet()) {
            claims.put(ClaimMapping.build(entry.getKey(), entry.getKey(), null,
                    false), entry.getValue().toString());
            if (log.isDebugEnabled() &&
                    IdentityUtil.isTokenLoggable(IdentityConstants.IdentityTokens.USER_CLAIMS)) {
                log.debug("Adding claim mapping : " + entry.getKey() + " <> " + entry.getKey() + " : "
                        + entry.getValue());
            }

        }
        if (StringUtils.isBlank(context.getExternalIdP().getIdentityProvider().getClaimConfig().getUserClaimURI())) {
            context.getExternalIdP().getIdentityProvider().getClaimConfig().setUserClaimURI
                    (FacebookAuthenticatorConstants.EMAIL);
        }
        String subjectFromClaims = FrameworkUtils.getFederatedSubjectFromClaims(
                context.getExternalIdP().getIdentityProvider(), claims);
        if (subjectFromClaims != null && !subjectFromClaims.isEmpty()) {
            AuthenticatedUser authenticatedUser =
                    AuthenticatedUser.createFederateAuthenticatedUserFromSubjectIdentifier(subjectFromClaims);
            context.setSubject(authenticatedUser);
        } else {
            setSubject(context, jsonObject);
        }

        context.getSubject().setUserAttributes(claims);

    } else {
        if (log.isDebugEnabled()) {
            log.debug("Decoded json object is null");
        }
        throw new ApplicationAuthenticatorException("Decoded json object is null");
    }
}
 
Example 4
Source File: SAMLSSOAuthenticator.java    From carbon-identity with Apache License 2.0 4 votes vote down vote up
@Override
protected void processAuthenticationResponse(HttpServletRequest request, HttpServletResponse response,
                                             AuthenticationContext context)
        throws AuthenticationFailedException {

    try {
        SAML2SSOManager saml2SSOManager = getSAML2SSOManagerInstance();
        saml2SSOManager.init(context.getTenantDomain(), context.getAuthenticatorProperties(),
                context.getExternalIdP().getIdentityProvider());
        saml2SSOManager.processResponse(request);
        Map<ClaimMapping, String> receivedClaims = (Map<ClaimMapping, String>) request
                .getSession(false).getAttribute("samlssoAttributes");

        String subject = null;
        String idpSubject = null;
        String isSubjectInClaimsProp = context.getAuthenticatorProperties().get(
                IdentityApplicationConstants.Authenticator.SAML2SSO.IS_USER_ID_IN_CLAIMS);
        if ("true".equalsIgnoreCase(isSubjectInClaimsProp)) {
            subject = FrameworkUtils.getFederatedSubjectFromClaims(
                    context.getExternalIdP().getIdentityProvider(), receivedClaims);
            if (subject == null) {
                log.warn("Subject claim could not be found amongst attribute statements. " +
                        "Defaulting to Name Identifier.");
            }
        }
        idpSubject = (String) request.getSession().getAttribute("username");
        if (subject == null) {
            subject = idpSubject;
        }
        if (subject == null) {
            throw new SAMLSSOException("Cannot find federated User Identifier");
        }

        Object sessionIndexObj = request.getSession(false).getAttribute(SSOConstants.IDP_SESSION);
        String nameQualifier = (String) request.getSession().getAttribute(SSOConstants.NAME_QUALIFIER);
        String spNameQualifier = (String) request.getSession().getAttribute(SSOConstants.SP_NAME_QUALIFIER);
        String sessionIndex = null;

        if (sessionIndexObj != null) {
            sessionIndex = (String) sessionIndexObj;
        }

        StateInfo stateInfoDO = new StateInfo();
        stateInfoDO.setSessionIndex(sessionIndex);
        stateInfoDO.setSubject(subject);
        stateInfoDO.setNameQualifier(nameQualifier);
        stateInfoDO.setSpNameQualifier(spNameQualifier);
        context.setStateInfo(stateInfoDO);

        AuthenticatedUser authenticatedUser =
                AuthenticatedUser.createFederateAuthenticatedUserFromSubjectIdentifier(subject);
        authenticatedUser.setUserAttributes(receivedClaims);
        context.setSubject(authenticatedUser);
    } catch (SAMLSSOException e) {
        throw new AuthenticationFailedException(e.getMessage(), e);
    }
}