-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathindex.ts
More file actions
140 lines (131 loc) · 3.24 KB
/
index.ts
File metadata and controls
140 lines (131 loc) · 3.24 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
import { CommonAccessToken, CommonAccessTokenFactory } from './cat';
import { KeyNotFoundError } from './errors';
export { CommonAccessToken } from './cat';
export { HttpValidator } from './validators/http';
export type CatValidationTypes = 'mac' | 'sign' | 'none';
export interface CatValidationOptions {
alg?: string;
issuer: string;
audience?: string[];
}
export interface CatGenerateOptions {
type: CatValidationTypes;
alg: string;
kid: string;
}
/**
* Options for the CAT object
*
* @param {Object} keys - Key object
* @param {boolean} expectCwtTag - Expect CWT tag in token
*
* @example
* const opts = {
* keys: {
* Symmetric256: Buffer.from('403697de87af64611c1d32a05dab0fe1fcb715a86ab435f1ec99192d79569388', 'hex')
* },
* expectCwtTag: true
* };
*/
export interface CatOptions {
keys: { [keyid: string]: Buffer };
expectCwtTag?: boolean;
}
/**
* Common Access Token (CAT) validator and generator
*
* @param {CatOptions} opts - Options for the CAT object
*
* @example
* const validator = new CAT({
* keys: {
* Symmetric256: Buffer.from(
* '403697de87af64611c1d32a05dab0fe1fcb715a86ab435f1ec99192d79569388',
* 'hex'
* )
* },
* });
* try {
* await validator.validate(base64encoded, 'mac', {
* kid: 'Symmetric256',
* issuer: 'coap://as.example.com'
* });
* } catch (e) {
* // Not valid, handle error
* }
*/
export class CAT {
private keys: { [keyid: string]: Buffer };
private expectCwtTag = false;
constructor(opts: CatOptions) {
this.keys = opts.keys;
this.expectCwtTag = opts.expectCwtTag || false;
}
public async validate(
token: string,
type: CatValidationTypes,
opts: CatValidationOptions
) {
const tokenWithoutPadding = token.trim();
let cat;
if (type == 'mac') {
if (!opts) {
throw new Error('Missing options for MAC validation');
}
let error;
for (const kid in this.keys) {
try {
const key = this.keys[kid];
cat = await CommonAccessTokenFactory.fromMacedToken(
tokenWithoutPadding,
{
k: key,
kid: kid
},
this.expectCwtTag
);
if (cat && cat.claims) {
error = undefined;
break;
}
} catch (err) {
error = err;
}
}
if (error) {
if ((error as Error).message === 'Tag mismatch') {
throw new KeyNotFoundError();
} else {
throw error;
}
}
} else {
throw new Error('Unsupported validation type');
}
if (cat) {
const valid = await cat.isValid(opts);
if (valid) {
return cat;
}
}
}
public async generate(
claims: { [key: string]: string | number },
opts?: CatGenerateOptions
) {
const cat = new CommonAccessToken(claims);
if (opts && opts.type == 'mac') {
const key = this.keys[opts.kid];
if (!key) {
throw new Error('Key not found');
}
const mac = await cat.mac({ k: key, kid: opts.kid }, opts.alg, {
addCwtTag: this.expectCwtTag
});
if (!mac.raw) {
throw new Error('Failed to MAC token');
}
return mac.raw.toString('base64');
}
}
}