use of org.platformlayer.auth.crypto.SecretStore in project platformlayer by platformlayer.
the class JoinProject method runCommand.
@Override
public Object runCommand() throws RepositoryException, IOException {
UserDatabase userRepository = getContext().getUserRepository();
UserEntity me = getContext().loginDirect();
ProjectEntity project = userRepository.findProjectByKey(projectKey.getKey());
if (project == null) {
throw new CliException("Project not found: " + projectKey.getKey());
}
SecretStore secretStore = new SecretStore(project.secretData);
CryptoKey projectSecret = secretStore.getSecretFromUser(me);
if (projectSecret == null) {
String msg = "Cannot retrieve project secret.";
msg += " Is " + me.key + " a member of " + project.getName() + "?";
throw new CliException(msg);
}
if (Strings.isNullOrEmpty(roleKey)) {
throw new CliException("Role is required");
}
RoleId role = new RoleId(roleKey);
userRepository.addUserToProject(username.getKey(), project.getName(), projectSecret, Collections.singletonList(role));
return project;
}
use of org.platformlayer.auth.crypto.SecretStore in project platformlayer by platformlayer.
the class JdbcUserRepository method grantProjectToProject.
@Override
@JdbcTransaction
public void grantProjectToProject(String grantToProjectKey, String onProjectKey, SecretKey onProjectSecret) throws RepositoryException {
DbHelper db = new DbHelper();
try {
ProjectEntity grantToProject = db.findProjectByKey(grantToProjectKey);
if (grantToProject == null) {
throw new RepositoryException("Project not found");
}
ProjectEntity onProject = db.findProjectByKey(onProjectKey);
if (onProject == null) {
throw new RepositoryException("Project not found");
}
byte[] projectSecretData = onProjectSecret.getEncoded();
PublicKey grantToProjectPublicKey = grantToProject.getPublicKey();
byte[] newSecretData;
try {
SecretStore store = new SecretStore(onProject.secretData);
Writer writer = store.buildWriter();
writer.writeAsymetricProjectKey(projectSecretData, grantToProject.id, grantToProjectPublicKey);
writer.close();
store.appendContents(writer);
newSecretData = store.getEncoded();
} catch (IOException e) {
throw new RepositoryException("Error writing secrets", e);
}
db.updateProjectSecret(onProject.id, newSecretData);
// db.insertUserProject(user.id, project.id);
} 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 ProjectEntity method unlockWithProject.
public void unlockWithProject(ProjectEntity project) {
SecretStore secretStore = new SecretStore(this.secretData);
this.projectSecret = secretStore.getSecretFromProject(project);
if (this.projectSecret == null) {
throw new SecurityException();
}
}
use of org.platformlayer.auth.crypto.SecretStore in project platformlayer by platformlayer.
the class ProjectEntity method unlockWithUser.
public void unlockWithUser(UserEntity user) {
SecretStore secretStore = new SecretStore(this.secretData);
this.projectSecret = secretStore.getSecretFromUser(user);
if (this.projectSecret == null) {
throw new SecurityException();
}
}
use of org.platformlayer.auth.crypto.SecretStore in project platformlayer by platformlayer.
the class SecretHelper method encodeItemSecret.
// TODO: We need to use the project secret, not the item secret
public byte[] encodeItemSecret(CryptoKey itemSecret) {
try {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
SecretStore.Writer writer = new SecretStore.Writer(baos);
byte[] plaintext = FathomdbCrypto.serialize(itemSecret);
for (int backend : keyStore.getBackends()) {
PublicKey publicKey = keyStore.findPublicKey(backend);
if (publicKey != null) {
writer.writeAsymetricSystemKey(plaintext, backend, publicKey);
} else {
throw new IllegalStateException();
}
}
for (ProjectAuthorization project : OpsContext.get().getEncryptingProjects()) {
if (project.isLocked()) {
throw new IllegalStateException();
// {
// UserInfo user = OpsContext.get().getUserInfo();
// ProjectId projectId = user.getProjectId();
// OpsProject project = userRepository.findProjectByKey(projectId.getKey());
// if (project == null) {
// throw new IllegalStateException("Project not found");
// }
//
// OpsUser opsUser = userRepository.findUser(user.getUserKey());
// if (project == null) {
// throw new IllegalStateException("User not found");
// }
//
// SecretStore secretStore = new SecretStore(project.secretData);
// projectKey = secretStore.getSecretFromUser(opsUser);
//
// project.unlockWithUser(opsUser);
//
// SecretKey projectSecret = project.getProjectSecret();
// }
}
writer.writeLockedByProjectKey(plaintext, project.getId(), project.getProjectSecret());
}
// for (int userId : keyStore.getProjectIds()) {
// SecretKey secretKey = keyStore.findUserSecret(userId);
// if (secretKey != null) {
// writer.writeLockedByUserKey(plaintext, userId, secretKey);
// } else {
// throw new IllegalStateException();
// }
// }
writer.close();
baos.close();
return baos.toByteArray();
} catch (IOException e) {
throw new IllegalStateException("Error serializing key", e);
}
}
Aggregations