-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.js
60 lines (49 loc) · 1.66 KB
/
app.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
'use strict';
module.exports = app => {
const parameters = ['username', 'password'];
// For OAuth 2.0 Token Exchange you can specify allowedDuplicateParameters as ['audience', 'resource']
const allowedDuplicateParameters = [];
app.oidcProvider.registerGrantType('password', function (providerInstance) {
return async (ctx, next) => {
const account = await ctx.app.model.Account.findOne({
where: {
username: ctx.oidc.params.username,
password: ctx.oidc.params.password
}
});
if (account) {
if (!account.client_ids || !(account.client_ids.indexOf(ctx.oidc.client.clientId) >= 0)) {
ctx.body = {
error: 'access_denied',
error_description: 'You don\'t have permission to access',
};
ctx.status = 403;
} else {
const AccessToken = providerInstance.AccessToken;
const at = new AccessToken({
gty: 'password',
claims: ctx.oidc.claims,
accountId: account.id,
scope: ctx.oidc.params.scope,
clientId: ctx.oidc.client.clientId,
grantId: ctx.oidc.uuid,
});
const accessToken = await at.save();
const expiresIn = AccessToken.expiresIn;
ctx.body = {
access_token: accessToken,
expires_in: expiresIn,
token_type: 'Bearer',
};
}
} else {
ctx.body = {
error: 'invalid_grant',
error_description: 'invalid credentials provided',
};
ctx.status = 400;
}
await next();
};
}, parameters, allowedDuplicateParameters);
}