Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
Odevzdávací Systém MO
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Deploy
Model registry
Analyze
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Martin Mareš
Odevzdávací Systém MO
Commits
fc7fa83b
Commit
fc7fa83b
authored
3 years ago
by
Martin Mareš
Browse files
Options
Downloads
Patches
Plain Diff
mo.tokens: Obecný parametrizovaný podpis a hash
parent
245e6be8
No related branches found
No related tags found
1 merge request
!86
Registrace
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
mo/tokens.py
+17
-3
17 additions, 3 deletions
mo/tokens.py
with
17 additions
and
3 deletions
mo/tokens.py
+
17
−
3
View file @
fc7fa83b
# Podepsané tokeny
import
hashlib
import
hmac
import
urllib.parse
from
typing
import
Optional
,
List
...
...
@@ -8,9 +9,7 @@ import mo.config as config
def
_sign_token
(
token
:
str
,
use
:
str
)
->
str
:
key
=
'
-
'
.
join
((
'
sign-token
'
,
use
,
config
.
SECRET_KEY
))
mac
=
hmac
.
HMAC
(
key
.
encode
(
'
us-ascii
'
),
token
.
encode
(
'
us-ascii
'
),
'
sha256
'
)
return
mac
.
hexdigest
()[:
16
]
return
sign
(
token
,
'
token-
'
+
use
)
def
sign_token
(
fields
:
List
[
str
],
use
:
str
)
->
str
:
...
...
@@ -27,3 +26,18 @@ def verify_token(token: str, use: str) -> Optional[List[str]]:
if
_sign_token
(
'
:
'
.
join
(
enc_fields
),
use
)
!=
sign
:
return
None
return
[
urllib
.
parse
.
unquote
(
f
)
for
f
in
enc_fields
]
def
sign
(
msg
:
str
,
use
:
str
)
->
str
:
"""
Podpis parametrizovaný tajným klíčem a účelem.
"""
key
=
use
+
'
#
'
+
config
.
SECRET_KEY
mac
=
hmac
.
HMAC
(
key
.
encode
(
'
us-ascii
'
),
msg
.
encode
(
'
us-ascii
'
),
'
sha256
'
)
return
mac
.
hexdigest
()[:
16
]
def
hash
(
msg
:
str
,
use
:
str
)
->
str
:
"""
Hešovací funkce parametrizovaná tajným klíčem a účelem.
"""
m
=
'
#
'
.
join
((
use
,
config
.
SECRET_KEY
,
msg
)).
encode
(
'
us-ascii
'
)
return
hashlib
.
sha256
(
m
).
hexdigest
()
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment