-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfigureOAuth.js
169 lines (155 loc) · 5.68 KB
/
configureOAuth.js
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
163
164
165
166
167
168
169
const OAuth2Server = require("express-oauth-server");
const bodyParser = require("body-parser");
const express = require("express");
const authorizationCodes = new Map();
const accessTokens = new Map();
const refreshTokens = new Map();
/**
* Configure an Express app to fake an OAuth server
* @param app The app to configure
* @param options The configuration options for the OAuth server
*/
const configureOAuth = (
app = express(),
{
accessTokenLifetimeSeconds = 45,
refreshTokenLifetimeSeconds = 90,
redirectUri = "http://localhost:58816/ext-core-webapi/callback/LocalOAuth",
noExpiresIn = "false"
},
) => {
const log = (...args) => console.log((new Date()).toLocaleString(), ...args);
log("OAuth Configuration: ", {
accessTokenLifetimeSeconds,
refreshTokenLifetimeSeconds,
redirectUri,
noExpiresIn
});
const oauth = new OAuth2Server({
model: {
getClient: (id, secret) => {
const client = {
id: "myClientId",
redirectUris: [redirectUri],
grants: [
"authorization_code",
"client_credentials",
"password",
"refresh_token",
"implicit"
]
};
log("Got client ", client);
return client;
},
saveAuthorizationCode: (code, client, user) => {
log("saveAuthorizationCode", {
code,
client,
user
});
authorizationCodes.set(code.authorizationCode, {
...code,
client,
user
});
return code;
},
getAuthorizationCode: code => {
const existingCode = authorizationCodes.get(code);
log("getAuthorizationCode ", existingCode);
return existingCode;
},
saveToken: (token, client, user) => {
if (noExpiresIn === "true") {
delete token.accessTokenExpiresAt;
}
log("saveAccessToken ", token);
accessTokens.set(token.accessToken, { token, client, user });
if (token.refreshToken) {
log("saveRefreshToken ", token);
refreshTokens.set(token.refreshToken, {
token,
client,
user
});
}
return { ...token, client, user };
},
getAccessToken: accessToken => {
const existingToken = accessTokens.get(accessToken);
log("getAccessToken ", existingToken);
return existingToken;
},
getUser: (username, password) => {
log("getUser username ", username, " password ", password);
return { username, password };
},
getUserFromClient: client => {
log("getUserFromClient ", client);
return { userForClient: client };
},
verifyScope: (token, scope) => {
log("verifyScope", scope);
}, // exception will be thrown if this function isn't set
revokeAuthorizationCode: code => {
log("revokeAuthorizationCode ", code);
authorizationCodes.delete(code.authorizationCode);
return Promise.resolve(true);
},
revokeToken: token => {
if (token.accessToken != null) {
log("revokeAccessToken ", token);
accessTokens.delete(token.accessToken);
}
if (token.refreshToken != null) {
log("revokeRefreshToken ", token);
refreshTokens.delete(token.refreshToken);
}
return Promise.resolve(true);
},
getRefreshToken: refreshToken => {
const foundToken = Array.from(refreshTokens.values()).filter(
t =>
t.token.refreshToken === refreshToken &&
t.token.refreshTokenExpiresAt > new Date()
)[0];
log("getRefreshToken ", foundToken);
return foundToken;
}
},
allowBearerTokensInQueryString: true,
accessTokenLifetime: accessTokenLifetimeSeconds,
refreshTokenLifetime: refreshTokenLifetimeSeconds,
requireClientAuthentication: {
[undefined]: false
},
useErrorHandler: true,
allowEmptyState: true,
authorizationCodeLifetime: accessTokenLifetimeSeconds
});
app.use(bodyParser.text({type: ["text/*","application/xml"]}));
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());
app.use(({ url, body, headers }, res, next) => {
log("REQUEST: ", { url, body });
next();
});
app.oauth = oauth;
app.post("/token", app.oauth.token());
app.get("/token", app.oauth.token());
app.get("/authorize", (req, res, next) => {
const options = {
authenticateHandler: {
handle: data => {
return { id: "someUser" }; // everyone is someUser
}
}
};
log("Authorizing");
// Include options to override
oauth.authorize(options)(req, res, next);
});
return app;
};
module.exports = configureOAuth;