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

Alexander Lind #1

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
3 changes: 3 additions & 0 deletions questions/Q01.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,6 @@ What three components make up a JSON Web Token?

## Answer

1. Header
2. Payload
3. Signature
1 change: 1 addition & 0 deletions questions/Q02.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ What is Base64 and why is it used to construct a JWT?

## Answer

Base64 allows you to represent 8 bit bytes (0's and 1's) by using digits.
2 changes: 2 additions & 0 deletions questions/Q03.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,7 @@

What two pieces of information are used to construct the signature for a JWT?


## Answer

The encoded header and payload, as well as the "secret" phrase which can be base64 encoded as well.
7 changes: 7 additions & 0 deletions questions/Q04.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,10 @@ Create a JWT for the following and place it in the answer section:

## Answer

Witout base64 encoded secret:

eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpZCI6MTUzLCJ1c2VybmFtZSI6InNhbmNoZXoiLCJlbWFpbCI6InJpY2tAc2FuY2hlei5jb20iLCJyb2xlIjoiQURNSU4ifQ.mEzEZU6ileMd9Uo6k9LMFimIlBpuYuD2pdpP-GHaPmA

With base64 encoded secret:

eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpZCI6MTUzLCJ1c2VybmFtZSI6InNhbmNoZXoiLCJlbWFpbCI6InJpY2tAc2FuY2hlei5jb20iLCJyb2xlIjoiQURNSU4ifQ.rwGFsO1cBf4ydtsLkvYv1bWe5ggtyafrXXmQbPlrZm8
41 changes: 28 additions & 13 deletions src/token.js
Original file line number Diff line number Diff line change
@@ -1,34 +1,49 @@
const jwt = require('jsonwebtoken');
const jwt = require("jsonwebtoken");

/**
* Implement this function to accept a payload and a secret key and return a JWT without an expiry time
*
*
* Documentation: https://www.npmjs.com/package/jsonwebtoken#jwtsignpayload-secretorprivatekey-options-callback
*/
function createToken() {

function createToken(payload, secret) {
let token = jwt.sign(payload, secret);
return token;
}

/**
* Implement this function to accept a payload, secret key and an expiry time, and return a JWT with an expiry
*
*
* Documentation: https://www.npmjs.com/package/jsonwebtoken#token-expiration-exp-claim
*/
function createTokenWithExpiry() {

function createTokenWithExpiry(payload, secret, expiry) {
let token = jwt.sign(
payload,
secret,
{ expiresIn: expiry }
);
return token;
}


/**
* Implement this function to accept a JWT and a secret key. Return the decoded token (the payload) if verification is successful, and false if it fails
*
*
* Documentation: https://www.npmjs.com/package/jsonwebtoken#jwtverifytoken-secretorpublickey-options-callback
*/
function verifyToken() {

function verifyToken(token, secret) {


try {
let decoded = jwt.verify(token, secret);
return decoded
} catch (error) {
return false;
}
}

module.exports = {
createToken,
createTokenWithExpiry,
verifyToken
}
createToken,
createTokenWithExpiry,
verifyToken,
};