Skip to content

Commit

Permalink
getname token
Browse files Browse the repository at this point in the history
  • Loading branch information
marceloarocha committed Nov 26, 2024
1 parent 003620b commit c5de98f
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 0 deletions.
36 changes: 36 additions & 0 deletions routes/names.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
import logging
from flask import Blueprint, request
from flask_jwt_extended import jwt_required, get_jwt_identity
from jwt import encode
from datetime import datetime, timedelta, timezone

from models.main import User, dbSession, db
from models.appendix import SchemaConfig
Expand Down Expand Up @@ -125,6 +127,40 @@ def proxy_multiple():
return names, status.HTTP_200_OK


@app_names.route("/names/auth-token", methods=["GET"])
@jwt_required()
def auth_token():
user = User.find(get_jwt_identity())
dbSession.setSchema(user.schema)

schema_config = (
db.session.query(SchemaConfig)
.filter(SchemaConfig.schemaName == user.schema)
.first()
)

getname_config = (
schema_config.config.get("getname", {}) if schema_config.config else {}
)
key = getname_config.get("secret", "")

if not key:
return {
"status": "error",
"message": "Invalid key",
}, status.HTTP_400_BAD_REQUEST

token = encode(
payload={
"exp": datetime.now(tz=timezone.utc) + timedelta(minutes=2),
"iss": "noharm",
},
key=key,
)

return {"status": "success", "data": token}


def _get_token(config):
token_url = config["getname"]["token"]["url"]
params = config["getname"]["token"]["params"]
Expand Down
5 changes: 5 additions & 0 deletions services/auth_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,10 @@ def _auth_user(
extra=extra_audit,
)

getname_config = (
schema_config.config.get("getname", {}) if schema_config.config else {}
)

return {
"status": "success",
"userName": user.name,
Expand All @@ -190,6 +194,7 @@ def _auth_user(
"nameUrl": nameUrl["value"] if "value" in nameUrl else None,
"multipleNameUrl": nameUrl["multiple"] if "multiple" in nameUrl else None,
"nameHeaders": nameUrl["headers"] if "headers" in nameUrl else {},
"getnameAuth": getname_config.get("auth", False),
"proxy": nameUrl["proxy"] if "proxy" in nameUrl else False,
"notify": notification,
"access_token": access_token,
Expand Down

0 comments on commit c5de98f

Please sign in to comment.