-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathauth.ts
124 lines (103 loc) · 3.01 KB
/
auth.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
import { AuthenticationRequiredError, NotImplementedError } from 'common-errors'
import { Microfleet } from '@microfleet/core'
import { ServiceRequest, ServiceActionHandler } from '../../types/router'
declare module '../../types/router' {
interface ServiceAction {
auth?: string | ServiceActionAuthGetName | ServiceActionAuthConfig
passAuthError?: boolean
}
}
export interface AuthInfo {
credentials: unknown
}
declare module '../../types/router' {
interface ServiceRequest {
auth?: AuthInfo | null
}
}
export type AuthStrategy = ServiceActionHandler
export interface AuthConfig {
readonly strategies: Record<string, AuthStrategy>
}
export interface AuthStrategyConfig {
name: string
authStrategy: 'required' | 'try'
passAuthError: boolean,
strategy: AuthStrategy | null
}
export interface ServiceActionAuthConfig {
name: string
passAuthError?: boolean
strategy?: 'required' | 'try'
}
function retrieveStrategy(request: ServiceRequest, strategies: AuthConfig['strategies']): AuthStrategyConfig {
const { action } = request
const { auth: authConfig } = action
// for ```MicrofleetAction.auth = (request) => assert(request.param)```
if (typeof authConfig === 'function') {
const name = authConfig(request)
return {
name,
authStrategy: 'required',
passAuthError: action.passAuthError || false,
strategy: strategies[name] || null
}
}
// for ```MicrofleetAction.auth = 'token'```
if (typeof authConfig === 'string') {
const name = authConfig
return {
name,
authStrategy: 'required',
passAuthError: action.passAuthError || false,
strategy: strategies[name] || null
}
}
// for ```MicrofleetAction.auth = {
// name: 'token',
// authStrategy: 'try',
// }```
if (typeof authConfig === 'object') {
const name = authConfig.name
return {
name,
authStrategy: authConfig.strategy || 'required',
passAuthError: authConfig.passAuthError || false,
strategy: strategies[name] || null
}
}
throw new Error(`authConfig is invalid: ${authConfig}`)
}
export default (config: AuthConfig) => async function authHandler(
this: Microfleet,
request: ServiceRequest,
): Promise<void> {
if (request.action.auth === undefined) {
return
}
// @todo avoid object creation
const authConfig = retrieveStrategy(request, config.strategies)
if (authConfig.strategy === null) {
throw new NotImplementedError(authConfig.name)
}
try {
// @todo make it deprecated, change request.auth inside
const credentials = await authConfig.strategy.call(this, request)
if (credentials) {
request.auth = { credentials }
}
} catch (error: any) {
// @todo const 'try'
if (authConfig.authStrategy === 'try') {
request.auth = null
} else {
if (authConfig.passAuthError) {
throw error
}
if (error.constructor === AuthenticationRequiredError) {
throw error
}
throw new AuthenticationRequiredError(error.message, error)
}
}
}