Skip to content
This repository has been archived by the owner on May 2, 2023. It is now read-only.

Improve authorization. #26

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
103 changes: 65 additions & 38 deletions config/http.js
Original file line number Diff line number Diff line change
Expand Up @@ -257,13 +257,7 @@ module.exports.http = {
callback(null, token);
});
} else {
token.token = gcapi.generateUID(256);

token.save(function (err) {
if (err) return callback(err);

callback(null, token);
});
callback(null, token);
}


Expand Down Expand Up @@ -368,42 +362,75 @@ module.exports.http = {
});
}),
function (req, res) {
if (req.oauth2.client.redirectURI !== req.oauth2.req.redirectURI) {
return res.json(400, {
error: "wrong redirect_uri",
documentation_url: docs_url
});
}
async.waterfall([
function check4ActiveTokens(callback) {
Token.findOne({
clientId: req.oauth2.client.id,
userId: req.user.id,
scope: req.oauth2.client.scope
}).exec(function (err, token) {
if (err) return callback(err);

var scopes;
if (req.oauth2.client.scope.split(',') === req.oauth2.client.scope) {
scopes = req.oauth2.client.scope
} else {
scopes = req.oauth2.client.scope.split(',')
}
if (token) {
Authcode.create({
code: gcapi.generateUID(32),
clientId: req.oauth2.client.id,
redirectURI: req.oauth2.client.redirectURI,
userId: req.user.id,
scope: req.oauth2.client.scope
}).exec(function (err, code) {
if (err) {
return res.serverError();
}

res.redirect(req.oauth2.client.redirectURI + '?code=' + code.code);
});
} else {
callback(null);
}
});
},
function otherChecks(callback) {
if (req.oauth2.client.redirectURI !== req.oauth2.req.redirectURI) {
return res.json(400, {
error: "wrong redirect_uri",
documentation_url: docs_url
});
}

if (req.oauth2.client.internal === true) {
Authcode.create({
code: gcapi.generateUID(32),
clientId: req.oauth2.client.id,
redirectURI: req.oauth2.client.redirectURI,
userId: req.user.id,
scope: req.oauth2.client.scope
}).exec(function (err, code) {
if (err) {
return res.serverError();
var scopes;
if (req.oauth2.client.scope.split(',') === req.oauth2.client.scope) {
scopes = req.oauth2.client.scope
} else {
scopes = req.oauth2.client.scope.split(',')
}

res.redirect(req.oauth2.client.redirectURI + '?code=' + code.code);
});
return;
}
if (req.oauth2.client.internal === true) {
Authcode.create({
code: gcapi.generateUID(32),
clientId: req.oauth2.client.id,
redirectURI: req.oauth2.client.redirectURI,
userId: req.user.id,
scope: req.oauth2.client.scope
}).exec(function (err, code) {
if (err) {
return res.serverError();
}

res.render('dialog', {
transactionID: req.oauth2.transactionID,
user: req.user,
cli: req.oauth2.client,
scopes: scopes
res.redirect(req.oauth2.client.redirectURI + '?code=' + code.code);
});
return;
}

res.render('dialog', {
transactionID: req.oauth2.transactionID,
user: req.user,
cli: req.oauth2.client,
scopes: scopes
});
}
], function (err) {
if (err) throw err;
});
});

Expand Down