Skip to content

Commit

Permalink
fix: make generateAccessToken URL-safe by default
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelfig committed Sep 13, 2020
1 parent 20c1f27 commit 722f811
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 4 deletions.
9 changes: 7 additions & 2 deletions packages/agoric-cli/lib/open.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,20 @@ import path from 'path';

import { openSwingStore } from '@agoric/swing-store-simple';

// From https://stackoverflow.com/a/43866992/14073862
// Adapted from https://stackoverflow.com/a/43866992/14073862
export function generateAccessToken({
stringBase = 'base64',
stringBase = 'base64url',
byteLength = 48,
} = {}) {
return new Promise((resolve, reject) =>
crypto.randomBytes(byteLength, (err, buffer) => {
if (err) {
reject(err);
} else if (stringBase === 'base64url') {
// Convert to url-safe base64.
const base64 = buffer.toString('base64');
const base64url = base64.replace(/\+/g, '-').replace(/\//g, '_');
resolve(base64url);
} else {
resolve(buffer.toString(stringBase));
}
Expand Down
9 changes: 7 additions & 2 deletions packages/cosmic-swingset/lib/ag-solo/access-token.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,20 @@ import path from 'path';

import { openSwingStore } from '@agoric/swing-store-simple';

// From https://stackoverflow.com/a/43866992/14073862
// Adapted from https://stackoverflow.com/a/43866992/14073862
export function generateAccessToken({
stringBase = 'base64',
stringBase = 'base64url',
byteLength = 48,
} = {}) {
return new Promise((resolve, reject) =>
crypto.randomBytes(byteLength, (err, buffer) => {
if (err) {
reject(err);
} else if (stringBase === 'base64url') {
// Convert to url-safe base64.
const base64 = buffer.toString('base64');
const base64url = base64.replace(/\+/g, '-').replace(/\//g, '_');
resolve(base64url);
} else {
resolve(buffer.toString(stringBase));
}
Expand Down

0 comments on commit 722f811

Please sign in to comment.