-
Notifications
You must be signed in to change notification settings - Fork 15
/
provider.ts
156 lines (127 loc) Β· 3.62 KB
/
provider.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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
import { Shared } from './shared';
import { Common, Interceptors, Logger, URL, Dedupe } from '../../utils';
import { SalteAuthError } from './salte-auth-error';
export class Provider extends Shared {
protected logger: Logger;
public constructor(config: Provider.Config) {
super(config);
this.config = Common.defaults(this.config, {
validation: true,
level: 'warn'
});
this.logger = new Logger(`@salte-auth/salte-auth:providers/${this.$name}`, this.config.level);
}
/**
* Determines if validation is enabled for the given key.
* @param key - the key to determine whether validation is enabled for
* @returns whether validation is enabled for the key.
*/
protected validation(key: string): boolean {
if (typeof(this.config.validation) === 'object') {
return this.config.validation[key] === true;
}
return this.config.validation === true;
}
/**
* The unique name of the provider
*/
public get $name(): string {
return this.config.name || this.name;
}
/**
* Returns a scoped key for storage.
* @param key - The storage key.
*
* @example auth0.key('hello') // 'salte.auth.provider.auth0.hello'
*/
protected get key() {
return `salte.auth.provider.${this.$name}`;
}
protected url = URL.url;
public dedupe = Dedupe.dedupe();
/**
* Returns the logout url for the provider.
*/
public get logout(): string {
throw new SalteAuthError({
code: 'logout_not_supported',
message: `This provider doesn't support logout.`,
});
}
}
export interface Provider {
/**
* The unique name of the provider
*/
name: string;
/**
* Returns the login url for the provider.
*/
login: string;
/**
* An internal login command to `salte-auth` that enables enhancing the login with common parameters.
*/
$login(options?: object): string;
validate(options: object | void): void;
/**
* Checks for errors returned from the provider.
*/
$validate(options: object): void;
/**
* Reinitializes the ID and Access Tokens of the given provider.
*/
sync(): void;
/**
* Invoked when Salte Auth is initialized
*/
connected?(): void;
/**
* Invoked when an endpoint is marked as secured.
* @returns true if the endpoint is already secured, otherwise it returns a url to secure the endpoint.
*/
secure?(request?: Interceptors.XHR.ExtendedXMLHttpRequest | Request): Promise<'login' | boolean>;
on(name: 'login', listener: (error?: Error, data?: any) => void): void;
on(name: 'logout', listener: (error?: Error) => void): void;
}
export interface Provider {
config: Provider.Config;
};
export declare namespace Provider {
export interface Config extends Shared.Config {
/**
* The name associated with your provider
*/
name?: string;
/**
* The url of the designated provider.
*/
url?: string;
/**
* Used to disable certain security validations if your provider doesn't support them.
*
* @defaultValue true
*/
validation?: ValidationOptions | boolean;
/**
* The routes to secure for this provider.
*/
routes?: (string | RegExp)[] | boolean;
/**
* The endpoints to secure for this provider.
*/
endpoints?: (string | RegExp)[];
/**
* Determines the level of verbosity of the logs.
*
* @defaultValue 'warn'
*/
level?: ('error'|'warn'|'info'|'trace');
/**
* Add extra parameters to the given authentication request.
*/
queryParams?: (type: 'login' | 'logout') => { [key: string]: any };
}
export interface ValidationOptions {
[key: string]: boolean;
}
}