forked from laardee/serverless-authentication
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconfig.js
More file actions
71 lines (66 loc) · 2.04 KB
/
Copy pathconfig.js
File metadata and controls
71 lines (66 loc) · 2.04 KB
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
'use strict';
import { Utils } from './utils';
/**
* Config class
*/
class Config {
constructor() {
const data = process.env;
this.providers = {};
for (const key in data) {
if (data.hasOwnProperty(key)) {
const value = data[key];
const providerItem = (/PROVIDER_(.*)?_(.*)?/g).exec(key);
if (providerItem) {
const provider = providerItem[1].toLowerCase();
const type = providerItem[2].toLowerCase();
if (!this.providers[provider]) {
this.providers[provider] = {};
}
this.providers[provider][type] = value;
} else if (key === 'REDIRECT_URI') {
this.redirect_uri = value;
} else if (key == 'REDIRECT_METHOD') {
this.redirect_method = value;
} else if (key === 'REDIRECT_CLIENT_URI') {
this.redirect_client_uri = value;
} else if (key === 'TOKEN_SECRET') {
this.token_secret = value;
}
}
}
}
getConfig(provider) {
let result = {};
if (provider) {
const configProvider = provider.replace(/-/g, '_');
result = this.providers[configProvider] ? this.providers[configProvider] : {};
result.redirect_uri = Utils.redirectUrlBuilder(this.redirect_uri, provider);
result.redirect_client_uri = Utils.redirectUrlBuilder(this.redirect_client_uri, provider);
result.provider = provider;
}
result.token_secret = this.token_secret;
result.redirect_method = this.redirect_method;
return result;
}
}
/**
* @param provider {string} oauth provider name e.g. facebook or google
*/
export function config(options) {
let provider;
let host;
let stage;
// fallback -- remove on version upgrade
if (typeof (options) === 'string') {
provider = options;
} else {
provider = options.provider;
host = options.host;
stage = options.stage;
}
if (!process.env.REDIRECT_URI) {
process.env.REDIRECT_URI = `https://${host}/${stage}/authentication/callback/{provider}`;
}
return (new Config()).getConfig(provider);
}