Implement token generation

This commit is contained in:
Dominik Korsa 2021-01-25 14:25:34 +01:00
parent ecb9ab21fd
commit 43e86c70cd
No known key found for this signature in database
GPG key ID: 546F986F71A6FE6E
5 changed files with 85 additions and 3 deletions

View file

@ -1,6 +1,7 @@
import type { Connection, Repository } from 'typeorm';
import { createConnection } from 'typeorm';
import Application from './entities/application';
import Token from './entities/token';
import User from './entities/user';
class Database {
@ -10,6 +11,8 @@ class Database {
public userRepo!: Repository<User>;
public tokenRepo!: Repository<Token>;
public async connect(): Promise<void> {
this.connection = await createConnection({
type: 'mongodb',
@ -19,12 +22,14 @@ class Database {
entities: [
Application,
User,
Token,
],
useUnifiedTopology: true,
logging: false,
});
this.applicationRepo = this.connection.getRepository(Application);
this.userRepo = this.connection.getRepository(User);
this.tokenRepo = this.connection.getRepository(Token);
}
}

View file

@ -0,0 +1,48 @@
import { nanoid } from 'nanoid';
import type { ObjectID } from 'typeorm';
import {
BaseEntity, Column, Entity, ObjectIdColumn,
} from 'typeorm';
@Entity()
export default class Token extends BaseEntity {
@ObjectIdColumn()
public _id!: ObjectID;
@Column()
public tokenId!: string;
@Column()
public creationDate!: Date;
@Column()
public studentIds!: number[];
@Column()
public scopes!: string[];
@Column()
public clientId!: string;
@Column()
public userId!: ObjectID;
@Column()
public tokenSecret!: string;
@Column()
public publicKey!: string;
@Column()
public encryptedPassword!: string;
@Column()
public encryptedSDK!: string;
@Column()
public encryptedPrivateKey!: string;
public static generateTokenId(): string {
return nanoid(20);
}
}

View file

@ -1,10 +1,12 @@
import type { FastifyReply } from 'fastify';
import { getCode, invalidateCode } from '../../codes';
import database from '../../database/database';
import Token from '../../database/entities/token';
import { ParamError } from '../../errors';
import type { CodeInfo, MyFastifyInstance } from '../../types';
import type { CodeInfo, MyFastifyInstance, TokenContent } from '../../types';
import {
encryptSymmetrical,
isObject, sha256, validateParam,
} from '../../utils';
@ -100,10 +102,33 @@ export default function registerToken(server: MyFastifyInstance): void {
}
}
// TODO: Generate and return token;
const tokenId = Token.generateTokenId();
const token = new Token();
token.tokenId = tokenId;
token.creationDate = new Date();
token.clientId = codeInfo.clientId;
token.scopes = codeInfo.scopes;
token.studentIds = codeInfo.studentIds;
token.tokenSecret = codeInfo.tokenSecret;
token.userId = codeInfo.userId;
token.encryptedPassword = codeInfo.encryptedPassword;
token.encryptedPrivateKey = codeInfo.encryptedPrivateKey;
token.encryptedSDK = codeInfo.encryptedSDK;
token.publicKey = codeInfo.publicKey;
await database.tokenRepo.save(token);
const content: TokenContent = {
tk: tokenKey,
};
invalidateCode(codeInfo.id);
await reply.code(500).send('Not implemented');
await reply.code(200).send({
access_token: `${tokenId}~${encryptSymmetrical(JSON.stringify(content), codeInfo.tokenSecret)}`,
token_type: 'bearer',
scope: codeInfo.scopes.join(' '),
});
return;
} catch (error) {
if (error instanceof ParamError) {

View file

@ -91,3 +91,7 @@ export interface CodeInfo {
export interface CodeContent {
tk: string;
}
export interface TokenContent {
tk: string;
}