Skip to content

Commit 1c48636

Browse files
Algorithm Lucidity
See #35
1 parent 618da17 commit 1c48636

File tree

5 files changed

+71
-0
lines changed

5 files changed

+71
-0
lines changed

lib/key/private.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ function PrivateKey(protocol) {
2828
protocol = protocol || new V2();
2929

3030
self._protocol = protocol;
31+
self._purpose = 'public';
3132
}
3233

3334

@@ -216,6 +217,20 @@ function protocol() {
216217
return this._protocol;
217218
}
218219

220+
/***
221+
* purpose
222+
*
223+
* return the underlying purpose object
224+
*
225+
* @function
226+
* @api public
227+
*
228+
* @returns {string}
229+
*/
230+
PrivateKey.prototype.purpose = purpose;
231+
function purpose() {
232+
return this._purpose;
233+
}
219234

220235
/***
221236
* encode

lib/key/public.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ function PublicKey(protocol) {
2222
protocol = protocol || new V2();
2323

2424
self._protocol = protocol;
25+
self._purpose = 'public';
2526
}
2627

2728

@@ -148,6 +149,21 @@ function protocol() {
148149
return this._protocol;
149150
}
150151

152+
/***
153+
* purpose
154+
*
155+
* return the underlying purpose object
156+
*
157+
* @function
158+
* @api public
159+
*
160+
* @returns {string}
161+
*/
162+
PublicKey.prototype.purpose = purpose;
163+
function purpose() {
164+
return this._purpose;
165+
}
166+
151167

152168
/***
153169
* encode

lib/key/symmetric.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ function SymmetricKey(protocol) {
2525
self.INFO_AUTHENTICATION = 'paseto-auth-key-for-aead';
2626

2727
self._protocol = protocol || new V2();
28+
self._purpose = 'local';
2829
}
2930

3031

@@ -162,6 +163,21 @@ function protocol() {
162163
return this._protocol;
163164
}
164165

166+
/***
167+
* purpose
168+
*
169+
* return the underlying purpose object
170+
*
171+
* @function
172+
* @api public
173+
*
174+
* @returns {string}
175+
*/
176+
SymmetricKey.prototype.purpose = purpose;
177+
function purpose() {
178+
return this._purpose;
179+
}
180+
165181

166182
/***
167183
* encode

lib/protocol/V1.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,9 @@ function __encrypt(data, key, footer, nonce, cb) {
137137
const self = this;
138138
const done = utils.ret(cb);
139139

140+
if (key.purpose() !== 'local') {
141+
return done(new InvalidVersionError('The given key is not intended for local PASETO tokens.'));
142+
}
140143
if (!(key.protocol() instanceof V1)) {
141144
return done(new InvalidVersionError('The given key is not intended for this version of PASETO.'));
142145
}
@@ -189,6 +192,9 @@ function decrypt(token, key, footer, cb) {
189192
const self = this;
190193
const done = utils.ret(cb);
191194

195+
if (key.purpose() !== 'local') {
196+
return done(new InvalidVersionError('The given key is not intended for local PASETO tokens.'));
197+
}
192198
if (!(key.protocol() instanceof V1)) {
193199
return done(new InvalidVersionError('The given key is not intended for this version of PASETO.'));
194200
}
@@ -226,6 +232,9 @@ function sign(data, key, footer, cb) {
226232
const self = this;
227233
const done = utils.ret(cb);
228234

235+
if (key.purpose() !== 'public') {
236+
return done(new InvalidVersionError('The given key is not intended for local PASETO tokens.'));
237+
}
229238
if (!(key.protocol() instanceof V1)) {
230239
return done(new InvalidVersionError('The given key is not intended for this version of PASETO.'));
231240
}
@@ -278,6 +287,9 @@ function verify(token, key, footer, cb) {
278287
const self = this;
279288
const done = utils.ret(cb);
280289

290+
if (key.purpose() !== 'public') {
291+
return done(new InvalidVersionError('The given key is not intended for local PASETO tokens.'));
292+
}
281293
if (!(key.protocol() instanceof V1)) {
282294
return done(new InvalidVersionError('The given key is not intended for this version of PASETO.'));
283295
}

lib/protocol/V2.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,9 @@ function __encrypt(data, key, footer, nonce, cb) {
129129
const self = this;
130130
const done = utils.ret(cb);
131131

132+
if (key.purpose() !== 'local') {
133+
return done(new InvalidVersionError('The given key is not intended for local PASETO tokens.'));
134+
}
132135
if (!(key.protocol() instanceof V2)) {
133136
return done(new InvalidVersionError('The given key is not intended for this version of PASETO.'));
134137
}
@@ -190,6 +193,9 @@ function decrypt(token, key, footer, cb) {
190193
const self = this;
191194
const done = utils.ret(cb);
192195

196+
if (key.purpose() !== 'local') {
197+
return done(new InvalidVersionError('The given key is not intended for local PASETO tokens.'));
198+
}
193199
if (!(key.protocol() instanceof V2)) {
194200
return done(new InvalidVersionError('The given key is not intended for this version of PASETO.'));
195201
}
@@ -231,6 +237,9 @@ function sign(data, key, footer, cb) {
231237
const self = this;
232238
const done = utils.ret(cb);
233239

240+
if (key.purpose() !== 'public') {
241+
return done(new InvalidVersionError('The given key is not intended for local PASETO tokens.'));
242+
}
234243
if (!(key.protocol() instanceof V2)) {
235244
return done(new InvalidVersionError('The given key is not intended for this version of PASETO.'));
236245
}
@@ -283,6 +292,9 @@ function verify(token, key, footer, cb) {
283292
const self = this;
284293
const done = utils.ret(cb);
285294

295+
if (key.purpose() !== 'public') {
296+
return done(new InvalidVersionError('The given key is not intended for local PASETO tokens.'));
297+
}
286298
if (!(key.protocol() instanceof V2)) {
287299
return done(new InvalidVersionError('The given key is not intended for this version of PASETO.'));
288300
}

0 commit comments

Comments
 (0)