This repository has been archived on 2022-11-26. You can view files and clone it, but cannot push or open issues or pull requests.
IcyNet.eu/server/api/oauth2/controller/tokens/clientCredentials.js

30 lines
771 B
JavaScript
Raw Permalink Normal View History

import { ServerError, InvalidScope } from '../../error'
2017-08-23 23:13:45 +03:00
export async function clientCredentials (oauth2, client, wantScope) {
2017-08-23 23:13:45 +03:00
let scope = null
2020-05-28 21:30:21 +03:00
const resObj = {
2017-08-23 23:13:45 +03:00
token_type: 'bearer'
}
scope = oauth2.model.client.transformScope(wantScope)
scope = oauth2.model.client.checkScope(client, scope)
if (!scope) {
throw new InvalidScope('Client does not allow access to this scope')
2017-08-23 23:13:45 +03:00
}
console.debug('Scope check passed ', scope)
2017-08-23 23:13:45 +03:00
try {
2020-06-15 19:47:03 +03:00
resObj.access_token = await oauth2.model.accessToken.create(null, oauth2.model.client.getId(client),
scope, oauth2.model.accessToken.ttl)
2017-08-23 23:13:45 +03:00
} catch (err) {
throw new ServerError('Failed to call accessToken.create function')
2017-08-23 23:13:45 +03:00
}
resObj.expires_in = oauth2.model.accessToken.ttl
return resObj
}