server/database/entities/Application.ts

77 lines
2 KiB
TypeScript
Raw Normal View History

2023-09-12 22:48:10 +02:00
import { BaseEntity, Column, Entity, PrimaryGeneratedColumn } from "typeorm";
import { APIApplication } from "~types/entities/application";
2023-09-22 00:42:59 +02:00
import { Token } from "./Token";
2023-09-12 22:48:10 +02:00
2023-09-28 20:19:21 +02:00
/**
* Represents an application that can authenticate with the API.
2023-09-12 22:48:10 +02:00
*/
@Entity({
name: "applications",
})
export class Application extends BaseEntity {
2023-09-28 20:19:21 +02:00
/** The unique identifier for this application. */
2023-09-12 22:48:10 +02:00
@PrimaryGeneratedColumn("uuid")
id!: string;
2023-09-28 20:19:21 +02:00
/** The name of this application. */
2023-09-12 22:48:10 +02:00
@Column("varchar")
name!: string;
2023-09-28 20:19:21 +02:00
/** The website associated with this application, if any. */
2023-09-12 22:48:10 +02:00
@Column("varchar", {
nullable: true,
})
website!: string | null;
2023-09-28 20:19:21 +02:00
/** The VAPID key associated with this application, if any. */
2023-09-12 22:48:10 +02:00
@Column("varchar", {
nullable: true,
})
vapid_key!: string | null;
2023-09-28 20:19:21 +02:00
/** The client ID associated with this application. */
2023-09-14 04:25:45 +02:00
@Column("varchar")
client_id!: string;
2023-09-28 20:19:21 +02:00
/** The secret associated with this application. */
2023-09-13 21:11:24 +02:00
@Column("varchar")
secret!: string;
2023-09-28 20:19:21 +02:00
/** The scopes associated with this application. */
2023-09-13 21:11:24 +02:00
@Column("varchar")
scopes = "read";
2023-09-28 20:19:21 +02:00
/** The redirect URIs associated with this application. */
2023-09-13 21:11:24 +02:00
@Column("varchar")
redirect_uris = "urn:ietf:wg:oauth:2.0:oob";
2023-09-28 20:19:21 +02:00
/**
* Retrieves the application associated with the given access token.
* @param token The access token to retrieve the application for.
* @returns The application associated with the given access token, or null if no such application exists.
*/
static async getFromToken(token: string): Promise<Application | null> {
2023-09-22 00:42:59 +02:00
const dbToken = await Token.findOne({
where: {
access_token: token,
},
2023-09-22 03:41:12 +02:00
relations: ["application"],
2023-09-22 00:42:59 +02:00
});
return dbToken?.application || null;
}
2023-09-28 20:19:21 +02:00
/**
* Converts this application to an API application.
* @returns The API application representation of this application.
*/
2023-09-12 22:48:10 +02:00
// eslint-disable-next-line @typescript-eslint/require-await
async toAPI(): Promise<APIApplication> {
return {
2023-09-13 21:11:24 +02:00
name: this.name,
website: this.website,
vapid_key: this.vapid_key,
2023-09-13 02:29:13 +02:00
};
2023-09-12 22:48:10 +02:00
}
2023-09-13 02:29:13 +02:00
}