forked from MetinSeylan/Nestjs-OpenTelemetry
-
Notifications
You must be signed in to change notification settings - Fork 0
/
guard.injector.ts
129 lines (113 loc) · 4.74 KB
/
guard.injector.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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
import { CanActivate, Injectable, Logger, Type } from '@nestjs/common'
import { APP_GUARD } from '@nestjs/core'
import { GUARDS_METADATA } from '@nestjs/common/constants'
import { AttributeNames, NestScope } from '../../open-telemetry.enums'
import { EnhancerInjector } from './enhancer.injector'
@Injectable()
export class GuardInjector extends EnhancerInjector {
private readonly logger = new Logger(GuardInjector.name)
public inject(): void {
this.injectGlobals()
const controllers = this.getControllers()
for (const controller of controllers) {
if (this.isGuarded(controller.metatype)) {
const guards = this.getGuards(controller.metatype)
.map((guard) => {
const wrappedGuard = this.wrapEnhancer(guard)
const prototype = typeof wrappedGuard === 'function' ? wrappedGuard['prototype'] : wrappedGuard
if (this.isAffected(prototype.canActivate))
return wrappedGuard
const traceName = `Guard -> ${controller.name}.${prototype.constructor.name}`
prototype.canActivate = this.wrap(prototype.canActivate, traceName, {
attributes: {
[AttributeNames.MODULE]: controller.host?.name,
[AttributeNames.CONTROLLER]: controller.name,
[AttributeNames.GUARD]: prototype.constructor.name,
[AttributeNames.SCOPE]: NestScope.CONTROLLER,
[AttributeNames.INJECTOR]: GuardInjector.name,
},
})
if (typeof wrappedGuard === 'function' && typeof guard === 'function')
this.resolveWrappedEnhancer(controller.host!, guard, wrappedGuard)
this.logger.log(`Mapped ${traceName}`)
return wrappedGuard
})
if (guards.length > 0)
Reflect.defineMetadata(GUARDS_METADATA, guards, controller.metatype)
}
const keys = this.metadataScanner.getAllMethodNames(
controller.metatype.prototype,
)
for (const key of keys) {
if (
this.isGuarded(controller.metatype.prototype[key])
) {
const guards = this.getGuards(controller.metatype.prototype[key]).map(
(guard) => {
const wrappedGuard = this.wrapEnhancer(guard)
const prototype = typeof wrappedGuard === 'function' ? wrappedGuard['prototype'] : wrappedGuard
if (this.isAffected(prototype.canActivate))
return wrappedGuard
const traceName = `Guard -> ${controller.name}.${controller.metatype.prototype[key].name}.${prototype.constructor.name}`
prototype.canActivate = this.wrap(
prototype.canActivate,
traceName,
{
attributes: {
[AttributeNames.MODULE]: controller.host?.name,
[AttributeNames.CONTROLLER]: controller.name,
[AttributeNames.GUARD]: prototype.constructor.name,
[AttributeNames.SCOPE]: NestScope.METHOD,
[AttributeNames.PROVIDER_METHOD]: controller.metatype.prototype[key].name,
[AttributeNames.INJECTOR]: GuardInjector.name,
},
},
)
if (typeof wrappedGuard === 'function' && typeof guard === 'function')
this.resolveWrappedEnhancer(controller.host!, guard, wrappedGuard)
this.logger.log(`Mapped ${traceName}`)
return wrappedGuard
},
)
if (guards.length > 0) {
Reflect.defineMetadata(
GUARDS_METADATA,
guards,
controller.metatype.prototype[key],
)
}
}
}
}
}
private injectGlobals() {
const providers = this.getProviders()
for (const provider of providers) {
if (
typeof provider.token === 'string'
&& provider.token.includes(APP_GUARD)
&& !this.isAffected(provider.metatype.prototype.canActivate)
) {
const traceName = `Guard -> Global -> ${provider.metatype.name}`
provider.metatype.prototype.canActivate = this.wrap(
provider.metatype.prototype.canActivate,
traceName,
{
attributes: {
[AttributeNames.GUARD]: provider.metatype.name,
[AttributeNames.SCOPE]: NestScope.GLOBAL,
[AttributeNames.INJECTOR]: GuardInjector.name,
},
},
)
this.logger.log(`Mapped ${traceName}`)
}
}
}
private getGuards(prototype: object): Type<CanActivate>[] {
return Reflect.getMetadata(GUARDS_METADATA, prototype) || []
}
private isGuarded(prototype: object): boolean {
return Reflect.hasMetadata(GUARDS_METADATA, prototype)
}
}