-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathcatu.ts
More file actions
206 lines (193 loc) · 5.25 KB
/
catu.ts
File metadata and controls
206 lines (193 loc) · 5.25 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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
import { InvalidCatuError } from './errors';
import {
labelsToMatch,
match,
matchToLabels,
MatchType,
MatchValue
} from './cattypes/match';
type UriPart =
| 'scheme'
| 'host'
| 'port'
| 'path'
| 'query'
| 'parent-path'
| 'filename'
| 'stem'
| 'extension';
const uriPartToLabels: { [key: string]: number } = {
scheme: 0,
host: 1,
port: 2,
path: 3,
query: 4,
'parent-path': 5,
filename: 6,
stem: 7,
extension: 8
};
const labelsToUriPart: { [key: number]: UriPart } = {
0: 'scheme',
1: 'host',
2: 'port',
3: 'path',
4: 'query',
5: 'parent-path',
6: 'filename',
7: 'stem',
8: 'extension'
};
type UriPartMap = Map<number, string | string[]>;
export type CommonAccessTokenUriMap = Map<number, UriPartMap>;
export class CommonAccessTokenUri {
private catuMap: CommonAccessTokenUriMap = new Map();
/**
* Create a CATU claim from a dictionary with numbers as keys (labels)
*/
public static fromDictTags(dict: { [key: number]: any }) {
const newDict: { [key: string]: any } = {};
for (const uriPartTag in dict) {
const matchDict: { [key: string]: any } = {};
for (const matchTag in dict[uriPartTag]) {
const tag = parseInt(matchTag);
matchDict[labelsToMatch[tag]] = dict[uriPartTag][matchTag];
}
newDict[labelsToUriPart[parseInt(uriPartTag)]] = matchDict;
}
return CommonAccessTokenUri.fromDict(newDict);
}
/**
* Create a CATU claim from a dictionary with string as keys
*/
public static fromDict(dict: { [key: string]: any }) {
const catu = new CommonAccessTokenUri();
for (const uriPart in dict) {
const matchMap = new Map<number, MatchValue>();
for (const match in dict[uriPart]) {
matchMap.set(matchToLabels[match], dict[uriPart][match]);
}
catu.catuMap.set(uriPartToLabels[uriPart], matchMap);
}
return catu;
}
/**
* Create a CATU claim from a map with string as keys
*/
public static fromUnlabeledMap(unLabeledMap: Map<string, any>) {
const map: CommonAccessTokenUriMap = new Map();
unLabeledMap.forEach((value, uriPart) => {
const uriPartLabel = uriPartToLabels[uriPart];
const matchMap = new Map<number, MatchValue>();
value.forEach((v: any, matchPart: string) => {
matchMap.set(matchToLabels[matchPart], v);
});
map.set(uriPartLabel, matchMap);
});
return CommonAccessTokenUri.fromMap(map);
}
/**
* Create a CATU claim from a map with number as keys
*/
public static fromMap(map: CommonAccessTokenUriMap) {
const catu = new CommonAccessTokenUri();
catu.catuMap = map;
return catu;
}
private async doMatch(
value: string,
matchType: MatchType,
matchValue: string | string[]
) {
try {
return await match(value, matchType, matchValue);
} catch (err) {
throw new InvalidCatuError((err as Error).message);
}
}
public async match(uri: URL): Promise<boolean> {
for (const [uriPart, uriPartMap] of this.catuMap) {
const uriPartType = labelsToUriPart[uriPart];
const matchLabel = uriPartMap.keys().next().value;
const matchValue = uriPartMap.get(matchLabel!);
let value;
switch (uriPartType) {
case 'scheme':
value = uri.protocol.slice(0, -1);
break;
case 'host':
value = uri.hostname;
break;
case 'port':
value = uri.port;
break;
case 'path':
value = uri.pathname;
break;
case 'query':
{
const params = new URLSearchParams(uri.search);
params.delete('cat');
value = params.toString();
}
break;
case 'parent-path':
{
const idx = uri.pathname.lastIndexOf('/');
value = uri.pathname.slice(0, idx);
}
break;
case 'filename':
{
const idx = uri.pathname.lastIndexOf('/');
value = uri.pathname.slice(idx + 1);
}
break;
case 'stem':
{
const filename = uri.pathname.split('/').pop();
value = filename?.slice(
0,
filename.indexOf('.') === -1
? filename.length
: filename.indexOf('.')
);
}
break;
case 'extension':
{
const filename = uri.pathname.split('/').pop();
value = filename?.slice(
filename.indexOf('.') === -1
? filename.length
: filename.indexOf('.')
);
}
break;
default:
throw new InvalidCatuError(`Unsupported URI part: ${uriPartType}`);
}
if (
!(await this.doMatch(value!, labelsToMatch[matchLabel!], matchValue!))
) {
return false;
}
}
return true;
}
toDict() {
const result: { [key: string]: any } = {};
this.catuMap.forEach((uriPartMap, uriPart) => {
const part = labelsToUriPart[uriPart];
const match: { [key: string]: any } = {};
uriPartMap.forEach((value, matchType) => {
match[labelsToMatch[matchType]] = value;
});
result[part] = match;
});
return result;
}
get payload() {
return this.catuMap;
}
}