X-Git-Url: http://git.squeep.com/?p=squeep-resource-authentication-module;a=blobdiff_plain;f=lib%2Fresource-authenticator.js;h=f4558c1d3f1fb5543122eee8c97d30a2431f3f3a;hp=04ac9991189bf8b10f096e77e10cc88c91e57f66;hb=efa8e263868de47ff3528561c5f4c8eeefbd5035;hpb=69a2f5e7d73dd3f58e07b652c306daa8b253245d diff --git a/lib/resource-authenticator.js b/lib/resource-authenticator.js index 04ac999..f4558c1 100644 --- a/lib/resource-authenticator.js +++ b/lib/resource-authenticator.js @@ -57,7 +57,7 @@ class ResourceAuthenticator { */ async getSalt() { const saltBytes = await common.randomBytesAsync(this.saltBytes); - return common.base64ToBase64URL(saltBytes.toString('base64')); + return saltBytes.toString('base64url'); } @@ -133,6 +133,7 @@ class ResourceAuthenticator { * @param {String} identifier UUID */ async authenticate(identifier, secret) { + const authenticationType = 'Bearer'; const currentEpoch = ResourceAuthenticator.currentEpoch.toString(); const smallIdentifier = ResourceAuthenticator.ensmallenIdentifier(identifier); const salt = await this.getSalt(); @@ -142,7 +143,8 @@ class ResourceAuthenticator { salt, this.createDigest(secret, smallIdentifier, currentEpoch, salt), ]; - return parts.join(':'); + const token = parts.join(':'); + return [authenticationType, token].join(' '); } @@ -152,8 +154,7 @@ class ResourceAuthenticator { * @returns {String} b64u-encoded UUID */ static ensmallenIdentifier(identifier) { - const uuidBase64 = Buffer.from(uuid.parse(identifier)).toString('base64'); - return common.base64ToBase64URL(uuidBase64); + return Buffer.from(uuid.parse(identifier)).toString('base64url'); } @@ -163,8 +164,7 @@ class ResourceAuthenticator { * @returns {String} UUID */ static embiggenIdentifier(small) { - const uuidBase64 = common.base64URLToBase64(small); - const uuidBuffer = Buffer.from(uuidBase64, 'base64'); + const uuidBuffer = Buffer.from(small, 'base64url'); return uuid.stringify(uuidBuffer); } @@ -178,8 +178,7 @@ class ResourceAuthenticator { createDigest(secret, ...contents) { const hmac = crypto.createHmac(this.digestAlgorithm, secret); contents.forEach((content) => hmac.update(Buffer.from(content))); - const digestBase64 = hmac.digest('base64'); - return common.base64ToBase64URL(digestBase64); + return hmac.digest('base64url'); }