Search in sources :

Example 6 with SecretStore

use of org.platformlayer.auth.crypto.SecretStore in project platformlayer by platformlayer.

the class SecretHelper method getSecret.

// public SecretKey decodeSecret(byte[] encoded) {
// SecretStoreDecoder visitor = new SecretStoreDecoder() {
// @Override
// public void visitAsymetricSystemKey(int keyId, byte[] data) {
// PrivateKey privateKey = keyStore.findPrivateKey(keyId);
// if (privateKey != null) {
// setSecretKey(decryptAsymetricKey(privateKey, data));
// }
// }
//
// @Override
// public void visitUserKey(int userId, byte[] data) {
// SecretKey userKey = keyStore.findUserSecret(userId);
// if (userKey != null) {
// setSecretKey(decryptSymetricKey(userKey, data));
// }
// }
// };
//
// try {
// SecretStore.read(encoded, visitor);
// } catch (IOException e) {
// throw new IllegalArgumentException("Error deserializing secret", e);
// }
//
// SecretKey secretKey = visitor.getSecretKey();
// if (secretKey == null)
// throw new IllegalArgumentException("Cannot decrypt secret");
// return secretKey;
//
// }
// public byte[] decryptSecret(byte[] data, byte[] secret) {
// CryptoKey secretKey = getSecret(secret);
//
// return FathomdbCrypto.decrypt(secretKey, data);
// }
public CryptoKey getSecret(byte[] secret) {
    SecretStore secretStore = new SecretStore(secret);
    CryptoKey secretKey = null;
    for (ProjectAuthorization project : OpsContext.get().getEncryptingProjects()) {
        secretKey = secretStore.getSecretFromProject(project);
        if (secretKey != null) {
            break;
        }
    }
    if (secretKey == null) {
        throw new SecurityException();
    }
    return secretKey;
}
Also used : CryptoKey(com.fathomdb.crypto.CryptoKey) ProjectAuthorization(org.platformlayer.model.ProjectAuthorization) SecretStore(org.platformlayer.auth.crypto.SecretStore)

Example 7 with SecretStore

use of org.platformlayer.auth.crypto.SecretStore in project platformlayer by platformlayer.

the class JdbcUserRepository method addUserToProject.

@Override
@JdbcTransaction
public void addUserToProject(String username, String projectKey, CryptoKey projectSecret, List<RoleId> roles) throws RepositoryException {
    DbHelper db = new DbHelper();
    try {
        UserEntity user = db.findUserByKey(username);
        if (user == null) {
            throw new RepositoryException("User not found");
        }
        ProjectEntity project = db.findProjectByKey(projectKey);
        if (project == null) {
            throw new RepositoryException("Project not found");
        }
        byte[] projectSecretData = FathomdbCrypto.serialize(projectSecret);
        PublicKey userPublicKey = user.getPublicKey();
        byte[] newSecretData;
        try {
            SecretStore store = new SecretStore(project.secretData);
            Writer writer = store.buildWriter();
            writer.writeAsymetricUserKey(projectSecretData, user.id, userPublicKey);
            writer.close();
            store.appendContents(writer);
            newSecretData = store.getEncoded();
        } catch (IOException e) {
            throw new RepositoryException("Error writing secrets", e);
        }
        db.updateProjectSecret(project.id, newSecretData);
        UserProjectEntity userProjectEntity = new UserProjectEntity();
        userProjectEntity.userId = user.id;
        userProjectEntity.projectId = project.id;
        userProjectEntity.addRoles(roles);
        db.insertUserProject(userProjectEntity);
    } catch (SQLException e) {
        throw new RepositoryException("Error reading groups", e);
    } finally {
        db.close();
    }
}
Also used : SQLException(java.sql.SQLException) PublicKey(java.security.PublicKey) RepositoryException(org.platformlayer.RepositoryException) IOException(java.io.IOException) SecretStore(org.platformlayer.auth.crypto.SecretStore) Writer(org.platformlayer.auth.crypto.SecretStore.Writer) JdbcTransaction(com.fathomdb.jdbc.JdbcTransaction)

Example 8 with SecretStore

use of org.platformlayer.auth.crypto.SecretStore in project platformlayer by platformlayer.

the class UserEntity method unlockWithPassword.

public CryptoKey unlockWithPassword(String password) {
    SecretStore secretStore = new SecretStore(secret);
    this.userSecret = secretStore.getSecretFromPassword(id, password);
    if (this.userSecret == null) {
        throw new SecurityException();
    }
    return this.userSecret;
}
Also used : SecretStore(org.platformlayer.auth.crypto.SecretStore)

Aggregations

SecretStore (org.platformlayer.auth.crypto.SecretStore)8 IOException (java.io.IOException)3 PublicKey (java.security.PublicKey)3 CryptoKey (com.fathomdb.crypto.CryptoKey)2 JdbcTransaction (com.fathomdb.jdbc.JdbcTransaction)2 SQLException (java.sql.SQLException)2 RepositoryException (org.platformlayer.RepositoryException)2 Writer (org.platformlayer.auth.crypto.SecretStore.Writer)2 ProjectAuthorization (org.platformlayer.model.ProjectAuthorization)2 CliException (com.fathomdb.cli.CliException)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 ProjectEntity (org.platformlayer.auth.ProjectEntity)1 UserDatabase (org.platformlayer.auth.UserDatabase)1 UserEntity (org.platformlayer.auth.UserEntity)1 RoleId (org.platformlayer.model.RoleId)1