OAuth2 authorization code exchange processing

master
rob 2 years ago
parent d7b75db5be
commit a8c324092f

@ -151,7 +151,7 @@ class OAuth2Service extends SiteService {
var code = uuidv4(); var code = uuidv4();
var ac = new OAuth2AuthorizationCode({ var ac = new OAuth2AuthorizationCode({
code, code,
clientId: client._id, client: client._id,
redirectUri, redirectUri,
user: user._id, user: user._id,
scopes: client.scopes, scopes: client.scopes,
@ -166,10 +166,21 @@ class OAuth2Service extends SiteService {
async processExchange (client, code, redirectUri, done) { async processExchange (client, code, redirectUri, done) {
try { try {
const ac = await OAuth2AuthorizationCode.findOne({ code }); const ac = await OAuth2AuthorizationCode
.findOne({ code })
.populate([
{
path: 'client',
},
{
path: 'user',
select: 'username username_lc displayName picture',
},
]);
this.log.debug('process OAuth2 exchange', { client, code, redirectUri }); this.log.debug('process OAuth2 exchange', { client, code, redirectUri });
if (!client._id.equals(ac.clientId)) { if (!client._id.equals(ac.client._id)) {
this.log.alert('OAuth2 client ID mismatch', { provided: client.id, onfile: ac.clientId }); this.log.alert('OAuth2 client ID mismatch', { provided: client.id, onfile: ac.client._id });
return done(null, false); return done(null, false);
} }
if (redirectUri !== ac.redirectUri) { if (redirectUri !== ac.redirectUri) {
@ -180,8 +191,8 @@ class OAuth2Service extends SiteService {
var token = uuidv4(); var token = uuidv4();
var at = new OAuth2AccessToken({ var at = new OAuth2AccessToken({
token, token,
user: ac.userId, user: ac.user._id,
client: ac.clientId, client: ac.client._id,
scope: ac.scope, scope: ac.scope,
}); });
await at.save(); await at.save();

Loading…
Cancel
Save