Skip to content
This repository was archived by the owner on Dec 27, 2024. It is now read-only.

Fix base64 decoding #45

Merged
merged 1 commit into from
Oct 5, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 13 additions & 4 deletions src/authentication.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import * as debugInit from 'debug';
import { Handler, NextFunction, Request, Response } from 'express';
import { decode as decodeToken, sign as signToken } from 'jsonwebtoken';
import { ObjectID } from 'mongodb';
import { decode as urlBase64Decode, encode as urlBase64Encode, validate as urlBase64Validate } from 'urlsafe-base64';
import { encode as urlBase64Encode, validate as urlBase64Validate } from 'urlsafe-base64';
import { v4 as uuid } from 'uuid';
import * as pwStrength from 'zxcvbn';

Expand Down Expand Up @@ -1234,9 +1234,9 @@ export function addAuthenticationAuthority(sapi: SakuraApi, options: IAuthentica
return reject(403);
}

const encryptedToken = urlBase64Decode(tokenBase64);
const hmacBuffer = urlBase64Decode(hmacBase64);
const ivBuffer = urlBase64Decode(ivBase64);
const encryptedToken = this.urlBase64Decode(tokenBase64);
const hmacBuffer = this.urlBase64Decode(hmacBase64);
const ivBuffer = this.urlBase64Decode(ivBase64);

let token;
try {
Expand All @@ -1254,6 +1254,15 @@ export function addAuthenticationAuthority(sapi: SakuraApi, options: IAuthentica
});
}

private urlBase64Decode(encoded: string): Buffer {
const remainder = encoded.length % 4;
encoded += remainder === 2 ? '==' : remainder === 3 ? '=' : '';
encoded = encoded
.replace(/\-/g, '+')
.replace(/\_/g, '/');
return Buffer.from(encoded, 'base64');
}

private hashToken(token): string {
return createHash('sha256').update(JSON.stringify(token)).digest('base64');
}
Expand Down