{"size": 1014, "ext": "js", "max_stars_repo_path": "srv/lib/webtoken.js", "max_stars_repo_name": "srossross/mongo-realtime", "max_stars_repo_stars_event_min_datetime": null, "max_stars_repo_stars_event_max_datetime": null, "max_issues_repo_path": "srv/lib/webtoken.js", "max_issues_repo_name": "srossross/mongo-realtime", "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "srv/lib/webtoken.js", "max_forks_repo_name": "srossross/mongo-realtime", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": null, "max_issues_count": null, "max_forks_count": null, "avg_line_length": 22.5333333333, "max_line_length": 73, "alphanum_fraction": 0.6055226824}
import jwt from 'jsonwebtoken';
import NodeRSA from 'node-rsa';

import { PRIVATE_KEY } from './defaults';

const privateKey = Buffer.from(PRIVATE_KEY, 'base64').toString();

const pk = NodeRSA(privateKey);
const publicKey = pk.exportKey('public');

export async function verify(token, options = {}) {
  const opts = Object.assign({}, options, { algorithms: ['RS256'] });
  return new Promise((resolve, reject) => {
    jwt.verify(
      token,
      publicKey,
      opts,
      (err, payload) => { if (err) reject(err); else resolve(payload); },
    );
  });
}

export function sign(data, options = {}) {
  return new Promise((resolve, reject) => {
    jwt.sign(
      data,
      privateKey,
      Object.assign({}, options, { algorithm: 'RS256' }),
      (err, token) => { if (err) reject(err); else resolve(token); },
    );
  });
}

// Test Private key
const token = jwt.sign(
  { hello: 'world' },
  privateKey,
  { algorithm: 'RS256' },
);
jwt.verify(
  token,
  publicKey,
  { algorithms: ['RS256'] },
);
