-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
87 lines (81 loc) · 3.32 KB
/
index.js
File metadata and controls
87 lines (81 loc) · 3.32 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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
"use strict";
const assert = require("assert");
const CapabilityToken = require("capability-token");
class CapabilityURI
{
constructor(config)
{
const self = this;
assert.ok(config.authority || config.capabilityAuthority);
if (config.authority)
{
assert.ok(!config.capabilityAuthority);
assert.ok(!config.authorityScheme);
assert.ok(new RegExp("[^\/]{3,}").exec(config.authority));
assert.ok(config.capabilityToken && typeof config.capabilityToken.serialize == "function");
assert.ok(CapabilityToken.parse(config.capabilityToken.serialize()));
self.authority = config.authority;
self.capabilityToken = config.capabilityToken;
}
else if (config.capabilityAuthority)
{
assert.ok(!config.authority);
assert.ok(new RegExp(`^${CapabilityToken.BASE64URL_REGEX.source}$`).exec(config.capabilityAuthority));
if (config.authorityScheme)
{
assert.ok(new RegExp("[-_A-Za-z0-9]+").exec(config.authorityScheme));
}
assert.ok(config.capabilityToken && typeof config.capabilityToken.serialize == "function");
assert.ok(CapabilityToken.parse(config.capabilityToken.serialize()));
self.capabilityAuthority = config.capabilityAuthority;
self.authorityScheme = config.authorityScheme;
self.capabilityToken = config.capabilityToken;
}
}
static parse(uri)
{
const nonCustomParsed = CapabilityURI.NON_CUSTOM_URI_REGEX.exec(uri);
if (nonCustomParsed)
{
// nonCustomParsed[1] is authority
// nonCustomParsed[2] is CapabilityToken
return new CapabilityURI(
{
authority: nonCustomParsed[1],
capabilityToken: CapabilityToken.parse(nonCustomParsed[2])
}
);
}
const customParsed = CapabilityURI.CUSTOM_URI_REGEX.exec(uri);
if (customParsed)
{
// customParsed[1] is capabilityAuthority
// customParsed[2] is authorityScheme
// customParsed[3] is CapabilityToken
return new CapabilityURI(
{
authorityScheme: customParsed[2].length > 0 ? customParsed[2].slice(1) : undefined,
capabilityAuthority: customParsed[1],
capabilityToken: CapabilityToken.parse(customParsed[3])
}
);
}
return false;
}
serialize()
{
const self = this;
if (self.authority)
{
return `cpblty://${self.authority}/#${self.capabilityToken.serialize()}`
}
else if (self.capabilityAuthority)
{
return `cpblty:${self.capabilityAuthority}${self.authorityScheme ? `@${self.authorityScheme}` : ""}:${self.capabilityToken.serialize()}`
}
throw new Error("Invalid CapabilityURI");
}
};
CapabilityURI.CUSTOM_URI_REGEX = new RegExp(`^cpblty:(${CapabilityToken.BASE64URL_REGEX.source})((?:@[-_A-Za-z0-9]+)?):(${CapabilityToken.TOKEN_REGEX.source.slice(1)})`);
CapabilityURI.NON_CUSTOM_URI_REGEX = new RegExp(`^cpblty:\/\/([^\/]{3,})/#(${CapabilityToken.TOKEN_REGEX.source.slice(1)})`);
module.exports = CapabilityURI;