Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
C
cake-bot
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Oleksandr Barabash
cake-bot
Commits
582b1db9
Commit
582b1db9
authored
Oct 25, 2022
by
Oleksandr Barabash
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
import fixes
parent
8297969f
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
29 additions
and
22 deletions
+29
-22
config.py
config.py
+13
-9
token_helper.py
utils/token_helper.py
+16
-13
No files found.
config.py
View file @
582b1db9
...
@@ -14,15 +14,19 @@ CARDS_PATH = os.path.join(ASSETS_PATH, "cards")
...
@@ -14,15 +14,19 @@ CARDS_PATH = os.path.join(ASSETS_PATH, "cards")
class
Auth
:
class
Auth
:
""" Auth type """
""" Auth type """
TYPE
=
"jwt"
class
Types
:
""" Auth types """
RS256
=
"RS256"
BEARER
=
"Bearer"
HS256
=
"HS256"
BASIC
=
"Basic"
CURRENT
=
RS256
class
Algorithms
:
BEARER
=
"Bearer"
""" Auth Algorithms """
RS256
=
"RS256"
ALG
=
RS256
HS256
=
"HS256"
TYPE
=
Types
.
BEARER
ALGORITHM
=
Algorithms
.
RS256
TOKEN_TYPE
=
"jwt"
ADMIN_LOGIN_SECRET
=
"adminLogin"
ADMIN_LOGIN_SECRET
=
"adminLogin"
ADMIN_PASSW_SECRET
=
"adminPassword"
ADMIN_PASSW_SECRET
=
"adminPassword"
...
...
utils/token_helper.py
View file @
582b1db9
""" Token Helper """
""" Token Helper """
import
asyncio
import
asyncio
import
sys
from
calendar
import
timegm
from
calendar
import
timegm
from
concurrent.futures.thread
import
ThreadPoolExecutor
from
concurrent.futures.thread
import
ThreadPoolExecutor
from
datetime
import
datetime
,
timedelta
from
datetime
import
datetime
,
timedelta
...
@@ -11,7 +10,6 @@ from Crypto.Hash import SHA256
...
@@ -11,7 +10,6 @@ from Crypto.Hash import SHA256
from
aiohttp.web
import
Request
,
Response
from
aiohttp.web
import
Request
,
Response
from
azure.core.exceptions
import
ResourceNotFoundError
,
HttpResponseError
from
azure.core.exceptions
import
ResourceNotFoundError
,
HttpResponseError
from
config
import
Auth
from
entities.json.admin_user
import
AdminUser
from
entities.json.admin_user
import
AdminUser
from
utils.azure_key_vault_client
import
AzureKeyVaultClient
from
utils.azure_key_vault_client
import
AzureKeyVaultClient
from
utils.functions
import
b64encode_str
,
b64encode_np
,
parse_auth_header
,
\
from
utils.functions
import
b64encode_str
,
b64encode_np
,
parse_auth_header
,
\
...
@@ -39,7 +37,7 @@ class TokenHelper:
...
@@ -39,7 +37,7 @@ class TokenHelper:
""" Sign token and return "{token}.{signature}" """
""" Sign token and return "{token}.{signature}" """
from
config
import
Auth
from
config
import
Auth
if
alg
==
Auth
.
RS256
:
if
alg
==
Auth
.
Algorithms
.
RS256
:
""" RSA signature with SHA-256 """
""" RSA signature with SHA-256 """
key
=
self
.
azure_kv
.
get_or_create_random_key_bl
()
key
=
self
.
azure_kv
.
get_or_create_random_key_bl
()
header
.
update
(
dict
(
kid
=
key
.
name
))
header
.
update
(
dict
(
kid
=
key
.
name
))
...
@@ -50,7 +48,7 @@ class TokenHelper:
...
@@ -50,7 +48,7 @@ class TokenHelper:
signature_encrypted
=
self
.
azure_kv
.
encrypt_bl
(
key
,
signature
)
signature_encrypted
=
self
.
azure_kv
.
encrypt_bl
(
key
,
signature
)
signature_b64
=
b64encode_np
(
signature_encrypted
)
.
decode
(
"utf-8"
)
signature_b64
=
b64encode_np
(
signature_encrypted
)
.
decode
(
"utf-8"
)
return
"{}.{}"
.
format
(
token_unsigned
,
signature_b64
)
return
"{}.{}"
.
format
(
token_unsigned
,
signature_b64
)
elif
alg
==
Auth
.
HS256
:
elif
alg
==
Auth
.
Algorithms
.
HS256
:
""" HMAC with SHA-256 (HS256) """
""" HMAC with SHA-256 (HS256) """
pass
pass
raise
NotImplementedError
(
"'{}' ALGORITHM ISN'T SUPPORTED"
.
format
(
alg
))
raise
NotImplementedError
(
"'{}' ALGORITHM ISN'T SUPPORTED"
.
format
(
alg
))
...
@@ -61,7 +59,7 @@ class TokenHelper:
...
@@ -61,7 +59,7 @@ class TokenHelper:
date
=
datetime
.
utcnow
()
+
timedelta
(
seconds
=
ttl_seconds
)
date
=
datetime
.
utcnow
()
+
timedelta
(
seconds
=
ttl_seconds
)
exp
=
timegm
(
date
.
utctimetuple
())
exp
=
timegm
(
date
.
utctimetuple
())
alg
=
Auth
.
CURRENT
alg
=
Auth
.
ALGORITHM
jwt_head
=
dict
(
typ
=
MimeTypes
.
JWT
,
alg
=
alg
)
jwt_head
=
dict
(
typ
=
MimeTypes
.
JWT
,
alg
=
alg
)
jwt_body
=
dict
(
sub
=
login
,
exp
=
exp
)
jwt_body
=
dict
(
sub
=
login
,
exp
=
exp
)
token_signed
=
self
.
sign_token_bl
(
jwt_head
,
jwt_body
,
alg
)
token_signed
=
self
.
sign_token_bl
(
jwt_head
,
jwt_body
,
alg
)
...
@@ -76,7 +74,7 @@ class TokenHelper:
...
@@ -76,7 +74,7 @@ class TokenHelper:
if
user
.
login
==
login
and
user
.
password
==
passw
:
if
user
.
login
==
login
and
user
.
password
==
passw
:
ttl
=
3600
ttl
=
3600
token
=
self
.
create_token_bl
(
user
.
login
,
ttl
)
token
=
self
.
create_token_bl
(
user
.
login
,
ttl
)
return
dict
(
tokenType
=
Auth
.
BEARER
,
return
dict
(
tokenType
=
Auth
.
TYPE
,
expiresIn
=
ttl
,
expiresIn
=
ttl
,
accessToken
=
token
)
accessToken
=
token
)
return
None
return
None
...
@@ -88,6 +86,8 @@ class TokenHelper:
...
@@ -88,6 +86,8 @@ class TokenHelper:
def
is_token_valid
(
self
,
token
:
str
)
->
bool
:
def
is_token_valid
(
self
,
token
:
str
)
->
bool
:
""" Check if token is Valid """
""" Check if token is Valid """
from
config
import
Auth
# split first
# split first
header_b64_str
,
body_b64_str
,
signature
=
token
.
split
(
"."
)
header_b64_str
,
body_b64_str
,
signature
=
token
.
split
(
"."
)
token_unsigned
=
"{}.{}"
.
format
(
header_b64_str
,
body_b64_str
)
token_unsigned
=
"{}.{}"
.
format
(
header_b64_str
,
body_b64_str
)
...
@@ -106,11 +106,11 @@ class TokenHelper:
...
@@ -106,11 +106,11 @@ class TokenHelper:
return
False
return
False
# check type
# check type
if
token_typ
!=
Auth
.
TYPE
:
if
token_typ
!=
Auth
.
T
OKEN_T
YPE
:
return
False
return
False
# check alg
# check alg
if
token_alg
!=
Auth
.
ALG
:
if
token_alg
!=
Auth
.
ALG
ORITHM
:
return
False
return
False
# check expiration
# check expiration
...
@@ -130,20 +130,23 @@ class TokenHelper:
...
@@ -130,20 +130,23 @@ class TokenHelper:
Log
.
e
(
__name__
,
"Key not found: '{}'"
.
format
(
token_kid
))
Log
.
e
(
__name__
,
"Key not found: '{}'"
.
format
(
token_kid
))
return
False
return
False
# signature_gen = SHA256.new(token_unsigned.encode("utf-8")).digest()
signature_gen
=
SHA256
.
new
(
token_unsigned
.
encode
(
"utf-8"
))
.
digest
()
# signature_encrypted = self.azure_kv.encrypt_bl(key, signature_gen)
signature_encrypted
=
self
.
azure_kv
.
encrypt_bl
(
key
,
signature_gen
)
# signature_b64 = b64encode_np(signature_encrypted).decode("utf-8")
signature_gen
=
b64encode_np
(
signature_encrypted
)
.
decode
(
"utf-8"
)
# Log.e(__name__, exc_info=sys.exc_info())
if
signature
!=
signature_gen
:
return
False
return
True
return
True
def
is_auth
(
self
,
f
):
def
is_auth
(
self
,
f
):
""" Is auth decorator """
""" Is auth decorator """
from
config
import
Auth
async
def
wr
(
request
:
Request
)
->
Response
:
async
def
wr
(
request
:
Request
)
->
Response
:
""" Wrapper """
""" Wrapper """
a_type
,
a_value
=
parse_auth_header
(
a_type
,
a_value
=
parse_auth_header
(
request
.
headers
.
get
(
"Authorization"
)
request
.
headers
.
get
(
"Authorization"
)
)
)
if
a_type
==
Auth
.
BEARER
and
self
.
is_token_valid
(
a_value
):
if
a_type
==
Auth
.
TYPE
and
self
.
is_token_valid
(
a_value
):
return
await
f
(
request
)
return
await
f
(
request
)
return
Response
(
status
=
HTTPStatus
.
FORBIDDEN
)
return
Response
(
status
=
HTTPStatus
.
FORBIDDEN
)
return
wr
return
wr
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment