I forgot about access keys
was lazy today sorry
This commit is contained in:
parent
eec5450159
commit
63072a8af0
6 changed files with 77 additions and 29 deletions
21
src/main/java/eu/m724/KeysResource.java
Normal file
21
src/main/java/eu/m724/KeysResource.java
Normal file
|
@ -0,0 +1,21 @@
|
|||
package eu.m724;
|
||||
|
||||
import eu.m724.auth.master.AccountService;
|
||||
import io.quarkus.security.identity.SecurityIdentity;
|
||||
import jakarta.inject.Inject;
|
||||
import jakarta.ws.rs.Path;
|
||||
import jakarta.ws.rs.Produces;
|
||||
import jakarta.ws.rs.core.MediaType;
|
||||
|
||||
/**
|
||||
* managing access keys (not master keys or accounts)
|
||||
*/
|
||||
@Path("/api/keys")
|
||||
@Produces(MediaType.APPLICATION_JSON)
|
||||
public class KeysResource {
|
||||
@Inject
|
||||
SecurityIdentity securityIdentity;
|
||||
|
||||
@Inject
|
||||
AccountService accountService;
|
||||
}
|
|
@ -44,7 +44,7 @@ public class UsersResource {
|
|||
return Json.createObjectBuilder()
|
||||
.add("masterKey", censoredKey)
|
||||
.add("role", account.role)
|
||||
//.add("accessKeys", user.accessKeys.size())
|
||||
.add("accessKeys", account.accessKeys.size())
|
||||
.build();
|
||||
}
|
||||
}
|
||||
|
|
42
src/main/java/eu/m724/auth/master/AccessKeyService.java
Normal file
42
src/main/java/eu/m724/auth/master/AccessKeyService.java
Normal file
|
@ -0,0 +1,42 @@
|
|||
package eu.m724.auth.master;
|
||||
|
||||
import eu.m724.orm.AccessKey;
|
||||
import eu.m724.orm.AccessLimits;
|
||||
import eu.m724.orm.Account;
|
||||
import jakarta.enterprise.context.ApplicationScoped;
|
||||
import jakarta.transaction.Transactional;
|
||||
|
||||
import java.security.SecureRandom;
|
||||
import java.util.Base64;
|
||||
|
||||
@ApplicationScoped
|
||||
public class AccessKeyService {
|
||||
private final SecureRandom random = new SecureRandom();
|
||||
|
||||
/**
|
||||
* generates an access key for an account
|
||||
* @param account the account
|
||||
* @param accessLimits access limits
|
||||
* @return base64 encoded access key
|
||||
*/
|
||||
@Transactional
|
||||
public String createAccessKey(Account account, AccessLimits accessLimits) {
|
||||
byte[] key = new byte[18];
|
||||
random.nextBytes(key);
|
||||
|
||||
AccessKey accessKey = new AccessKey();
|
||||
accessKey.key = key;
|
||||
accessKey.account = account;
|
||||
accessKey.accessLimits = accessLimits;
|
||||
|
||||
account.accessKeys.add(accessKey);
|
||||
account.persist();
|
||||
|
||||
return Base64.getEncoder().encodeToString(key);
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public void deleteAccessKey(AccessKey accessKey) {
|
||||
accessKey.account = null; // TODO hopefully that works
|
||||
}
|
||||
}
|
|
@ -1,7 +1,6 @@
|
|||
package eu.m724.auth.master;
|
||||
|
||||
import eu.m724.orm.AccessKey;
|
||||
import eu.m724.orm.AccessLimits;
|
||||
import eu.m724.orm.Account;
|
||||
import jakarta.enterprise.context.ApplicationScoped;
|
||||
import jakarta.transaction.Transactional;
|
||||
|
@ -71,21 +70,4 @@ public class AccountService {
|
|||
add(key, role);
|
||||
return Base64.getEncoder().encodeToString(key);
|
||||
}
|
||||
|
||||
/**
|
||||
* generates an access key for an account
|
||||
* @return base64 encoded access key
|
||||
*/
|
||||
public String createAccessKey(Account account, AccessLimits accessLimits) {
|
||||
byte[] key = new byte[18];
|
||||
random.nextBytes(key);
|
||||
|
||||
AccessKey accessKey = new AccessKey();
|
||||
accessKey.key = key;
|
||||
accessKey.account = account;
|
||||
accessKey.accessLimits = accessLimits;
|
||||
accessKey.persist();
|
||||
|
||||
return Base64.getEncoder().encodeToString(key);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,23 +1,25 @@
|
|||
package eu.m724.orm;
|
||||
|
||||
import io.quarkus.hibernate.orm.panache.PanacheEntity;
|
||||
import jakarta.persistence.Column;
|
||||
import jakarta.persistence.ManyToOne;
|
||||
|
||||
//@Entity
|
||||
public class AccessKey extends PanacheEntity {
|
||||
/**
|
||||
* the user owning this access key
|
||||
*/
|
||||
@ManyToOne
|
||||
public Account account;
|
||||
|
||||
/**
|
||||
* raw bytes of this key, it's provided to users in base64
|
||||
*/
|
||||
@Column(unique = true)
|
||||
public byte[] key;
|
||||
|
||||
/**
|
||||
* access limits of this key
|
||||
*/
|
||||
public AccessLimits accessLimits;
|
||||
|
||||
/**
|
||||
* the user owning this access key
|
||||
*/
|
||||
@ManyToOne
|
||||
public Account account;
|
||||
}
|
||||
|
|
|
@ -2,12 +2,13 @@ package eu.m724.orm;
|
|||
|
||||
import io.quarkus.hibernate.orm.panache.PanacheEntity;
|
||||
import io.quarkus.security.jpa.Roles;
|
||||
import jakarta.persistence.CascadeType;
|
||||
import jakarta.persistence.Column;
|
||||
import jakarta.persistence.Entity;
|
||||
import jakarta.persistence.OneToMany;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
|
||||
// TODO organize all this like work on variable names move functions etc
|
||||
|
||||
|
@ -16,8 +17,8 @@ public class Account extends PanacheEntity {
|
|||
@Column(unique = true)
|
||||
public byte[] masterKey;
|
||||
|
||||
@OneToMany
|
||||
public List<AccessKey> accessKeys = new ArrayList<>();
|
||||
@OneToMany(mappedBy = "account", cascade = CascadeType.ALL, orphanRemoval = true)
|
||||
public Set<AccessKey> accessKeys = new HashSet<>();
|
||||
|
||||
@Roles
|
||||
public String role = "user";
|
||||
|
|
Loading…
Reference in a new issue