Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Request: Method to check if token is still valid #19

Open
ehennestad opened this issue May 12, 2023 · 2 comments
Open

Request: Method to check if token is still valid #19

ehennestad opened this issue May 12, 2023 · 2 comments

Comments

@ehennestad
Copy link

ehennestad commented May 12, 2023

I tried to find something about this in the documentation and the source code (so I apologise if I missed something), but it would be useful to have a method for testing whether the token has expired, and also update the token if needed.

I tried to use the ping endpoint of the sea file api, but it seems like It does not require a valid token.

Other ideas or suggestions for best practices are also appreciated.

@ehennestad
Copy link
Author

ehennestad commented May 12, 2023

Update: I found the auth/ping which is what is needed, and suggest something like this:

try:
    auth_ping_endpoint_path = '/api2/auth/ping/' # self.id
    response = drive_client.get(auth_ping_endpoint_path)
    
except ebrains_drive.exceptions.ClientHttpError as e:
    if e.code == 403:
        raise ebrains_drive.exceptions.TokenExpired(e.code, 'The token has expired')
    else:
        raise ebrains_drive.exceptions.ClientHttpError(e.code, e.message)

@xgui3783
Copy link
Contributor

xgui3783 commented Jul 27, 2023

edit: it seems you are referring to th token issued by seafile, and not by ebrains IAM. Please ignore the original post.

since the token is a standard JWT token, one could decode it with something like:

import json
from base64 import b64decode
import time

auth_token = "ey..."
tolerance=30 # if expires within 30seconds

_header, body, _sig, *_rest = auth_token.split('.')
exp = json.loads(b64decode(body.encode("utf-8") + b"====").decode("utf-8")).get("exp")

if exp - time.time() < tolerance:
    raise ebrains_drive.exceptions.TokenExpired(e.code, 'The token has expired')

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants