Skip to content

Commit

Permalink
ch 11, ex 1 - fixed the issue with nosql (oauthinaction#14)
Browse files Browse the repository at this point in the history
  • Loading branch information
artursudnik committed Oct 25, 2019
1 parent ce732ae commit 64e5585
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 21 deletions.
39 changes: 19 additions & 20 deletions exercises/ch-11-ex-1/authorizationServer.js
Original file line number Diff line number Diff line change
Expand Up @@ -247,28 +247,27 @@ app.post("/token", function(req, res){
return;
}
} else if (req.body.grant_type == 'refresh_token') {
nosql.one(function(token) {
if (token.refresh_token == req.body.refresh_token) {
return token;
}
}, function(err, token) {
if (token) {
console.log("We found a matching refresh token: %s", req.body.refresh_token);
if (token.client_id != clientId) {
nosql.remove(function(found) { return (found == token); }, function () {} );
nosql.one().make(builder => {
builder.where('refresh_token', req.body.refresh_token);
builder.callback(function(err, token) {
if (token) {
console.log("We found a matching refresh token: %s", req.body.refresh_token);
if (token.client_id != clientId) {
nosql.remove(function(found) { return (found == token); }, function () {} );
res.status(400).json({error: 'invalid_grant'});
return;
}
var access_token = randomstring.generate();
nosql.insert({ access_token: access_token, client_id: clientId });
var token_response = { access_token: access_token, token_type: 'Bearer', refresh_token: token.refresh_token };
res.status(200).json(token_response);
return;
} else {
console.log('No matching token was found.');
res.status(400).json({error: 'invalid_grant'});
return;
}
var access_token = randomstring.generate();
nosql.insert({ access_token: access_token, client_id: clientId });
var token_response = { access_token: access_token, token_type: 'Bearer', refresh_token: token.refresh_token };
res.status(200).json(token_response);
return;
} else {
console.log('No matching token was found.');
res.status(400).json({error: 'invalid_grant'});
return;
}
});
});
} else {
console.log('Unknown grant type %s', req.body.grant_type);
Expand Down Expand Up @@ -315,4 +314,4 @@ var server = app.listen(9001, 'localhost', function () {

console.log('OAuth Authorization Server is listening at http://%s:%s', host, port);
});

2 changes: 1 addition & 1 deletion exercises/ch-11-ex-1/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"consolidate": "^0.13.1",
"qs": "^4.0.0",
"randomstring": "^1.0.7",
"nosql": "^3.0.3",
"nosql": "^6.1.0",
"base64url": "^1.0.4",
"cors": "^2.7.1",
"jsrsasign": "^5.0.0"
Expand Down

0 comments on commit 64e5585

Please sign in to comment.