-
Notifications
You must be signed in to change notification settings - Fork 112
Expand file tree
/
Copy pathauthenticator.js
More file actions
162 lines (150 loc) · 4.19 KB
/
authenticator.js
File metadata and controls
162 lines (150 loc) · 4.19 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
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
157
158
159
160
161
162
var fs = require('fs')
var configPath = 'data/strava_config'
/** @type {string | undefined} */
var token
/** @type {string | undefined} */
var clientId
/** @type {string | undefined} */
var clientSecret
/** @type {string | undefined} */
var redirectUri
/** @type {boolean} */
var configLoaded = false
/**
* Config object for Strava credentials (file, env, or passed directly).
* @typedef {{
* access_token?: string;
* client_id?: string;
* client_secret?: string;
* redirect_uri?: string;
* }} StravaConfig
*/
/**
* Reads config from the config file and populates module-level credentials.
* @returns {void}
*/
var readConfigFile = function () {
try {
var configStr = fs.readFileSync(configPath, { encoding: 'utf-8' })
var config = /** @type {StravaConfig} */ (JSON.parse(configStr))
if (config.access_token) token = config.access_token
if (config.client_id) clientId = config.client_id
if (config.client_secret) clientSecret = config.client_secret
if (config.redirect_uri) redirectUri = config.redirect_uri
} catch {
// Config file does not exist. This may be a valid case if the config is
// either passed directly as an argument or via environment variables
}
}
/**
* Reads config from environment variables and populates module-level credentials.
* @returns {void}
*/
var readEnvironmentVars = function () {
if (typeof process.env.STRAVA_ACCESS_TOKEN !== 'undefined') { token = process.env.STRAVA_ACCESS_TOKEN }
if (typeof process.env.STRAVA_CLIENT_ID !== 'undefined') { clientId = process.env.STRAVA_CLIENT_ID }
if (typeof process.env.STRAVA_CLIENT_SECRET !== 'undefined') { clientSecret = process.env.STRAVA_CLIENT_SECRET }
if (typeof process.env.STRAVA_REDIRECT_URI !== 'undefined') { redirectUri = process.env.STRAVA_REDIRECT_URI }
}
/**
* Loads config from the given object, or from file and env if config is omitted.
* @param {StravaConfig} [config] - Optional config object.
* @returns {void}
*/
var fetchConfig = function (config) {
if (config) {
if (config.access_token) token = config.access_token
if (config.client_id) clientId = config.client_id
if (config.client_secret) clientSecret = config.client_secret
if (config.redirect_uri) redirectUri = config.redirect_uri
} else {
readConfigFile()
readEnvironmentVars()
}
configLoaded = true
}
/**
* Authenticator module interface (fetchConfig, getToken, getClientId, etc.).
* @typedef {{
* fetchConfig: (config?: StravaConfig) => void;
* getToken: () => string | undefined;
* getClientId: () => string | undefined;
* getClientSecret: () => string | undefined;
* getRedirectUri: () => string | undefined;
* purge: () => void;
* }} Authenticator
*/
/** @type {Authenticator} */
module.exports = {
fetchConfig: fetchConfig,
/**
* Returns the current access token (loads config if needed).
* @returns {string | undefined}
*/
getToken: function () {
if (!configLoaded) {
fetchConfig()
}
if (token) {
return token
} else {
return undefined
}
},
/**
* Returns the Strava application client ID.
* @returns {string | undefined}
*/
getClientId: function () {
if (!configLoaded) {
fetchConfig()
}
if (clientId) {
return clientId
} else {
console.log('No client id found')
return undefined
}
},
/**
* Returns the Strava application client secret.
* @returns {string | undefined}
*/
getClientSecret: function () {
if (!configLoaded) {
fetchConfig()
}
if (clientSecret) {
return clientSecret
} else {
console.log('No client secret found')
return undefined
}
},
/**
* Returns the OAuth redirect URI.
* @returns {string | undefined}
*/
getRedirectUri: function () {
if (!configLoaded) {
fetchConfig()
}
if (redirectUri) {
return redirectUri
} else {
console.log('No redirectUri found')
return undefined
}
},
/**
* Clears all in-memory config (token, client id/secret, redirect URI).
* @returns {void}
*/
purge: function () {
token = undefined
clientId = undefined
clientSecret = undefined
redirectUri = undefined
configLoaded = false
}
}