Commit 553e7c38 authored by Anthony Jacob's avatar Anthony Jacob
Browse files

add last login date management

parent 483ef758
Loading
Loading
Loading
Loading
+2 −2
Original line number Diff line number Diff line
@@ -44,8 +44,8 @@ def login():
    # Create tokens
    # access_token = create_access_token(identity={"username": username, "role": user["role"]})
    # refresh_token = create_refresh_token(identity={"username": username, "role": user["role"]})
    access_token = create_access_token(identity={"username": username})
    refresh_token = create_refresh_token(identity={"username": username})
    access_token = create_access_token(identity={"username": username, "id": user["id"]})
    refresh_token = create_refresh_token(identity={"username": username, "id": user["id"]})

    return jsonify({"access_token": access_token, "refresh_token": refresh_token})

+10 −6
Original line number Diff line number Diff line
@@ -2,6 +2,7 @@ from flask import request, jsonify
from flask.cli import F
from model.security import check_api_key, check_token, check_auth, revoke_jwt
from model.user.user import updateLastLogin, getUserByLogin
from model.apikey.apikey import updateLastUsage
import datetime
from flask_jwt_extended import (
    get_jwt_identity,
@@ -10,9 +11,10 @@ from flask_jwt_extended import (
    verify_jwt_in_request
)
from jwt.exceptions import DecodeError

from functools import wraps

def require_auth(f):
    @wraps(f)
    def decorated_function(*args, **kwargs):
        successAuth: bool = False
        key = request.headers.get("X-API-KEY")
@@ -22,10 +24,15 @@ def require_auth(f):
        if key:
            print("check access by API KEY")
            successAuth = check_api_key(key)
            if(successAuth):
                updateLastUsage(key)

        elif auth_header and auth_header.startswith("Bearer "):
            print("check access by regular Bearer Token")
            successAuth = check_token(auth_header)
            if(not successAuth):
            if(successAuth):
                updateLastUsage(auth_header.split(" ")[1])
            else:
                print("check access by JWT Bearer Token")
                try:
                    if(verify_jwt_in_request(optional=True)):
@@ -53,15 +60,11 @@ def require_auth(f):
                    print(type(e))
                    raise



        elif auth and auth.username and auth.password:
            print("check access by Basic Auth")
            successAuth = check_auth(auth.username, auth.password)
            updateLastLogin(auth.username)



        if successAuth:
            return f(*args, **kwargs)
        else:
@@ -69,3 +72,4 @@ def require_auth(f):

    decorated_function.__name__ = f.__name__  # Maintain function name
    return decorated_function
+5 −9
Original line number Diff line number Diff line
@@ -66,13 +66,6 @@ def getUserByLogin(userLogin: str) -> Any | Literal[-1] | Literal[False]:
        return False


def isLanguagLoginExists(userLogin: str):
    User = getUserByLogin(userLogin)
    if User and User != -1:
        return True

    return False


def insertUser(userLogin: str, password:str,  is_active: bool, force_jwt_reconnect: bool , last_login: datetime  ) -> bool | int:

@@ -159,6 +152,9 @@ def updateUser(id: int, userLogin: str, password: str , is_active: bool, force_j
                    update_values.append(force_jwt_reconnect)

                if last_login is not None:
                    if last_login == "NULL":
                        update_fields.append("last_login = NULL")
                    else:
                        update_fields.append("last_login = %s")
                        update_values.append(last_login)