Skip to content

Commit

Permalink
fix memory store
Browse files Browse the repository at this point in the history
  • Loading branch information
davidgubler committed Jan 10, 2025
1 parent 77f6808 commit ff4d6ba
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 29 deletions.
38 changes: 38 additions & 0 deletions app/entities/memory/MemoryDynamicPassword.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package entities.memory;

import entities.DynamicPassword;

import java.util.Base64;

public class MemoryDynamicPassword implements DynamicPassword {
private long timestamp;
private String hash;

public MemoryDynamicPassword() {
// dummy for Morphia
}

public MemoryDynamicPassword(byte[] hash) {
this.timestamp = System.currentTimeMillis();
this.hash = Base64.getEncoder().encodeToString(hash);
}

@Override
public long getTimestamp() {
return timestamp;
}

@Override
public String getHashBase64() {
return hash;
}

@Override
public byte[] getHash() {
try {
return Base64.getDecoder().decode(hash);
} catch (Exception e) {
return null;
}
};
}
35 changes: 11 additions & 24 deletions app/entities/memory/MemoryServicePasswords.java
Original file line number Diff line number Diff line change
@@ -1,36 +1,18 @@
package entities.memory;

import entities.DynamicPassword;
import entities.ServicePasswords;

import java.util.ArrayList;
import java.util.Base64;
import java.util.List;

public class MemoryServicePasswords implements ServicePasswords {
private long dynamicPwTimestamp;
private String dynamicPwHash;
private String staticPwHash;
private List<MemoryDynamicPassword> dynamicPasswords = new ArrayList<>();

@Override
public long getDynamicPwTimestamp() {
return dynamicPwTimestamp;
}

@Override
public String getDynamicPwHashBase64() {
return dynamicPwHash;
}

@Override
public byte[] getDynamicPwHash() {
try {
return Base64.getDecoder().decode(dynamicPwHash);
} catch (Exception e) {
return null;
}
}

public void setDynamicPwHash(byte[] pwHash) {
this.dynamicPwTimestamp = System.currentTimeMillis();
this.dynamicPwHash = Base64.getEncoder().encodeToString(pwHash);
public void addDynamicPassword(MemoryDynamicPassword dynamicPassword) {
dynamicPasswords.add(dynamicPassword);
}

@Override
Expand All @@ -47,6 +29,11 @@ public byte[] getStaticPwHash() {
}
}

@Override
public List<? extends DynamicPassword> getDynamicPasswords() {
return dynamicPasswords;
}

public void setStaticPwHash(byte[] pwHash) {
this.staticPwHash = Base64.getEncoder().encodeToString(pwHash);
}
Expand Down
3 changes: 0 additions & 3 deletions app/store/memory/MemoryGroupsStore.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
package store.memory;

import dev.morphia.UpdateOptions;
import dev.morphia.query.updates.UpdateOperators;
import entities.Group;
import entities.GroupsMetadata;
import entities.memory.MemoryGroup;
import entities.mongodb.MongoDbGroup;
import store.GroupsStore;
import util.Config;

Expand Down
7 changes: 5 additions & 2 deletions app/store/memory/MemoryUsersStore.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import entities.Service;
import entities.User;
import entities.UserSession;
import entities.memory.MemoryDynamicPassword;
import entities.memory.MemoryServicePasswords;
import entities.memory.MemoryUser;
import entities.memory.MemoryUserSession;
Expand Down Expand Up @@ -152,13 +153,15 @@ public String generateDynamicPassword(User user, Service service) {
String password = IdGenerator.generateSessionId();
byte[] passwordHash = PasswordUtil.createStoragePassword(password, LdapSecurityConstants.HASH_METHOD_SSHA512);

MemoryDynamicPassword dynamicPassword = new MemoryDynamicPassword(passwordHash);

MemoryServicePasswords servicePasswords = (MemoryServicePasswords)memoryUser.getServicePasswords(service.getId());
if (servicePasswords == null) {
servicePasswords = new MemoryServicePasswords();
servicePasswords.setDynamicPwHash(passwordHash);
servicePasswords.addDynamicPassword(dynamicPassword);
memoryUser.setServicePasswords(service.getId(), servicePasswords);
} else {
servicePasswords.setDynamicPwHash(passwordHash);
servicePasswords.addDynamicPassword(dynamicPassword);
}

return password;
Expand Down

0 comments on commit ff4d6ba

Please sign in to comment.