tokens: Add token creation

Fix token deletion
Update plantuml docs
Add token validUntil and lastUsed fields

Signed-off-by: Philip Molares <philip.molares@udo.edu>
This commit is contained in:
Philip Molares 2021-01-21 19:37:43 +01:00 committed by David Mehren
parent cce1626c48
commit c9751404f7
No known key found for this signature in database
GPG key ID: 185982BA4C42B7C3
9 changed files with 113 additions and 35 deletions

View file

@ -4,7 +4,13 @@
* SPDX-License-Identifier: AGPL-3.0-only
*/
import { Column, CreateDateColumn, Entity, ManyToOne, PrimaryGeneratedColumn } from 'typeorm';
import {
Column,
CreateDateColumn,
Entity,
ManyToOne,
PrimaryGeneratedColumn,
} from 'typeorm';
import { User } from './user.entity';
@Entity()
@ -12,6 +18,9 @@ export class AuthToken {
@PrimaryGeneratedColumn()
id: number;
@Column({ unique: true })
keyId: string;
@ManyToOne((_) => User, (user) => user.authTokens)
user: User;
@ -24,21 +33,32 @@ export class AuthToken {
@Column({ unique: true })
accessToken: string;
@Column({ type: 'date' })
validUntil: Date;
@Column({
nullable: true,
})
validUntil: number;
@Column({
nullable: true,
})
lastUsed: number;
public static create(
user: User,
identifier: string,
keyId: string,
accessToken: string,
validUntil: Date,
validUntil?: number,
): Pick<AuthToken, 'user' | 'accessToken'> {
const newToken = new AuthToken();
newToken.user = user;
newToken.identifier = identifier;
newToken.keyId = keyId;
newToken.accessToken = accessToken;
newToken.createdAt = new Date();
newToken.validUntil = validUntil;
if (validUntil !== undefined) {
newToken.validUntil = validUntil;
}
return newToken;
}
}