remove @squeep/base64url dependency in lieu of native encoder
[squeep-resource-authentication-module] / lib / resource-authenticator.js
index 04ac9991189bf8b10f096e77e10cc88c91e57f66..f4558c1d3f1fb5543122eee8c97d30a2431f3f3a 100644 (file)
@@ -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');
   }