Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a --noCopy option to the sign command #1

Merged
merged 2 commits into from
Nov 13, 2018
Merged
Show file tree
Hide file tree
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
1 change: 1 addition & 0 deletions bin/jwt.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ commander
.option("-t, --noTimestamp", "The generated JWT will not include an iat.")
.option("-j, --jwtid [jwtid]", "Case sensitive unique identifier")
.option("-s, --subject [subject]", "Identifies the subject of the JWT.")
.option("-n, --noCopy", "Don't copy the token to your clipboard.")
.action(sign);

commander
Expand Down
12 changes: 7 additions & 5 deletions lib/jwt.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ function sign(payload, secret, options = {}) {
verifyOption("payload", payload);
verifyOption("secret", secret);

let { algorithm, audience, expiresIn, header, issuer, keyid, notBefore, noTimestamp, jwtid, subject } = options;
let { algorithm, audience, expiresIn, header, issuer, keyid, notBefore, noTimestamp, jwtid, subject, noCopy } = options;

payload = parseObjectOption("payload", payload);
header = header && parseObjectOption("header", header);
Expand All @@ -60,10 +60,12 @@ function sign(payload, secret, options = {}) {

try {
const token = jwt.sign(payload, secret, jwtOptions);
clipboard.writeSync(token);
log();
log(chalk.black.bgYellow.bold("copied to clipboard:"));
log();
if (!noCopy) {
clipboard.writeSync(token);
log();
log(chalk.black.bgYellow.bold("copied to clipboard:"));
log();
}
log(chalk(token));
} catch (err) {
log();
Expand Down
15 changes: 15 additions & 0 deletions test/jwt.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,21 @@ describe("jwt", () => {
expect(payloadParsed.payload.aud).toBe(audience);
});

it("should not copy to clipboard if --noCopy option is present", done => {
const payload = JSON.stringify({ a: 1 });
const secret = "super secret";
shell.exec(`node ./bin/jwt.js sign '${payload}' '${secret}' --noCopy`, (code, stdout) => {
const clipboardToken = clipboard.readSync();
// Match token on anything with two dots between that doesn't start with a whitespace
var tokenFromStdout = stdout.match(/(\S.*\..*\.*)/g)[0];

expect(clipboardToken).not.toBe(tokenFromStdout);
// Fallback test where we just check if the copied to clipboard line is not being written to stdout
expect(stdout.indexOf("clipboard")).toBe(-1);
done();
});
});

it("should fail if `audience` value is not specified", () => {
let payload = JSON.stringify({ a: 1 });
let secret = "super secret";
Expand Down