Author: Hayden Smith 2021
Authentication: Process of verifying the identity of a user
Authorisation: Process of verifying an identity's access privileges
Naive method:
Let's observe auth.py
(found in lectures repo)
What's wrong with this?
import hashlib
print("mypassword")
print("mypassword".encode())
print(hashlib.sha256("mypassword".encode()))
print(hashlib.sha256("mypassword".encode()).hexdigest())
Now let's improve auth.py
Authorisation typically involves giving the user some kind of pseudo-password that they store on their computer (client-side) which is a shortcut method for authorising a particular user.
An SSH key is an example of this.
What is a "token"?
A packet of data used to authorise the user.
What kind of tokens exist?
User ID | Session ID | |
---|---|---|
Non JWT | One login session + insecure | Concurrent login sessions + insecure |
JWT | One login session + secure |
Concurrent login sessions + secure |
"JSON Web Tokens are an open, industry standard RFC 7519 method for representing claims securely between two parties."
They are lightweight ways of encoding and decoding private information via a secret
Play around:
import jwt
SECRET = 'sempai'
encoded_jwt = jwt.encode({'some': 'payload'}, SECRET, algorithm='HS256')
print(jwt.decode(encoded_jwt.encode('utf-8'), SECRET, algorithms=['HS256']))
Now let's improve auth.py