94 lines
2.6 KiB
Python
94 lines
2.6 KiB
Python
# -*- coding: utf-8 -*-
|
|
"""
|
|
@File: auth.py
|
|
@Date: 2020-09-14
|
|
@author: A2TEC
|
|
@section MODIFYINFO 수정정보
|
|
- 수정자/수정일 : 수정내역
|
|
- 2022-01-14/hsj100@a2tec.co.kr : refactoring
|
|
@brief: authentication api
|
|
"""
|
|
|
|
from itertools import groupby
|
|
from operator import attrgetter
|
|
from fastapi import APIRouter, Depends
|
|
from sqlalchemy.orm import Session
|
|
import bcrypt
|
|
import jwt
|
|
from datetime import datetime, timedelta
|
|
|
|
from rest.app.common import consts
|
|
from rest.app import models as M
|
|
from rest.app.database.conn import db
|
|
from rest.app.common.config import conf
|
|
from rest.app.database.schema import Users, UserLog
|
|
from rest.app.utils.extra import query_to_groupby, AESCryptoCBC
|
|
from rest.app.utils.date_utils import D
|
|
|
|
router = APIRouter(prefix='/auth')
|
|
|
|
|
|
@router.get('/find-account/{account}', response_model=M.ResponseBase, summary='계정유무 검사')
|
|
async def find_account(account: str):
|
|
"""
|
|
## 계정유무 검사
|
|
|
|
주어진 계정이 존재하면 true, 없으면 false 처리
|
|
|
|
**결과**
|
|
- ResponseBase
|
|
"""
|
|
try:
|
|
search_info = Users.get(account=account)
|
|
if not search_info:
|
|
raise Exception(f'not found data: {account}')
|
|
return M.ResponseBase()
|
|
except Exception as e:
|
|
return M.ResponseBase.set_error(str(e))
|
|
|
|
|
|
@router.post('/logout/{account}', status_code=200, response_model=M.TokenRes, summary='사용자 접속종료')
|
|
async def logout(account: str):
|
|
"""
|
|
## 사용자 접속종료
|
|
|
|
현재 버전에서는 로그인/로그아웃의 상태를 유지하지 않고 상태값만을 서버에서 사용하기 때문에,\n
|
|
***로그상태는 실제상황과 다를 수 있다.***
|
|
|
|
정상처리시 Authorization(null) 반환
|
|
|
|
**결과**
|
|
- TokenRes
|
|
"""
|
|
user_info = None
|
|
|
|
try:
|
|
# TODO(hsj100): LOGIN_STATUS
|
|
user_info = Users.filter(account=account)
|
|
if not user_info:
|
|
raise Exception('not found user')
|
|
|
|
user_info.update(auto_commit=True, login='logout')
|
|
return M.TokenRes()
|
|
except Exception as e:
|
|
if user_info:
|
|
user_info.close()
|
|
return M.ResponseBase.set_error(e)
|
|
|
|
|
|
async def is_account_exist(account: str):
|
|
get_account = Users.get(account=account)
|
|
return True if get_account else False
|
|
|
|
|
|
def create_access_token(*, data: dict = None, expires_delta: int = None):
|
|
|
|
if conf().GLOBAL_TOKEN:
|
|
return conf().GLOBAL_TOKEN
|
|
|
|
to_encode = data.copy()
|
|
if expires_delta:
|
|
to_encode.update({'exp': datetime.utcnow() + timedelta(hours=expires_delta)})
|
|
encoded_jwt = jwt.encode(to_encode, consts.JWT_SECRET, algorithm=consts.JWT_ALGORITHM)
|
|
return encoded_jwt
|