-
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 #47 from dmtzs/development
Development to master
- Loading branch information
Showing
11 changed files
with
299 additions
and
11 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
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,12 @@ | ||
try: | ||
from flask import Flask | ||
from . import flask_authgen_jwt | ||
except ImportError as eImp: | ||
print(f"The following import ERROR occurred in {__file__}: {eImp}") | ||
|
||
app = Flask(__name__) | ||
app.config["JSON_SORT_KEYS"] = False | ||
gen_auth = flask_authgen_jwt.GenJwt(rsa_encrypt=True) | ||
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,101 @@ | ||
try: | ||
import datetime as dt | ||
from typing import Union | ||
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 | ||
def encode_private_key() -> dict[str, Union[str, bytes]]: | ||
file_private = "./private-key.pem" | ||
private_key = open(file_private).read().encode("ascii") | ||
|
||
encode_attributes = { | ||
"key": private_key,# In this case key should be your private key | ||
"algorithm": "RS256", | ||
"passphrase": b"your password" | ||
} | ||
return encode_attributes | ||
|
||
@auth.enc_dec_jwt_config | ||
def decode_pub_key() -> dict[str, Union[str, bytes]]: | ||
file_public = "./public-key.pem" | ||
public_key = open(file_public).read().encode("ascii") | ||
|
||
decode_attributes = { | ||
"key": public_key,# In this case key should be your public key | ||
"algorithm": "RS256" | ||
} | ||
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 | ||
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() -> Response: | ||
return make_response("Todo bien", 200) | ||
|
||
@app.route("/generate_token", methods=["POST"]) | ||
@gen_auth.generate_jwt(roles=["eder", "user"]) | ||
def gen_token(token) -> Response: | ||
response = { | ||
"status": "success", | ||
"token": token | ||
} | ||
if token is None: | ||
response["status"] = "error" | ||
response["message_error"] = "Token was not generated correctly, please verify" | ||
return make_response(jsonify(response), 500) | ||
return make_response(jsonify(response)), 200 | ||
|
||
@app.route("/temp") | ||
def temp() -> Response: | ||
test = (("val1", "hola"), ("val2", "prueba2")) | ||
response = { | ||
"message": "solo prueba", | ||
"test_data": test | ||
} | ||
return make_response(jsonify(response)) |
File renamed without changes.
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 | ||
from . 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,96 @@ | ||
try: | ||
import datetime as dt | ||
from typing import Union | ||
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 | ||
def encode_private_key() -> dict[str, Union[str, bytes]]: | ||
file_private = "./private-key.pem" | ||
private_key = open(file_private).read().encode("ascii") | ||
|
||
encode_attributes = { | ||
"key": private_key,# In this case key should be your private key | ||
"algorithm": "RS256" | ||
} | ||
return encode_attributes | ||
|
||
@auth.enc_dec_jwt_config | ||
def decode_pub_key() -> dict[str, Union[str, bytes]]: | ||
file_public = "./public-key.pem" | ||
public_key = open(file_public).read().encode("ascii") | ||
|
||
decode_attributes = { | ||
"key": public_key,# In this case key should be your public key | ||
"algorithm": "RS256" | ||
} | ||
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 | ||
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() -> Response: | ||
return make_response("Todo bien", 200) | ||
|
||
@app.route("/generate_token", methods=["POST"]) | ||
@gen_auth.generate_jwt(roles=["eder", "user"]) | ||
def gen_token(token) -> Response: | ||
response = { | ||
"status": "success", | ||
"token": token | ||
} | ||
return make_response(jsonify(response)), 200 | ||
|
||
@app.route("/temp") | ||
def temp() -> Response: | ||
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") |
File renamed without changes.
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
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