Implement token generation
This commit is contained in:
parent
ecb9ab21fd
commit
43e86c70cd
5 changed files with 85 additions and 3 deletions
|
@ -1,6 +1,7 @@
|
||||||
import type { Connection, Repository } from 'typeorm';
|
import type { Connection, Repository } from 'typeorm';
|
||||||
import { createConnection } from 'typeorm';
|
import { createConnection } from 'typeorm';
|
||||||
import Application from './entities/application';
|
import Application from './entities/application';
|
||||||
|
import Token from './entities/token';
|
||||||
import User from './entities/user';
|
import User from './entities/user';
|
||||||
|
|
||||||
class Database {
|
class Database {
|
||||||
|
@ -10,6 +11,8 @@ class Database {
|
||||||
|
|
||||||
public userRepo!: Repository<User>;
|
public userRepo!: Repository<User>;
|
||||||
|
|
||||||
|
public tokenRepo!: Repository<Token>;
|
||||||
|
|
||||||
public async connect(): Promise<void> {
|
public async connect(): Promise<void> {
|
||||||
this.connection = await createConnection({
|
this.connection = await createConnection({
|
||||||
type: 'mongodb',
|
type: 'mongodb',
|
||||||
|
@ -19,12 +22,14 @@ class Database {
|
||||||
entities: [
|
entities: [
|
||||||
Application,
|
Application,
|
||||||
User,
|
User,
|
||||||
|
Token,
|
||||||
],
|
],
|
||||||
useUnifiedTopology: true,
|
useUnifiedTopology: true,
|
||||||
logging: false,
|
logging: false,
|
||||||
});
|
});
|
||||||
this.applicationRepo = this.connection.getRepository(Application);
|
this.applicationRepo = this.connection.getRepository(Application);
|
||||||
this.userRepo = this.connection.getRepository(User);
|
this.userRepo = this.connection.getRepository(User);
|
||||||
|
this.tokenRepo = this.connection.getRepository(Token);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
48
backend/src/database/entities/token.ts
Normal file
48
backend/src/database/entities/token.ts
Normal 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);
|
||||||
|
}
|
||||||
|
}
|
|
@ -1,10 +1,12 @@
|
||||||
import type { FastifyReply } from 'fastify';
|
import type { FastifyReply } from 'fastify';
|
||||||
import { getCode, invalidateCode } from '../../codes';
|
import { getCode, invalidateCode } from '../../codes';
|
||||||
import database from '../../database/database';
|
import database from '../../database/database';
|
||||||
|
import Token from '../../database/entities/token';
|
||||||
import { ParamError } from '../../errors';
|
import { ParamError } from '../../errors';
|
||||||
import type { CodeInfo, MyFastifyInstance } from '../../types';
|
import type { CodeInfo, MyFastifyInstance, TokenContent } from '../../types';
|
||||||
|
|
||||||
import {
|
import {
|
||||||
|
encryptSymmetrical,
|
||||||
isObject, sha256, validateParam,
|
isObject, sha256, validateParam,
|
||||||
} from '../../utils';
|
} 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);
|
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;
|
return;
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
if (error instanceof ParamError) {
|
if (error instanceof ParamError) {
|
||||||
|
|
|
@ -91,3 +91,7 @@ export interface CodeInfo {
|
||||||
export interface CodeContent {
|
export interface CodeContent {
|
||||||
tk: string;
|
tk: string;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export interface TokenContent {
|
||||||
|
tk: string;
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in a new issue