-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #41 from dmtzs/development
Development to master
- Loading branch information
Showing
5 changed files
with
154 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
try: | ||
from flask import Flask | ||
import flask_authgen_jwt | ||
except ImportError as eImp: | ||
print(f"The following import ERROR occurred in {__file__}: {eImp}") | ||
|
||
app = Flask(__name__) | ||
gen_auth = flask_authgen_jwt.GenJwt() | ||
auth = flask_authgen_jwt.DecJwt() | ||
|
||
from app import routes |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
try: | ||
import datetime as dt | ||
from app import app, auth, gen_auth | ||
from flask import Response, make_response, jsonify | ||
except ImportError as eImp: | ||
print(f"The following import ERROR occurred in {__file__}: {eImp}") | ||
|
||
@gen_auth.enc_dec_jwt_config | ||
@auth.enc_dec_jwt_config | ||
def test_creds() -> dict: | ||
decode_attributes = { | ||
"key": "secret", | ||
"algorithm": "HS256", | ||
} | ||
return decode_attributes | ||
|
||
@gen_auth.personal_credentials_field | ||
@auth.personal_credentials_field | ||
def personal_credentials_field() -> tuple[str, str]: | ||
return "per_username", "per_password" | ||
|
||
@gen_auth.verify_bauth_credentials | ||
def get_basic_auth_credentials2(username: str, password: str) -> dict: | ||
# Use the username and password to authenticate the user in the way you want- | ||
# and return true if the user is authenticated | ||
if username == "admin2" and password == "passwd2": | ||
return True | ||
else: | ||
return False | ||
|
||
@auth.get_user_roles | ||
@gen_auth.get_user_roles | ||
def my_roles(username: str) -> list[str]: | ||
# Use username to get roles from database | ||
print(f"username in roles: {username}") | ||
return ["admin", "user"] | ||
|
||
@auth.get_jwt_claims_to_verify | ||
def get_jwt_claims_to_verify() -> list[str]: | ||
# return ["exp", "iat", "nbf"] | ||
return ["exp", "iat"] | ||
|
||
@gen_auth.jwt_claims | ||
def jwt_claims() -> dict: | ||
claims = { | ||
"exp": dt.datetime.now(tz=dt.timezone.utc) + dt.timedelta(seconds=30), | ||
"iat": dt.datetime.now(tz=dt.timezone.utc) | ||
} | ||
return claims | ||
|
||
@auth.verify_jwt_credentials#TODO: Checar si debe cambiar o no | ||
def creds(username_jwt: str, password_jwt: str) -> bool: | ||
my_dict = { | ||
"username_jwt": username_jwt, | ||
"password_jwt": password_jwt | ||
} | ||
return True | ||
# return False | ||
|
||
# -------------Endpoints------------- | ||
@app.route("/") | ||
@auth.login_required(roles=["admin", "eder"]) | ||
def index(): | ||
return Response("Todo bien"), 200 | ||
|
||
@app.route("/generate_token", methods=["POST"]) | ||
@gen_auth.generate_jwt(roles=["eder", "user"]) | ||
def gen_token(token): | ||
response = { | ||
"status": "success", | ||
"token": token | ||
} | ||
return make_response(jsonify(response)), 200 | ||
|
||
@app.route("/temp") | ||
def temp(): | ||
test = (("val1", "hola"), ("val2", "prueba2")) | ||
response = { | ||
"message": "solo prueba", | ||
"test_data": test | ||
} | ||
return make_response(jsonify(response)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
try: | ||
from app import app | ||
from gevent.pywsgi import WSGIServer | ||
except ImportError as eImp: | ||
print(f"The following import ERROR occurred in {__file__}: {eImp}") | ||
|
||
if __name__== "__main__": | ||
try: | ||
# -----------------Dev mode----------------- | ||
app.run(host= "127.0.0.1", port= 5000, debug= True) | ||
# debug= True for apply changes made into the files without restarting the flask server | ||
|
||
# -----------------Prod mode---------------- | ||
#appServer= WSGIServer(("127.0.0.1", 5000), app) | ||
#appServer.serve_forever() | ||
except Exception as eImp: | ||
print(f"The following import ERROR occurred in {__file__}: {eImp}") | ||
finally: | ||
print("Finishing program") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
[metadata] | ||
name = Flask-authgen-jwt | ||
version = 1.2.4 | ||
version = 2.0.0 | ||
author = Diego Martinez and Guillermo Ortega | ||
author_email = [email protected] | ||
description = JWT authentication and generator for Flask routes | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters