scope storage

This commit is contained in:
Tom Moor committed 2025-01-24 22:45:32 -05:00
1 parent 08385b8a9e
commit a95005776f
4 files changed
+41 -1

No files matched your search

+12 -1
View File
@@ -6,11 +6,16 @@ import Field from "./decorators/Field";
class ApiKey extends ParanoidModel {
static modelName = "ApiKey";
/** The user chosen name of the API key. */
/** The human-readable name of this API key */
@Field
@observable
name: string;
/** A space-separated list of scopes that this API key has access to */
@Field
@observable
scope?: string;
/** An optional datetime that the API key expires. */
@Field
@observable
@@ -29,6 +34,12 @@ class ApiKey extends ParanoidModel {
/** A preview of the last 4 characters of the API key. */
last4: string;
/** An array of scopes that this API key has access to. */
@computed
get scopes() {
return this.scope?.split(" ") ?? [];
}
/** Whether the API key has an expiry in the past. */
@computed
get isExpired() {
@@ -0,0 +1,19 @@
"use strict";
/** @type {import('sequelize-cli').Migration} */
module.exports = {
async up(queryInterface, Sequelize) {
await queryInterface.sequelize.transaction(async transaction => {
await queryInterface.addColumn("apiKeys", "scope", {
type: Sequelize.STRING,
allowNull: true,
}, { transaction });
});
},
async down(queryInterface) {
await queryInterface.sequelize.transaction(async transaction => {
await queryInterface.removeColumn("apiKeys", "scope", { transaction });
});
},
};
+9
View File
@@ -1,4 +1,5 @@
import crypto from "crypto";
import { Matches } from "class-validator";
import { subMinutes } from "date-fns";
import randomstring from "randomstring";
import { InferAttributes, InferCreationAttributes, Op } from "sequelize";
@@ -31,6 +32,7 @@ class ApiKey extends ParanoidModel<
static eventNamespace = "api_keys";
/** The human-readable name of this API key */
@Length({
min: ApiKeyValidation.minNameLength,
max: ApiKeyValidation.maxNameLength,
@@ -39,6 +41,11 @@ class ApiKey extends ParanoidModel<
@Column
name: string;
/** A space-separated list of scopes that this API key has access to */
@Matches(/[\.\w\s]*/)
@Column
scope: string | null;
/** @deprecated The plain text value of the API key, removed soon. */
@Unique
@Column
@@ -59,10 +66,12 @@ class ApiKey extends ParanoidModel<
@SkipChangeset
last4: string;
/** The date and time when this API key will expire */
@IsDate
@Column
expiresAt: Date | null;
/** The date and time when this API key was last used */
@IsDate
@Column
@SkipChangeset
+1
View File
@@ -5,6 +5,7 @@ export default function presentApiKey(apiKey: ApiKey) {
id: apiKey.id,
userId: apiKey.userId,
name: apiKey.name,
scope: apiKey.scope,
value: apiKey.value,
last4: apiKey.last4,
createdAt: apiKey.createdAt,