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;
}
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();
}
}
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;
}
Aggregations