Files
boc/landvex-admin-backend/app/__pycache__/security.cpython-39.pyc
T

20 lines
3.0 KiB
Plaintext
Raw Normal View History

a
X3GjT
ã@s6dZddlmZmZddlmZmZddlmZddlm Z m
Z
ddl m Z ddl
mZddlmZeƒZe d gd
d Zeeed œd
dZeedœddZd(eeefeeedœddZeeefedœddZeeedœddZejdejdejdejdiZee dœd d!„Z!eeed"œd#d$„Z"ed%œd&d'„Z#dS))u@Säkerhetsfunktioner: lösenordshashning, JWT-hantering, roller.é)ÚdatetimeÚ timedelta)ÚOptionalÚUnion)ÚUUID)ÚjwtÚJWTError)Ú CryptContext)Ú get_settings)ÚUserRoleZbcryptÚauto)ZschemesÚ
deprecated)Úplain_passwordÚhashed_passwordÚreturncCs t ||¡S©N)Ú pwd_contextÚverify)rr©rúE/home/bernt/.openclaw/workspace/landvex-admin-backend/app/security.pyÚverify_passwordsr)ÚpasswordrcCs
t |¡Sr)rÚhash)rrrrÚget_password_hashsrN)ÚsubjectÚ
expires_deltarcCsN|rt ¡|}nt ¡ttjd}|t|ƒddœ}tj|tjtj d}|S)N)ÚminutesÚaccess©ÚexpÚsubÚtype©Ú algorithm)
rÚutcnowrÚsettingsÚACCESS_TOKEN_EXPIRE_MINUTESÚstrrÚencodeÚ
SECRET_KEYÚ ALGORITHM)rrÚexpireÚ to_encodeÚ encoded_jwtrrrÚcreate_access_tokens r.)rrcCs<t ¡ttjd}|t|ƒddœ}tj|tjtj d}|S)N)ÚdaysÚrefreshrr")
rr$rr%ÚREFRESH_TOKEN_EXPIRE_DAYSr'rr(r)r*)rr+r,r-rrrÚcreate_refresh_token#sr2)ÚtokenrcCs6ztj|tjtjgd}|WSty0YdS0dS)N)Z
algorithms)rÚdecoder%r)r*r)r3ÚpayloadrrrÚ decode_token*s
 r6ééé)ÚrolercCs t |d¡S)Nr)ÚROLE_HIERARCHYÚget)r:rrrÚ
role_level<sr=)Ú manager_roleÚ target_rolercCst|ƒt|ƒkS)uLReturnerar True om manager_role har högre eller lika nivÃ¥ som target_role.)r=)r>r?rrrÚ
can_manage@sr@)Úmin_rolecs*ddlmmtdœfdd }|S)u5Factory för roll-krav. Används i dependency-kedjan.r)Ú
HTTPExceptionÚstatus©Zcurrent_user_rolecs,t|ƒtˆƒkr(ˆˆjdˆjdddS)NzRequires role z
or higher)Ú status_codeÚdetailT)r=ÚHTTP_403_FORBIDDENÚvaluerD©rBrArCrrÚcheckerIs  þzrequire_role.<locals>.checker)ÚfastapirBrCr )rArJrrIrÚ require_roleEsrL)N)$Ú__doc__rrÚtypingrrÚuuidrZjoserrZpasslib.contextr Ú