-
-
Notifications
You must be signed in to change notification settings - Fork 768
/
Copy pathpat-service.ts
110 lines (91 loc) · 3.09 KB
/
pat-service.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
import {
type IAuditUser,
type IUnleashConfig,
type IUnleashStores,
PatCreatedEvent,
PatDeletedEvent,
} from '../types';
import type { Logger } from '../logger';
import type { IPatStore } from '../types/stores/pat-store';
import crypto from 'crypto';
import BadDataError from '../error/bad-data-error';
import NameExistsError from '../error/name-exists-error';
import { OperationDeniedError } from '../error/operation-denied-error';
import { PAT_LIMIT } from '../util/constants';
import type EventService from '../features/events/event-service';
import type { CreatePatSchema, PatSchema } from '../openapi';
export default class PatService {
private config: IUnleashConfig;
private logger: Logger;
private patStore: IPatStore;
private eventService: EventService;
constructor(
{ patStore }: Pick<IUnleashStores, 'patStore'>,
config: IUnleashConfig,
eventService: EventService,
) {
this.config = config;
this.logger = config.getLogger('services/pat-service.ts');
this.patStore = patStore;
this.eventService = eventService;
}
async createPat(
pat: CreatePatSchema,
forUserId: number,
auditUser: IAuditUser,
): Promise<PatSchema> {
await this.validatePat(pat, forUserId);
const secret = this.generateSecretKey();
const newPat = await this.patStore.create(pat, secret, forUserId);
await this.eventService.storeEvent(
new PatCreatedEvent({
data: { ...pat, secret: '***' },
auditUser,
}),
);
return { ...newPat, secret };
}
async getAll(userId: number): Promise<PatSchema[]> {
return this.patStore.getAllByUser(userId);
}
async deletePat(
id: number,
forUserId: number,
auditUser: IAuditUser,
): Promise<void> {
const pat = await this.patStore.get(id);
await this.eventService.storeEvent(
new PatDeletedEvent({
data: { ...pat, secret: '***' },
auditUser,
}),
);
return this.patStore.deleteForUser(id, forUserId);
}
async validatePat(
{ description, expiresAt }: CreatePatSchema,
userId: number,
): Promise<void> {
if (!description) {
throw new BadDataError('PAT description cannot be empty.');
}
if (new Date(expiresAt) < new Date()) {
throw new BadDataError('The expiry date should be in future.');
}
if ((await this.patStore.countByUser(userId)) >= PAT_LIMIT) {
throw new OperationDeniedError(
`Too many PATs (${PAT_LIMIT}) already exist for this user.`,
);
}
if (
await this.patStore.existsWithDescriptionByUser(description, userId)
) {
throw new NameExistsError('PAT description already exists.');
}
}
private generateSecretKey() {
const randomStr = crypto.randomBytes(28).toString('hex');
return `user:${randomStr}`;
}
}
module.exports = PatService;