Skip to content

Commit 94707ef

Browse files
committed
fix: implement password grant correctly
1 parent e42ee81 commit 94707ef

File tree

2 files changed

+13
-21
lines changed

2 files changed

+13
-21
lines changed

databases/memory/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ app.use(bodyParser.urlencoded({ extended: false }));
1515

1616
app.post('/token', app.oauth.token());
1717

18-
app.use('/secret', app.oauth.authorize(), function(req, res) {
18+
app.use('/secret', app.oauth.authenticate(), function(req, res) {
1919
console.debug('secret')
2020
res.send('Secret area');
2121
});

databases/memory/model.js

Lines changed: 12 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@ function InMemoryCache() {
1616
*/
1717

1818
InMemoryCache.prototype.dump = function() {
19+
this.users.length = 0;
20+
this.clients.length = 0;
21+
this.accessToken.clear();
22+
this.refreshToken.clear();
1923
console.log('clients', this.clients);
2024
console.log('tokens', this.tokens);
2125
console.log('users', this.users);
@@ -26,23 +30,18 @@ InMemoryCache.prototype.dump = function() {
2630
*/
2731

2832
InMemoryCache.prototype.getAccessToken = function(bearerToken) {
29-
let tokens = this.tokens.filter(function(token) {
30-
return token.accessToken === bearerToken;
31-
});
32-
33-
return tokens.length ? tokens[0] : false;
33+
const entry = { ...this.accessToken.get(bearerToken) };
34+
if (!entry) return null;
35+
entry.user = this.users.find(u => u.id === entry.userId);
36+
return entry
3437
};
3538

3639
/**
3740
* Get refresh token.
3841
*/
3942

4043
InMemoryCache.prototype.getRefreshToken = function(bearerToken) {
41-
let tokens = this.tokens.filter(function(token) {
42-
return token.refreshToken === bearerToken;
43-
});
44-
45-
return tokens.length ? tokens[0] : false;
44+
return this.refreshToken.get(bearerToken);
4645
};
4746

4847
/**
@@ -51,9 +50,10 @@ InMemoryCache.prototype.getRefreshToken = function(bearerToken) {
5150

5251
InMemoryCache.prototype.getClient = function(clientId, clientSecret) {
5352
let clients = this.clients.filter(function(client) {
54-
return client.clientId === clientId && client.clientSecret === clientSecret;
53+
return clientSecret !== null
54+
? client.clientId === clientId && client.clientSecret === clientSecret
55+
: client.clientId === clientId;
5556
});
56-
5757
return clients.length ? clients[0] : false;
5858
};
5959

@@ -87,14 +87,6 @@ InMemoryCache.prototype.saveToken = function(token, client, user) {
8787
return token;
8888
};
8989

90-
InMemoryCache.prototype.saveAuthorizationCode = function (code, client, user) {
91-
this.authCodes.push({ code, client, user });
92-
};
93-
94-
InMemoryCache.prototype.getAuthorizationCode = function (authorizationCode) {
95-
return this.authCodes.find(doc => doc.code.authorization_code === authorizationCode);
96-
};
97-
9890
/*
9991
* Get user.
10092
*/

0 commit comments

Comments
 (0)