Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Contribute to GitLab
Sign in
Toggle navigation
J
jwt
Project
Project
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
Мырзабеков Бекайдар
jwt
Commits
f6cb4690
Commit
f6cb4690
authored
Nov 29, 2023
by
Мырзабеков Бекайдар
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
added task_3
parent
841d3b17
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
76 additions
and
0 deletions
+76
-0
task_3.py
task_3.py
+76
-0
No files found.
task_3.py
0 → 100644
View file @
f6cb4690
import
hashlib
import
json
import
hmac
import
base64
from
datetime
import
datetime
,
timedelta
import
jwt
SECRET_KEY
=
'BwzBFTfdNJfVWi2CY1BK9KQBDdb5auQR'
# Get tokens by jwt
def
get_access_token
(
username
:
str
)
->
str
:
payload
=
{
"sub"
:
username
,
"exp"
:
datetime
.
utcnow
()
+
timedelta
(
minutes
=
30
)
}
return
jwt
.
encode
(
payload
,
SECRET_KEY
,
algorithm
=
'HS256'
)
def
get_refresh_token
(
username
:
str
)
->
str
:
payload
=
{
"sub"
:
username
,
"exp"
:
datetime
.
utcnow
()
+
timedelta
(
days
=
30
)
}
return
jwt
.
encode
(
payload
,
SECRET_KEY
,
algorithm
=
'HS256'
)
def
get_tokens_by_lib
(
username
:
str
)
->
dict
:
access_token
=
get_access_token
(
username
)
refresh_token
=
get_refresh_token
(
username
)
return
{
"access_token"
:
access_token
,
"refresh_token"
:
refresh_token
}
# Get tokens
def
get_tokens
(
username
:
str
)
->
dict
:
header
=
{
"alg"
:
"HS256"
,
"typ"
:
"JWT"
}
access_payload
=
{
"sub"
:
f
"{username}"
,
"exp"
:
(
datetime
.
utcnow
()
+
timedelta
(
minutes
=
30
))
.
isoformat
()
}
refresh_payload
=
{
"sub"
:
f
"{username}"
,
"exp"
:
(
datetime
.
utcnow
()
+
timedelta
(
days
=
30
))
.
isoformat
()
}
encoded_header
=
base64
.
urlsafe_b64encode
(
json
.
dumps
(
header
)
.
encode
()
)
.
decode
(
'utf-8'
)
encoded_access_payload
=
base64
.
urlsafe_b64encode
(
json
.
dumps
(
access_payload
)
.
encode
()
)
.
decode
(
'utf-8'
)
encoded_refresh_payload
=
base64
.
urlsafe_b64encode
(
json
.
dumps
(
refresh_payload
)
.
encode
()
)
.
decode
(
'utf-8'
)
unsigned_access_token
=
encoded_header
+
'.'
+
encoded_access_payload
unsigned_refresh_token
=
encoded_header
+
'.'
+
encoded_refresh_payload
access_signature
=
hmac
.
new
(
SECRET_KEY
.
encode
(),
unsigned_access_token
.
encode
(),
hashlib
.
sha256
)
.
digest
()
refresh_signature
=
hmac
.
new
(
SECRET_KEY
.
encode
(),
unsigned_refresh_token
.
encode
(),
hashlib
.
sha256
)
.
digest
()
access
=
encoded_header
+
'.'
+
encoded_access_payload
+
'.'
+
base64
.
urlsafe_b64encode
(
access_signature
)
.
decode
()
refresh
=
encoded_header
+
'.'
+
encoded_refresh_payload
+
'.'
+
base64
.
urlsafe_b64encode
(
refresh_signature
)
.
decode
()
return
{
"access"
:
access
,
"refresh"
:
refresh
}
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