-
-
Notifications
You must be signed in to change notification settings - Fork 58
Expand file tree
/
Copy pathscope-util.js
More file actions
40 lines (33 loc) · 1.1 KB
/
scope-util.js
File metadata and controls
40 lines (33 loc) · 1.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
const isFormat = require('@node-oauth/formats');
const InvalidScopeError = require('../errors/invalid-scope-error');
const whiteSpace = /\s+/g;
/**
* @module ScopeUtil
*/
/**
* Utility to parse and validate a scope string.
* Uses `isFormat` from {@link https://github.com/node-oauth/formats} to
* validate scopes against `nqchar` format.
*
* @function
* @param requestedScope {string|undefined|null}
* @throws {InvalidScopeError} if the type is not null, undefined or a string.
* @return {undefined|string[]}
* @see {https://github.com/node-oauth/formats}
*/
function parseScope (requestedScope) {
if (requestedScope == null) {
return undefined;
}
if (typeof requestedScope !== 'string') {
throw new InvalidScopeError('Invalid parameter: `scope`');
}
// XXX: this prevents spaced-only strings to become
// treated as valid nqchar by making them empty strings
requestedScope = requestedScope.trim();
if(!isFormat.nqschar(requestedScope)) {
throw new InvalidScopeError('Invalid parameter: `scope`');
}
return requestedScope.split(whiteSpace);
}
module.exports = { parseScope };