I forgot about access keys

was lazy today sorry
This commit is contained in:
Minecon724 2024-08-22 19:43:31 +02:00
parent eec5450159
commit 63072a8af0
Signed by: Minecon724
GPG key ID: 3CCC4D267742C8E8
6 changed files with 77 additions and 29 deletions

View 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;
}

View file

@ -44,7 +44,7 @@ public class UsersResource {
return Json.createObjectBuilder() return Json.createObjectBuilder()
.add("masterKey", censoredKey) .add("masterKey", censoredKey)
.add("role", account.role) .add("role", account.role)
//.add("accessKeys", user.accessKeys.size()) .add("accessKeys", account.accessKeys.size())
.build(); .build();
} }
} }

View 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
}
}

View file

@ -1,7 +1,6 @@
package eu.m724.auth.master; package eu.m724.auth.master;
import eu.m724.orm.AccessKey; import eu.m724.orm.AccessKey;
import eu.m724.orm.AccessLimits;
import eu.m724.orm.Account; import eu.m724.orm.Account;
import jakarta.enterprise.context.ApplicationScoped; import jakarta.enterprise.context.ApplicationScoped;
import jakarta.transaction.Transactional; import jakarta.transaction.Transactional;
@ -71,21 +70,4 @@ public class AccountService {
add(key, role); add(key, role);
return Base64.getEncoder().encodeToString(key); 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);
}
} }

View file

@ -1,23 +1,25 @@
package eu.m724.orm; package eu.m724.orm;
import io.quarkus.hibernate.orm.panache.PanacheEntity; import io.quarkus.hibernate.orm.panache.PanacheEntity;
import jakarta.persistence.Column;
import jakarta.persistence.ManyToOne; import jakarta.persistence.ManyToOne;
//@Entity //@Entity
public class AccessKey extends PanacheEntity { 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 * raw bytes of this key, it's provided to users in base64
*/ */
@Column(unique = true)
public byte[] key; public byte[] key;
/** /**
* access limits of this key * access limits of this key
*/ */
public AccessLimits accessLimits; public AccessLimits accessLimits;
/**
* the user owning this access key
*/
@ManyToOne
public Account account;
} }

View file

@ -2,12 +2,13 @@ package eu.m724.orm;
import io.quarkus.hibernate.orm.panache.PanacheEntity; import io.quarkus.hibernate.orm.panache.PanacheEntity;
import io.quarkus.security.jpa.Roles; import io.quarkus.security.jpa.Roles;
import jakarta.persistence.CascadeType;
import jakarta.persistence.Column; import jakarta.persistence.Column;
import jakarta.persistence.Entity; import jakarta.persistence.Entity;
import jakarta.persistence.OneToMany; import jakarta.persistence.OneToMany;
import java.util.ArrayList; import java.util.HashSet;
import java.util.List; import java.util.Set;
// TODO organize all this like work on variable names move functions etc // TODO organize all this like work on variable names move functions etc
@ -16,8 +17,8 @@ public class Account extends PanacheEntity {
@Column(unique = true) @Column(unique = true)
public byte[] masterKey; public byte[] masterKey;
@OneToMany @OneToMany(mappedBy = "account", cascade = CascadeType.ALL, orphanRemoval = true)
public List<AccessKey> accessKeys = new ArrayList<>(); public Set<AccessKey> accessKeys = new HashSet<>();
@Roles @Roles
public String role = "user"; public String role = "user";