UNPKG

@velas/account-agent

Version:

sdk

37 lines (29 loc) 1.06 kB
/* * Check scopes */ async function checkScopes(next) { if (typeof this.params.scope !== 'string' || this.params.scope === '') { console.warn('unknown scopes'); this.params.scope = 'openid'; }; const splited = this.params.scope.split(' '); const decoded = this.provider.sc.decode(splited) this.params.scope = Array.from(new Set(decoded)); // validate each scope this.params.scope.forEach(item => { if (this.provider.sc.PERMITTED_SCOPES.includes(item)) { if (item === 'authorization') { if (!this.params.challenge) { throw new Error('challenge is required for authorization scope') } }; } else { try { if (!item.split(':')[0] || !item.split(':')[1]) throw new Error(); if (!Number.isInteger(Number("0"))) throw new Error(); } catch (_) { throw new Error(`wrong scope format: ${item}`) }; }; }); await next(); }; export default checkScopes;