Search in sources :

Example 6 with UserEntity

use of org.platformlayer.auth.UserEntity in project platformlayer by platformlayer.

the class KeystoneCliContext method loginDirect.

/**
	 * Logs in the current user, directly accessing the database
	 */
public UserEntity loginDirect() throws RepositoryException {
    String username = options.getUsername();
    String password = options.getPassword();
    if (username == null || password == null) {
        throw new IllegalArgumentException("Must specify username & password");
    }
    UserEntity user = (UserEntity) getUserRepository().authenticateWithPassword(username, password);
    if (user == null) {
        throw new SecurityException("Credentials were not valid");
    }
    return user;
}
Also used : UserEntity(org.platformlayer.auth.UserEntity)

Example 7 with UserEntity

use of org.platformlayer.auth.UserEntity in project platformlayer by platformlayer.

the class CreateProject method runCommand.

@Override
public Object runCommand() throws RepositoryException {
    UserDatabase userRepository = getContext().getUserRepository();
    // We need to login to unlock the user key so we can encrypt the project key!
    UserEntity me = getContext().loginDirect();
    if (projectKey.contains("@@")) {
        throw new CliException("Project names with @@ are reserved for system uses");
    }
    ProjectEntity project = userRepository.createProject(projectKey, me);
    return project;
}
Also used : CliException(com.fathomdb.cli.CliException) ProjectEntity(org.platformlayer.auth.ProjectEntity) UserDatabase(org.platformlayer.auth.UserDatabase) UserEntity(org.platformlayer.auth.UserEntity)

Example 8 with UserEntity

use of org.platformlayer.auth.UserEntity in project platformlayer by platformlayer.

the class ListProjects method runCommand.

@Override
public Object runCommand() throws RepositoryException {
    UserDatabase userRepository = getContext().getUserRepository();
    // if (username == null) {
    // return userRepository.listAllProjectNames(null);
    // } else {
    UserEntity user = (UserEntity) userRepository.findUser(username.getKey());
    if (user == null) {
        throw new IllegalArgumentException("User not found");
    }
    return userRepository.listProjectsByUserId(user.id);
// }
}
Also used : UserDatabase(org.platformlayer.auth.UserDatabase) UserEntity(org.platformlayer.auth.UserEntity)

Example 9 with UserEntity

use of org.platformlayer.auth.UserEntity in project platformlayer by platformlayer.

the class TokensResource method validateToken.

@GET
// @HEAD support is automatic from the @GET
@Path("{tokenId}")
public ValidateTokenResponse validateToken(@PathParam("tokenId") String checkToken, @QueryParam("project") String project) {
    try {
        requireSystemAccess();
    } catch (AuthenticatorException e) {
        log.warn("Error while checking system token", e);
        throwInternalError();
    }
    TokenInfo checkTokenInfo = tokenService.decodeToken(checkToken);
    if (checkTokenInfo == null || checkTokenInfo.hasExpired()) {
        throw404NotFound();
    }
    UserEntity userEntity = null;
    try {
        userEntity = userAuthenticator.getUserFromToken(checkTokenInfo.userId, checkTokenInfo.tokenSecret);
    } catch (AuthenticatorException e) {
        log.warn("Error while fetching user", e);
        throwInternalError();
    }
    ValidateTokenResponse response = new ValidateTokenResponse();
    response.access = new ValidateAccess();
    response.access.user = Mapping.mapToUserValidation(userEntity);
    response.access.token = new Token();
    response.access.token.expires = checkTokenInfo.expiration;
    response.access.token.id = checkToken;
    String checkProject = project;
    if (checkProject != null) {
        ProjectEntity projectEntity = null;
        try {
            projectEntity = userAuthenticator.findProject(checkProject);
        } catch (AuthenticatorException e) {
            log.warn("Error while fetching project", e);
            throwInternalError();
        }
        if (projectEntity == null) {
            throw404NotFound();
        }
        projectEntity.unlockWithUser(userEntity);
        if (!projectEntity.isSecretValid()) {
            throw404NotFound();
        }
        UserProjectEntity userProject = null;
        try {
            userProject = userAuthenticator.findUserProject(userEntity, projectEntity);
        } catch (AuthenticatorException e) {
            log.warn("Error while fetching project", e);
            throwInternalError();
        }
        if (userProject == null) {
            // Not a member of project
            throw404NotFound();
        }
        response.access.project = Mapping.mapToProject(projectEntity);
        response.access.project.roles = Mapping.mapToRoles(userProject.getRoles());
    }
    return response;
}
Also used : ValidateTokenResponse(org.platformlayer.auth.model.ValidateTokenResponse) ValidateAccess(org.platformlayer.auth.model.ValidateAccess) UserProjectEntity(org.platformlayer.auth.UserProjectEntity) ProjectEntity(org.platformlayer.auth.ProjectEntity) AuthenticatorException(org.platformlayer.auth.AuthenticatorException) Token(org.platformlayer.auth.model.Token) UserProjectEntity(org.platformlayer.auth.UserProjectEntity) TokenInfo(org.platformlayer.auth.services.TokenInfo) UserEntity(org.platformlayer.auth.UserEntity) Path(javax.ws.rs.Path) GET(javax.ws.rs.GET)

Example 10 with UserEntity

use of org.platformlayer.auth.UserEntity in project platformlayer by platformlayer.

the class KeystoneRepositoryAuthenticator method getUserFromToken.

@Override
public UserEntity getUserFromToken(String userIdString, byte[] tokenSecret) throws AuthenticatorException {
    int userId;
    try {
        userId = Integer.parseInt(userIdString);
    } catch (NumberFormatException e) {
        throw new AuthenticatorException("Invalid user id", e);
    }
    if (tokenSecret.length < 1) {
        throw new IllegalArgumentException();
    }
    CryptoKey userSecret = authenticationSecrets.decryptSecretFromToken(tokenSecret);
    if (userSecret == null) {
        throw new AuthenticatorException("Authentication timed out");
    }
    UserEntity user;
    try {
        user = repository.findUserById(userId);
    } catch (RepositoryException e) {
        throw new AuthenticatorException("Error while authenticating user", e);
    }
    user.unlock(userSecret);
    if (user.isLocked()) {
        return null;
    }
    return user;
}
Also used : CryptoKey(com.fathomdb.crypto.CryptoKey) AuthenticatorException(org.platformlayer.auth.AuthenticatorException) RepositoryException(org.platformlayer.RepositoryException) UserEntity(org.platformlayer.auth.UserEntity)

Aggregations

UserEntity (org.platformlayer.auth.UserEntity)10 AuthenticatorException (org.platformlayer.auth.AuthenticatorException)5 ProjectEntity (org.platformlayer.auth.ProjectEntity)4 UserDatabase (org.platformlayer.auth.UserDatabase)3 CliException (com.fathomdb.cli.CliException)2 CryptoKey (com.fathomdb.crypto.CryptoKey)2 RepositoryException (org.platformlayer.RepositoryException)2 UserProjectEntity (org.platformlayer.auth.UserProjectEntity)2 ValidateAccess (org.platformlayer.auth.model.ValidateAccess)2 ValidateTokenResponse (org.platformlayer.auth.model.ValidateTokenResponse)2 GET (javax.ws.rs.GET)1 POST (javax.ws.rs.POST)1 Path (javax.ws.rs.Path)1 CustomerFacingException (org.platformlayer.CustomerFacingException)1 CertificateAuthenticationRequest (org.platformlayer.auth.CertificateAuthenticationRequest)1 CertificateAuthenticationResponse (org.platformlayer.auth.CertificateAuthenticationResponse)1 OpsUser (org.platformlayer.auth.OpsUser)1 SecretStore (org.platformlayer.auth.crypto.SecretStore)1 AuthenticateResponse (org.platformlayer.auth.model.AuthenticateResponse)1 RegistrationResponse (org.platformlayer.auth.model.RegistrationResponse)1