2020-12-13 14:36:07 +00:00
|
|
|
import { InvalidRequest, ServerError, InvalidGrant, InvalidClient } from '../../error'
|
2017-08-23 20:13:45 +00:00
|
|
|
|
2021-02-09 16:46:04 +00:00
|
|
|
export async function refreshToken (oauth2, client, pRefreshToken) {
|
2017-08-23 20:13:45 +00:00
|
|
|
let user = null
|
|
|
|
let ttl = null
|
|
|
|
let refreshToken = null
|
|
|
|
let accessToken = null
|
|
|
|
|
2020-05-28 18:30:21 +00:00
|
|
|
const resObj = {
|
2017-08-23 20:13:45 +00:00
|
|
|
token_type: 'bearer'
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!pRefreshToken) {
|
2020-12-13 14:36:07 +00:00
|
|
|
throw new InvalidRequest('refresh_token is mandatory for refresh_token grant type')
|
2017-08-23 20:13:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
refreshToken = await oauth2.model.refreshToken.fetchByToken(pRefreshToken)
|
|
|
|
} catch (err) {
|
2020-12-13 14:36:07 +00:00
|
|
|
throw new ServerError('Failed to call refreshToken.fetchByToken function')
|
2017-08-23 20:13:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!refreshToken) {
|
2020-12-13 14:36:07 +00:00
|
|
|
throw new InvalidGrant('Refresh token not found')
|
2017-08-23 20:13:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (oauth2.model.refreshToken.getClientId(refreshToken) !== oauth2.model.client.getId(client)) {
|
2020-06-15 16:47:03 +00:00
|
|
|
console.warn('Client %s tried to fetch a refresh token which belongs to client %s!', oauth2.model.client.getId(client),
|
|
|
|
oauth2.model.refreshToken.getClientId(refreshToken))
|
2020-12-13 14:36:07 +00:00
|
|
|
throw new InvalidGrant('Refresh token not found')
|
2017-08-23 20:13:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
user = await oauth2.model.user.fetchById(oauth2.model.refreshToken.getUserId(refreshToken))
|
|
|
|
} catch (err) {
|
2020-12-13 14:36:07 +00:00
|
|
|
throw new ServerError('Failed to call user.fetchById function')
|
2017-08-23 20:13:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!user) {
|
2020-12-13 14:36:07 +00:00
|
|
|
throw new InvalidClient('User not found')
|
2017-08-23 20:13:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
2020-06-15 16:47:03 +00:00
|
|
|
accessToken = await oauth2.model.accessToken.fetchByUserIdClientId(oauth2.model.user.getId(user),
|
|
|
|
oauth2.model.client.getId(client))
|
2017-08-23 20:13:45 +00:00
|
|
|
} catch (err) {
|
2020-12-13 14:36:07 +00:00
|
|
|
throw new ServerError('Failed to call accessToken.fetchByUserIdClientId function')
|
2017-08-23 20:13:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (accessToken) {
|
|
|
|
ttl = oauth2.model.accessToken.getTTL(accessToken)
|
|
|
|
|
|
|
|
if (!ttl) {
|
|
|
|
accessToken = null
|
|
|
|
} else {
|
|
|
|
resObj.access_token = oauth2.model.accessToken.getToken(accessToken)
|
|
|
|
resObj.expires_in = ttl
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!accessToken) {
|
|
|
|
try {
|
2017-08-24 16:23:03 +00:00
|
|
|
resObj.access_token = await oauth2.model.accessToken.create(oauth2.model.user.getId(user),
|
2017-08-23 20:13:45 +00:00
|
|
|
oauth2.model.client.getId(client), oauth2.model.refreshToken.getScope(refreshToken), oauth2.model.accessToken.ttl)
|
|
|
|
} catch (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
|
|
|
}
|
|
|
|
|
|
|
|
resObj.expires_in = oauth2.model.accessToken.ttl
|
|
|
|
}
|
|
|
|
|
|
|
|
return resObj
|
|
|
|
}
|