@ -18,9 +18,10 @@ from . import ServerBC
# объявление import
from fastapi import FastAPI , Form , Request , HTTPException , Depends , Header , Response , Body
from fastapi . responses import PlainTextResponse , HTMLResponse , FileResponse , RedirectResponse
from fastapi . responses import PlainTextResponse , HTMLResponse , FileResponse , RedirectResponse , JSONResponse
from fastapi . staticfiles import StaticFiles
from fastapi . templating import Jinja2Templates
from fastapi . encoders import jsonable_encoder
from starlette . datastructures import MutableHeaders
from pydantic import BaseModel
import uvicorn
@ -74,11 +75,11 @@ def IdentifyAuthorize(inRequest:Request, inResponse:Response,
return lCookieAuthToken
######################################
#Way 2 - try to logon
if len ( lHeaderAuthorization ) == 2 :
if lHeaderAuthorization != [ ' ' ] :
if " AuthExc " in lCookies :
raise AuthException ( )
else :
llHeaderAuthorizationDecodedUserPasswordList = base64 . b64decode ( lHeaderAuthorization [ 1 ] ) . decode ( " utf-8 " ) . split ( " : " )
llHeaderAuthorizationDecodedUserPasswordList = base64 . b64decode ( lHeaderAuthorization [ 0 ] ) . decode ( " utf-8 " ) . split ( " : " )
lUser = llHeaderAuthorizationDecodedUserPasswordList [ 0 ]
lPassword = llHeaderAuthorizationDecodedUserPasswordList [ 1 ]
lDomain = " "
@ -114,9 +115,7 @@ def IdentifyAuthorize(inRequest:Request, inResponse:Response,
mOpenRPA [ " Domain " ] = lResult [ " Domain " ]
mOpenRPA [ " User " ] = lResult [ " User " ]
mOpenRPA [ " IsSuperToken " ] = __Orchestrator__ . GSettingsGet ( ) . get ( " ServerDict " , { } ) . get ( " AccessUsers " , { } ) . get ( " AuthTokensDict " , { } ) . get ( mOpenRPA [ " AuthToken " ] , { } ) . get ( " FlagDoNotExpire " , False )
try : inResponse . delete_cookie ( key = " AuthExc " )
except Exception : pass
return lAuthToken
raise ReloadPage ( token = lAuthToken )
#inRequest.OpenRPASetCookie = {}
#New engine of server
#inRequest.OpenRPAResponseDict["SetCookies"]["AuthToken"] = lAuthToken
@ -151,27 +150,40 @@ class ErrorException(Exception):
self . text = text
class AuthException ( Exception ) :
def __init__ ( self , name : str = " AuthTryWindowCreate " ) :
def __init__ ( self , name : str = " AuthTryWindowCreate " ) :
self . name = name
class ReloadPage ( Exception ) :
def __init__ ( self , token : str , name : str = " AuthToken " ) :
self . name = name
self . token = token
templates = Jinja2Templates ( directory = CrossOS . PathJoinList ( CrossOS . PathSplitList ( __file__ ) [ : - 2 ] + [ " Resources " , " Web " , " orpa " ] ) )
# Обработчик ошибки авторизации (вывод информации о причинах неудачной авторизации)
@app.exception_handler ( ErrorException )
async def unicorn_exception_handler ( request : Request , exc : ErrorException ) :
response = templates . TemplateResponse ( status_code = 401 , name = " badAuth.xhtml " , context = { " request " : request , " errorMsg " : exc . text , " title " : " О Р К Е С Т Р А Т О Р PYOPENRPA" , " subtitle " : " ПАНЕЛЬ УПРАВЛЕН ИЯ" , " version " : __version__ } )
response . set_cookie ( key = exc . name , value = " True " )
response = templates . TemplateResponse ( status_code = 401 , name = " badAuth.xhtml " , context = { " request " : request , " errorMsg " : exc . text , " title " : " О Р К Е С Т Р А Т О Р PYOPENRPA" , " subtitle " : " АВТОРИЗАЦ ИЯ" , " version " : __version__ } )
response . set_cookie ( key = " AuthExc " , value = " True " )
return response
# Обработчик попытки авторизации (отвечает за вызов окна для ввода пары логин / пароль)
@app.exception_handler ( AuthException )
async def unicorn_exception_handler_2 ( request : Request , exc : AuthException ) :
response = HTMLResponse ( status_code = 401 , headers = { ' Content-type ' : ' text/html; charset=utf-8 ' , ' WWW-Authenticate ' : ' Basic ' } )
# Обработчик успешной попытки авторизации (обновление страницы + установки куки-токена)
@app.exception_handler ( ReloadPage )
async def unicorn_exception_handler_3 ( request : Request , exc : ReloadPage ) :
response = HTMLResponse ( content = " " , status_code = 200 )
response . set_cookie ( key = exc . name , value = exc . token )
try : response . delete_cookie ( key = " AuthExc " )
except Exception : pass
return response
# Обработчик попытки авторизации (отвечает за рендер формы для ввода пары логин / пароль)
@app.exception_handler ( AuthException )
def unicorn_exception_handler_2 ( request : Request , exc : AuthException ) :
response = templates . TemplateResponse ( status_code = 401 , name = " auth.xhtml " , context = { " request " : request , " title " : " О Р К Е С Т Р А Т О Р PYOPENRPA" , " subtitle " : " АВТОРИЗАЦИЯ " , " version " : __version__ } )
try : response . delete_cookie ( key = " AuthExc " )
except Exception : pass
return response