Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
E
esap-userprofile-python-client
Manage
Activity
Members
Labels
Plan
Wiki
Jira
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Model registry
Operate
Terraform modules
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
GitLab community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
ASTRON SDC
ESCAPE WP5
esap-userprofile-python-client
Commits
0871d286
Commit
0871d286
authored
3 years ago
by
Klaas Kliffen
Browse files
Options
Downloads
Patches
Plain Diff
Check expiry of JWT before creating the header
parent
a0411f33
Branches
Branches containing commit
No related tags found
1 merge request
!8
SDC-307 Cern dlaas notebook
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
shopping_client/shopping_client.py
+20
-3
20 additions, 3 deletions
shopping_client/shopping_client.py
with
20 additions
and
3 deletions
shopping_client/shopping_client.py
+
20
−
3
View file @
0871d286
import
base64
import
getpass
import
json
import
time
import
urllib.parse
from
os
import
getenv
from
typing
import
Optional
,
Union
from
warnings
import
warn
from
os
import
getenv
import
pandas
as
pd
import
requests
...
...
@@ -15,7 +17,7 @@ class shopping_client:
def
__init__
(
self
,
token
:
str
=
None
,
token
:
Optional
[
str
]
=
None
,
host
:
str
=
"
http://localhost:5555/
"
,
connectors
:
list
=
[],
):
...
...
@@ -88,8 +90,20 @@ class shopping_client:
return
self
.
basket
def
_is_valid_token
(
token
:
Optional
[
str
])
->
bool
:
"""
Checks expiry of the token
"""
if
token
is
None
:
return
False
try
:
payload
=
json
.
loads
(
base64
.
b64decode
(
token
.
split
(
"
.
"
)[
1
]))
return
payload
[
"
exp
"
]
>
int
(
time
.
time
())
+
10
except
KeyError
:
raise
RuntimeError
(
"
Invalid JWT format
"
)
def
_request_header
(
self
):
while
self
.
token
is
None
:
while
not
self
.
_is_valid_token
(
self
.
token
)
:
self
.
_get_token
()
return
dict
(
Accept
=
"
application/json
"
,
Authorization
=
f
"
Bearer
{
self
.
token
}
"
)
...
...
@@ -139,3 +153,6 @@ class shopping_client:
self
.
token
=
token_file
.
readline
()
else
:
self
.
token
=
getpass
.
getpass
(
"
Enter your ESAP access token:
"
)
if
self
.
token
is
None
:
raise
RuntimeError
(
"
No token found!
"
)
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