Search in sources :

Example 1 with Auth0User

use of com.enonic.app.auth0.impl.user.Auth0User in project app-auth0-idprovider by enonic.

the class Auth0LoginService method updateProfile.

private void updateProfile(final Auth0User auth0User, final PrincipalKey principalKey) {
    final UpdateUserParams updateUserParams = UpdateUserParams.create().userKey(principalKey).editor(editableUser -> this.updateProfile(editableUser.profile, auth0User)).build();
    runAs(() -> securityService.updateUser(updateUserParams), RoleKeys.ADMIN);
}
Also used : IdProviderKey(com.enonic.xp.security.IdProviderKey) UpdateUserParams(com.enonic.xp.security.UpdateUserParams) QueryParser(com.enonic.xp.query.parser.QueryParser) Auth0User(com.enonic.app.auth0.impl.user.Auth0User) Callable(java.util.concurrent.Callable) UserQuery(com.enonic.xp.security.UserQuery) HttpServletRequest(javax.servlet.http.HttpServletRequest) Component(org.osgi.service.component.annotations.Component) ContextAccessor(com.enonic.xp.context.ContextAccessor) Map(java.util.Map) JsonNode(com.fasterxml.jackson.databind.JsonNode) ContextBuilder(com.enonic.xp.context.ContextBuilder) VerifiedUsernameAuthToken(com.enonic.xp.security.auth.VerifiedUsernameAuthToken) QueryExpr(com.enonic.xp.query.expr.QueryExpr) SecurityService(com.enonic.xp.security.SecurityService) PropertyTree(com.enonic.xp.data.PropertyTree) HttpSession(javax.servlet.http.HttpSession) CreateUserParams(com.enonic.xp.security.CreateUserParams) User(com.enonic.xp.security.User) PropertySet(com.enonic.xp.data.PropertySet) PrincipalRelationship(com.enonic.xp.security.PrincipalRelationship) ConstraintExpr(com.enonic.xp.query.expr.ConstraintExpr) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) AuthenticationInfo(com.enonic.xp.security.auth.AuthenticationInfo) PrincipalKey(com.enonic.xp.security.PrincipalKey) RoleKeys(com.enonic.xp.security.RoleKeys) Reference(org.osgi.service.component.annotations.Reference) PrincipalKeys(com.enonic.xp.security.PrincipalKeys) UpdateUserParams(com.enonic.xp.security.UpdateUserParams)

Example 2 with Auth0User

use of com.enonic.app.auth0.impl.user.Auth0User in project app-auth0-idprovider by enonic.

the class Auth0LoginService method login.

public void login(final HttpServletRequest request, final Auth0User auth0User, final IdProviderKey idProviderKey) {
    // Retrieves the user by key
    final String userId = auth0User.getUserId().replace('|', '-');
    final PrincipalKey principalKey = PrincipalKey.ofUser(idProviderKey, userId);
    User user = runAs(() -> securityService.getUser(principalKey), RoleKeys.AUTHENTICATED).orElse(null);
    // If the user does not exist with this id
    if (user == null && auth0User.getEmail() != null) {
        // Retrieves the user by email
        final ConstraintExpr constraintExpr = QueryParser.parseCostraintExpression("userstorekey = '" + idProviderKey + "' AND email = '" + auth0User.getEmail() + "'");
        final QueryExpr queryExpr = QueryExpr.from(constraintExpr);
        final UserQuery userQuery = UserQuery.create().size(1).queryExpr(queryExpr).build();
        user = (User) runAs(() -> securityService.query(userQuery), RoleKeys.AUTHENTICATED).getUsers().first();
    }
    // If the user does not exist
    if (user == null) {
        // Creates the user
        user = createUser(auth0User, principalKey);
    }
    // Updates the profile
    updateProfile(auth0User, user.getKey());
    // Authenticates the user
    authenticate(request, user.getKey());
}
Also used : Auth0User(com.enonic.app.auth0.impl.user.Auth0User) User(com.enonic.xp.security.User) ConstraintExpr(com.enonic.xp.query.expr.ConstraintExpr) QueryExpr(com.enonic.xp.query.expr.QueryExpr) UserQuery(com.enonic.xp.security.UserQuery) PrincipalKey(com.enonic.xp.security.PrincipalKey)

Example 3 with Auth0User

use of com.enonic.app.auth0.impl.user.Auth0User in project app-auth0-idprovider by enonic.

the class Auth0LoginService method createUser.

private User createUser(final Auth0User auth0User, final PrincipalKey principalKey) {
    final String email = auth0User.getEmail();
    final String name = auth0User.getName();
    final PrincipalKeys defaultPrincipals = configurationService.getDefaultPrincipals(principalKey.getIdProviderKey());
    final CreateUserParams createUserParams = CreateUserParams.create().login(principalKey.getId()).displayName(name).email(email).userKey(principalKey).build();
    return runAs(() -> {
        final User user = securityService.createUser(createUserParams);
        for (PrincipalKey defaultPrincipal : defaultPrincipals) {
            securityService.addRelationship(PrincipalRelationship.from(defaultPrincipal).to(principalKey));
        }
        return user;
    }, RoleKeys.ADMIN);
}
Also used : CreateUserParams(com.enonic.xp.security.CreateUserParams) PrincipalKeys(com.enonic.xp.security.PrincipalKeys) Auth0User(com.enonic.app.auth0.impl.user.Auth0User) User(com.enonic.xp.security.User) PrincipalKey(com.enonic.xp.security.PrincipalKey)

Aggregations

Auth0User (com.enonic.app.auth0.impl.user.Auth0User)3 PrincipalKey (com.enonic.xp.security.PrincipalKey)3 User (com.enonic.xp.security.User)3 ConstraintExpr (com.enonic.xp.query.expr.ConstraintExpr)2 QueryExpr (com.enonic.xp.query.expr.QueryExpr)2 CreateUserParams (com.enonic.xp.security.CreateUserParams)2 PrincipalKeys (com.enonic.xp.security.PrincipalKeys)2 UserQuery (com.enonic.xp.security.UserQuery)2 ContextAccessor (com.enonic.xp.context.ContextAccessor)1 ContextBuilder (com.enonic.xp.context.ContextBuilder)1 PropertySet (com.enonic.xp.data.PropertySet)1 PropertyTree (com.enonic.xp.data.PropertyTree)1 QueryParser (com.enonic.xp.query.parser.QueryParser)1 IdProviderKey (com.enonic.xp.security.IdProviderKey)1 PrincipalRelationship (com.enonic.xp.security.PrincipalRelationship)1 RoleKeys (com.enonic.xp.security.RoleKeys)1 SecurityService (com.enonic.xp.security.SecurityService)1 UpdateUserParams (com.enonic.xp.security.UpdateUserParams)1 AuthenticationInfo (com.enonic.xp.security.auth.AuthenticationInfo)1 VerifiedUsernameAuthToken (com.enonic.xp.security.auth.VerifiedUsernameAuthToken)1