forked from openiap/opencore
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOAuthProvider.ts
More file actions
221 lines (216 loc) · 8.91 KB
/
Copy pathOAuthProvider.ts
File metadata and controls
221 lines (216 loc) · 8.91 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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
import * as OAuthServer from "oauth2-server";
import * as winston from "winston";
import * as express from "express";
import { TokenUser } from "openflow-api";
const Request = OAuthServer.Request;
const Response = OAuthServer.Response;
export class OAuthProvider {
private _logger: winston.Logger;
private app: express.Express;
public static instance: OAuthProvider = null;
private clients = [{
id: 'application', // TODO: Needed by refresh_token grant, because there is a bug at line 103 in https://github.com/oauthjs/node-oauth2-server/blob/v3.0.1/lib/grant-types/refresh-token-grant-type.js (used client.id instead of client.clientId)
clientId: 'application',
clientSecret: 'secret',
grants: [
'password',
'refresh_token',
'authorization_code'
],
redirectUris: []
}];
private tokens = [];
private codes = {};
public oauthServer: any = null;
private authorizationCodeStore: any = {};
static configure(logger: winston.Logger, app: express.Express): OAuthProvider {
var instance = new OAuthProvider();
try {
OAuthProvider.instance = instance;
instance._logger = logger;
instance.app = app;
instance.oauthServer = new OAuthServer({
model: instance,
grants: ['authorization_code', 'refresh_token'],
accessTokenLifetime: 60 * 60 * 24, // 24 hours, or 1 day
allowEmptyState: true,
allowExtendedTokenAttributes: true
});
(app as any).oauth = instance.oauthServer;
app.all('/oauth/token', instance.obtainToken.bind(instance));
app.get('/oauth/login', (req, res) => {
let GRAFANA_URI = "http://localhost.openrpa.dk:3000";
let state = req.params.state;
if (state == null) state = encodeURIComponent(req.query.state as any);
const access_type = req.query.access_type;
const client_id = req.query.client_id;
const redirect_uri = req.query.redirect_uri;
const response_type = req.query.response_type;
const scope = req.query.scope;
if (req.user) {
// TODO: Add logic for configurering redirect url's !!!!!!!!
if (instance.clients[0].redirectUris.indexOf(redirect_uri) == -1) {
instance.clients[0].redirectUris.push(redirect_uri);
}
instance._logger.info("[OAuth][" + (req.user as any).username + "] /oauth/login " + state);
instance.codes["cc536d98d27750394a87ab9d057016e636a8ac31"] = req.user;
instance.codes["cc536d98d27750394a87ab9d057016e636a8ac31"].redirect_uri = redirect_uri;
// res.redirect(`${GRAFANA_URI}/login/generic_oauth?state=${state}&code=cc536d98d27750394a87ab9d057016e636a8ac31`);
res.redirect(`${redirect_uri}?state=${state}&code=cc536d98d27750394a87ab9d057016e636a8ac31`);
} else {
instance._logger.info("[OAuth][anon] /oauth/login " + state);
res.redirect(`https://localhost.openrpa.dk`);
}
});
// app.get('/oauth/authorize', instance.authorize.bind(instance));
app.all('/oauth/authorize', (req, res) => {
var request = new Request(req);
var response = new Response(res);
return instance.oauthServer.authenticate(request, response)
.then((token) => {
res.json(token.user);
}).catch((err) => {
console.error(err);
res.status(err.code || 500).json(err);
});
});
// app.all('/oauth/authorize', instance.oauthServer.authenticate.bind(instance));
// app.all('/oauth/authorize/emails', instance.oauthServer.authenticate.bind(instance));
} catch (error) {
console.error(error);
var json = JSON.stringify(error, null, 3);
console.error(json);
throw error;
}
return instance;
}
authorize(req, res) {
this._logger.info("[OAuth] authorize");
var request = new Request(req);
var response = new Response(res);
console.log(request.headers);
return this.oauthServer.authorize(request, response)
.then((token) => {
res.json(token);
}).catch((err) => {
console.error(err);
res.status(err.code || 500).json(err);
});
}
authenticateHandler() {
return {
handle: (request, response) => {
//in this example, we store the logged-in user as the 'loginUser' attribute in session
if (request.session.loginUser) {
return { username: request.session.loginUser.username };
}
return null;
}
};
}
obtainToken(req, res) {
this._logger.info("[OAuth] obtainToken");
var request = new Request(req);
var response = new Response(res);
return this.oauthServer.token(request, response)
.then((token) => {
this._logger.info("[OAuth] obtainToken::success: token:");
res.json(token);
}).catch((err) => {
this._logger.info("[OAuth] obtainToken::failed: token:");
console.error(err);
res.status(err.code || 500).json(err);
});
}
public getAccessToken(bearerToken) {
this._logger.info("[OAuth] getAccessToken " + bearerToken);
var tokens = this.tokens.filter((token) => {
return token.accessToken === bearerToken;
});
return tokens.length ? tokens[0] : false;
}
public getRefreshToken(bearerToken) {
this._logger.info("[OAuth] getRefreshToken " + bearerToken);
var tokens = this.tokens.filter((token) => {
return token.refreshToken === bearerToken;
});
return tokens.length ? tokens[0] : false;
}
public getClient(clientId, clientSecret) {
this._logger.info("[OAuth] getClient " + clientId);
var clients = this.clients.filter((client) => {
return client.clientId === clientId && client.clientSecret === clientSecret;
});
return clients.length ? clients[0] : false;
}
public saveToken(token, client, user) {
this._logger.info("[OAuth] saveToken " + token);
var result = {
accessToken: token.accessToken,
access_token: token.accessToken,
accessTokenExpiresAt: token.accessTokenExpiresAt,
clientId: client.clientId,
refreshToken: token.refreshToken,
refresh_token: token.refreshToken,
refreshTokenExpiresAt: token.refreshTokenExpiresAt,
userId: user.id,
user: user,
client: this.clients[0]
};
this.tokens.push(result);
return result;
}
saveAuthorizationCode(code, client, user) {
this._logger.info("[OAuth] saveAuthorizationCode " + code);
var codeToSave: any = this.codes[code];
codeToSave = {
'authorizationCode': code.authorizationCode,
'expiresAt': code.expiresAt,
'redirectUri': code.redirectUri,
'scope': code.scope,
'client': client.id,
'user': user.username
};
this.codes[code] = codeToSave;
code = Object.assign({}, code, {
'client': client.id,
'user': user.username
});
return code;
}
getAuthorizationCode(code) {
this._logger.info("[OAuth] getAuthorizationCode " + code);
var user: TokenUser = this.codes[code];
if (user == null) return null;
var redirect_uri = (user as any).redirect_uri;
var expiresAt = new Date();
expiresAt.setMonth(expiresAt.getMonth() + 1);
var role = "Viewer";
user = TokenUser.From(user);
if (user.HasRoleName("admins")) role = "Admin";
var result = {
code: code,
client: this.clients[0],
user: {
id: user._id,
_id: user._id,
name: user.name,
username: user.username,
email: user.username,
role: role
},
expiresAt: expiresAt,
redirectUri: redirect_uri
}
// Viewer, Editor, Admin
// return result;
return result;
}
revokeAuthorizationCode(code) {
this._logger.info("[OAuth] revokeAuthorizationCode " + code);
return true;
// var user: TokenUser = this.codes[code];
// if (user != null) delete this.codes[code];
// return code;
}
}