Skip to content

Commit

Permalink
Login method coding
Browse files Browse the repository at this point in the history
Added basic password check
Initial response decoding
Custom secret_key implementation
  • Loading branch information
vexy committed Feb 29, 2020
1 parent cb7c3b7 commit 32f48d7
Showing 1 changed file with 17 additions and 2 deletions.
19 changes: 17 additions & 2 deletions auth-module.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
from flask import Flask
from flask import Flask, jsonify, request, make_response
import jwt
import datetime

# initialize main Flask object
if __name__ == '__main__':
app = Flask(__name__)

app.config['SECRET_KEY'] = 'some_secret_key'

# ROUTES DEFINITION:
@app.route('/unprotected')
def unprotected():
Expand All @@ -15,7 +19,18 @@ def protected():

@app.route('/login')
def login():
return 'You are now logged in'
auth = request.authorization
if auth and auth.password == 'password':
token_expiration = str(datetime.datetime.utcnow() + datetime.timedelta(minutes=30))
token = jwt.encode(
{
'user': auth.username,
'expiration': token_expiration
}, app.config['SECRET_KEY'], algorithm='HS256')

return jsonify({'token': token.decode('UTF-8')})

return make_response('Could not verify!', 401, {'WWW-Authenticate': 'Basic realm="Login required"'})

# start the server
if __name__ == '__main__':
Expand Down

0 comments on commit 32f48d7

Please sign in to comment.