forked from Azard/egg-oauth2-server
/
oauth.js
52 lines (43 loc) · 1.13 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
'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;
}
* getClient(clientId, clientSecret) {
const client = nconf.get('client');
if (clientId !== client.clientId || clientSecret !== client.clientSecret) {
return;
}
return client;
}
* getUser(username, password) {
const user = nconf.get('user');
if (username !== user.username || password !== user.password) {
return;
}
return { userId: user.id };
}
* getAccessToken(bearerToken) {
const token = nconf.get('token');
const user = nconf.get('user');
const client = nconf.get('client');
token.user = user;
token.client = client;
return token;
}
* saveToken(token, client, user) {
const _token = Object.assign({}, token, { user }, { client });
nconf.set('token', _token);
nconf.save();
return _token;
}
}
return Model;
};