Ejemplo n.º 1
0
def login_sso(request, data, db: Session):
    session_id = data["session_id"]
    user_id = int(str(session_id).split("@")[0])

    user_info = user_manager.get_user_info_by_user_id(user_id, db)
    if not user_info:
        return api_response_data("error_user_not_existed")

    client_ids = application_manager.get_list_enable_sso_client_ids(db)

    if data["client_id"] not in client_ids:
        return api_response_data("error_application_not_enable_sso")

    sso_session_obj = session_manager.get_session_by_user_id(user_id, db)
    if not sso_session_obj:
        return api_response_data("error_sso_session_not_found")

    if sso_session_obj.expired_time < get_timestamp():
        return api_response_data("error_sso_session_expired")

    token_obj = login_manager.get_token_obj(
        data["app_id"], data["client_id"], data["client_secret"], data["redirect_url"], user_info, data["algorithm"], db
    )

    return api_response_data("success", {"token_obj": token_obj})
Ejemplo n.º 2
0
        def _func(request: Request, data, db, header, *args, **kwargs):
            data_body = data
            try:
                data_body["authorization_code"] = header.Authorization_Code
                data_body["client_id"] = header.Client_Id
                if hasattr(header, 'Access_Token'):
                    data_body["access_token"] = header.Access_Token
            except Exception as err:
                return api_response_data("error_header")

            # get application info
            app_info = application_manager.get_application_info_by_client_id(
                data_body["client_id"], db)
            if not app_info:
                return api_response_data("error_client_id_incorrect")

            data_body["app_id"] = app_info.id
            data_body["redirect_url"] = app_info.redirect_url
            data_body["client_secret"] = app_info.client_secret
            data_body["algorithm"] = app_info.algorithm

            # check Authorization
            if "Basic" not in data_body["authorization_code"]:
                return api_response_data(
                    "error_formatted_authorization_incorrect")

            if not check_valid_authorization(data_body["authorization_code"],
                                             data_body["client_id"],
                                             data_body["client_secret"]):
                return api_response_data("error_authorization_incorrect")

            return func(request, data_body, db, *args, **kwargs)
Ejemplo n.º 3
0
        def _func(request: Request, data, db, *args, **kwargs):
            if not data.get("access_token"):
                return api_response_data("error_access_token_not_found")

            if "Bearer" not in data["access_token"]:
                return api_response_data("error_access_token_wrong_format")

            is_valid, result_code, object_data = login_manager.check_access_token(
                data["access_token"], data["app_id"], data["client_id"],
                data["client_secret"], data['algorithm'], db)

            if not is_valid or result_code != "success":
                return api_response_data(result_code)

            data["_session"] = object_data
            return func(request, data, db, *args, **kwargs)
Ejemplo n.º 4
0
def verify_authorization_code(request, data, db: Session):
    state = data["state"]
    code = data["code"]
    if code != login_manager.get_authorization_code(state):
        return api_response_data("error_auth_code_incorrect")

    user_id = int(state.split("_")[0])
    user_info = user_manager.get_user_info_by_user_id(user_id, db)
    if not user_info:
        return api_response_data("error_user_not_existed")

    token_obj = login_manager.get_token_obj(
        data["app_id"], data["client_id"], data["client_secret"], data["redirect_url"], user_info, data["algorithm"], db
    )

    return api_response_data("success", {"token_obj": token_obj})
Ejemplo n.º 5
0
def create_user(request, data, db: Session):
    affected_count = user_manager.create_user_info(data["user_name"],
                                                   data["password"],
                                                   data["full_name"],
                                                   data["email"],
                                                   data["phone"], db)
    return api_response_data("success", {"affected_count": affected_count})
Ejemplo n.º 6
0
def login(request, data, db: Session):
    user_info = user_manager.get_user_info_by_user_name(data["user_name"], db)
    if not user_info:
        return api_response_data("error_user_not_existed", {})

    # check password
    if user_manager.check_password_user(data["password"], user_info.password):
        url = login_manager.generate_authorization_code(
            app_id=data["app_id"],
            client_id=data["client_id"],
            client_secret=data["client_secret"],
            redirect_url=data["redirect_url"],
            user_info=user_info,
            algorithm=data["algorithm"],
            db=db
        )
        return api_response_data("success", {"redirect_url": url})

    return api_response_data("error_password_not_correct", None)
Ejemplo n.º 7
0
def update_application(request, data, db: Session):
    app_id = data["id"]
    app_name = data.get("app_name", None)
    redirect_url = data.get("redirect_url", None)
    algorithm = data.get("algorithm", None)
    enable_sso = data.get("enable_sso", None)

    app_info = application_manager.get_application_info_by_app_id(app_id, db)
    if not app_info:
        return api_response_data("error_application_not_existed")

    affected_count = application_manager.update_application(
        db=db,
        app_id=app_id,
        app_name=app_name,
        redirect_url=redirect_url,
        algorithm=algorithm,
        enable_sso=enable_sso)
    return api_response_data("success", {"affected_count": affected_count})
Ejemplo n.º 8
0
def register_application(request, data, db: Session):
    app_name = data["app_name"]
    redirect_url = data.get("redirect_url", "")
    algorithm = data.get("algorithm", "HS256")
    enable_sso = data.get("enable_sso", False)

    # check application is existed or not
    app_model = application_manager.get_application_info_by_app_name(
        app_name, db)
    if app_model:
        return api_response_data("error_application_existed")
    affected_count = application_manager.register_application(
        db,
        app_name=app_name,
        redirect_url=redirect_url,
        algorithm=algorithm,
        enable_sso=enable_sso)

    return api_response_data("success", {"affected_count": affected_count})
Ejemplo n.º 9
0
def hello_world(item: str, header=Depends(PreLoginHeader)):
    return api_response_data(
        "success",
        {
            "headers": {
                "Authorization": header.Authorization_Code,
                # "Access_Token": header.Access_Token
            },
            "item": item
        })
Ejemplo n.º 10
0
def get_user_ids(request, data, db: Session):
    user_name = data.get("user_name", None)
    email = data.get("email", None)
    phone = data.get("phone", None)

    user_ids = user_manager.filter_user_infos(db,
                                              user_name,
                                              email=email,
                                              phone=phone)

    return api_response_data("success", {"user_ids": user_ids})
Ejemplo n.º 11
0
        def _func(request: Request,
                  db: Session = Depends(get_db),
                  header=Depends(header_schema)):
            data = {}
            try:
                data["authorization_code"] = header.Authorization_Code
                data["client_id"] = header.Client_Id
                if hasattr(header, 'Access_Token'):
                    data["access_token"] = header.Access_Token
            except Exception as err:
                return api_response_data("error_header")

            # get application info
            app_info = application_manager.get_application_info_by_client_id(
                data["client_id"], db)
            if not app_info:
                return api_response_data("error_client_id_incorrect")

            data["app_id"] = app_info.id
            data["redirect_url"] = app_info.redirect_url
            data["client_secret"] = app_info.client_secret
            data["algorithm"] = app_info.algorithm

            return func(request, data, db, *args, **kwargs)
Ejemplo n.º 12
0
def get_user_infos(request, data, db: Session):
    user_ids = data["user_ids"]
    user_infos = user_manager.get_user_infos_by_ids(user_ids, db)
    ls_data = []
    if user_infos:
        for info in user_infos:
            ls_data.append({
                "id": info.id,
                "user_name": info.user_name,
                "full_name": info.full_name,
                "email": info.email,
                "phone": info.phone,
                "is_deleted": info.is_deleted,
                "created_time": info.created_time,
                "updated_time": info.updated_time
            })

    return api_response_data("success", {"user_infos": ls_data})
Ejemplo n.º 13
0
def logout(request, data, db: Session):
    token_id = data["_session"]["token_id"]
    token_manager.update_user_access_token(token_id, "", 0, db)
    return api_response_data("success")