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

Will Baxter #13

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
1 change: 1 addition & 0 deletions questions/Q01.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ What three components make up a JSON Web Token?

## Answer

Header, payload, signature
3 changes: 3 additions & 0 deletions questions/Q02.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,6 @@ What is Base64 and why is it used to construct a JWT?

## Answer

Base64 is how we send JWT across a network. It is the result of a string (in this case a JSON object) being translated into binary, and then into recognizeable characters (albeit not a recognizeable string).

Base64 is used because it keeps the data size small.
1 change: 1 addition & 0 deletions questions/Q03.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ What two pieces of information are used to construct the signature for a JWT?

## Answer

It is the header and payload of the JWT hashed and encoded along with a secret key.
2 changes: 1 addition & 1 deletion questions/Q04.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,4 @@ Create a JWT for the following and place it in the answer section:
```

## Answer

eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpZCI6MTUzLCJ1c2VybmFtZSI6InNhbmNoZXoiLCJlbWFpbCI6InJpY2tAc2FuY2hlei5jb20iLCJyb2xlIjoiQURNSU4ifQ.rwGFsO1cBf4ydtsLkvYv1bWe5ggtyafrXXmQbPlrZm8
19 changes: 12 additions & 7 deletions src/token.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,27 +5,32 @@ const jwt = require('jsonwebtoken');
*
* Documentation: https://www.npmjs.com/package/jsonwebtoken#jwtsignpayload-secretorprivatekey-options-callback
*/
function createToken() {
function createToken(payload, secretKey) {
return jwt.sign(payload, secretKey)
}

/**
* 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, secretKey) {
return jwt.sign(payload, secretKey, {expiresIn: 60 * 60})
}

/**
* 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, secretKey) {
try {
return jwt.verify(token, secretKey)
} catch (e) {
console.log(e)
return false
}
}

module.exports = {
createToken,
Expand Down