2020-12-13 14:36:07 +00:00
|
|
|
import { InvalidRequest, ServerError, InvalidGrant } from '../../error'
|
2017-08-23 20:13:45 +00:00
|
|
|
|
2021-02-09 16:46:04 +00:00
|
|
|
export async function authorizationCode (oauth2, client, providedCode) {
|
2020-05-28 18:30:21 +00:00
|
|
|
const respObj = {
|
2017-08-23 20:13:45 +00:00
|
|
|
token_type: 'bearer'
|
|
|
|
}
|
|
|
|
|
|
|
|
let code = null
|
|
|
|
|
2017-08-27 22:35:19 +00:00
|
|
|
if (!providedCode) {
|
2020-12-13 14:36:07 +00:00
|
|
|
throw new InvalidRequest('code is mandatory for authorization_code grant type')
|
2017-08-27 22:35:19 +00:00
|
|
|
}
|
|
|
|
|
2017-08-23 20:13:45 +00:00
|
|
|
try {
|
|
|
|
code = await oauth2.model.code.fetchByCode(providedCode)
|
|
|
|
} catch (err) {
|
2017-08-27 22:35:19 +00:00
|
|
|
console.error(err)
|
2020-12-13 14:36:07 +00:00
|
|
|
throw new ServerError('Failed to call code.fetchByCode function')
|
2017-08-23 20:13:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (code) {
|
|
|
|
if (oauth2.model.code.getClientId(code) !== oauth2.model.client.getId(client)) {
|
2020-12-13 14:36:07 +00:00
|
|
|
throw new InvalidGrant('Code was issued by another client')
|
2017-08-23 20:13:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!oauth2.model.code.checkTTL(code)) {
|
2020-12-13 14:36:07 +00:00
|
|
|
throw new InvalidGrant('Code has already expired')
|
2017-08-23 20:13:45 +00:00
|
|
|
}
|
|
|
|
} else {
|
2020-12-13 14:36:07 +00:00
|
|
|
throw new InvalidGrant('Code not found')
|
2017-08-23 20:13:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
console.debug('Code fetched ', code)
|
|
|
|
|
|
|
|
try {
|
2020-06-15 16:47:03 +00:00
|
|
|
await oauth2.model.refreshToken.removeByUserIdClientId(oauth2.model.code.getUserId(code),
|
|
|
|
oauth2.model.code.getClientId(code))
|
2017-08-23 20:13:45 +00:00
|
|
|
} catch (err) {
|
|
|
|
console.error(err)
|
2020-12-13 14:36:07 +00:00
|
|
|
throw new ServerError('Failed to call refreshToken.removeByUserIdClientId function')
|
2017-08-23 20:13:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
console.debug('Refresh token removed')
|
|
|
|
|
|
|
|
if (!oauth2.model.client.checkGrantType(client, 'refresh_token')) {
|
|
|
|
console.debug('Client does not allow grant type refresh_token, skip creation')
|
|
|
|
} else {
|
|
|
|
try {
|
2020-06-15 16:47:03 +00:00
|
|
|
respObj.refresh_token = await oauth2.model.refreshToken.create(oauth2.model.code.getUserId(code),
|
|
|
|
oauth2.model.code.getClientId(code), oauth2.model.code.getScope(code))
|
2017-08-23 20:13:45 +00:00
|
|
|
} catch (err) {
|
|
|
|
console.error(err)
|
2020-12-13 14:36:07 +00:00
|
|
|
throw new ServerError('Failed to call refreshToken.create function')
|
2017-08-23 20:13:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
2020-06-15 16:47:03 +00:00
|
|
|
respObj.access_token = await oauth2.model.accessToken.create(oauth2.model.code.getUserId(code),
|
|
|
|
oauth2.model.code.getClientId(code), oauth2.model.code.getScope(code), oauth2.model.accessToken.ttl)
|
2017-08-23 20:13:45 +00:00
|
|
|
} catch (err) {
|
|
|
|
console.error(err)
|
2020-12-13 14:36:07 +00:00
|
|
|
throw new ServerError('Failed to call accessToken.create function')
|
2017-08-23 20:13:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
respObj.expires_in = oauth2.model.accessToken.ttl
|
|
|
|
console.debug('Access token saved: ', respObj.access_token)
|
|
|
|
|
|
|
|
try {
|
|
|
|
await oauth2.model.code.removeByCode(providedCode)
|
|
|
|
} catch (err) {
|
|
|
|
console.error(err)
|
2020-12-13 14:36:07 +00:00
|
|
|
throw new ServerError('Failed to call code.removeByCode function')
|
2017-08-23 20:13:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return respObj
|
|
|
|
}
|