-
Notifications
You must be signed in to change notification settings - Fork 45
/
Copy pathoauth.js
54 lines (45 loc) · 1.29 KB
/
oauth.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
'use strict';
const path = require('path');
const nconf = require('nconf');
module.exports = app => {
// Mock Data
nconf.use('file', {
file: path.join(app.config.baseDir, 'app/mock/db.json'),
});
class Model {
constructor(ctx) {
this.ctx = ctx;
}
async getClient(clientId, clientSecret) {
const client = nconf.get('client');
if (clientId !== client.clientId || clientSecret !== client.clientSecret) {
return;
}
return client;
}
async getUser(username, password) {
const user = nconf.get('user');
if (username !== user.username || password !== user.password) {
return;
}
return { userId: user.id };
}
async getAccessToken(bearerToken) {
const token = nconf.get('token');
token.accessTokenExpiresAt = new Date(token.accessTokenExpiresAt);
token.refreshTokenExpiresAt = new Date(token.refreshTokenExpiresAt);
const user = nconf.get('user');
const client = nconf.get('client');
token.user = user;
token.client = client;
return token;
}
async saveToken(token, client, user) {
const _token = Object.assign({}, token, { user }, { client });
nconf.set('token', _token);
nconf.save();
return _token;
}
}
return Model;
};