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

Feature: Autorenew OAuth tokens #402

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions okta/oauth.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from urllib.parse import urlencode, quote
from okta.jwt import JWT
from okta.http_client import HTTPClient
from okta.utils import is_jwt_token_expired


class OAuth:
Expand Down Expand Up @@ -37,8 +38,8 @@ async def get_access_token(self):
str, Exception: Tuple of the access token, error that was raised
(if any)
"""
# Return token if already generated
if self._access_token:
# Return token if already generated and token is not expired.
if self._access_token and not is_jwt_token_expired(self._access_token):
return (self._access_token, None)

# Otherwise create new one
Expand Down
6 changes: 6 additions & 0 deletions okta/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
from enum import Enum
from datetime import datetime as dt
from urllib.parse import urlsplit, urlunsplit
from jose.jwt import get_unverified_claims
import time

from okta.constants import DATETIME_FORMAT, EPOCH_DAY, EPOCH_MONTH,\
EPOCH_YEAR
Expand Down Expand Up @@ -74,3 +76,7 @@ def convert_absolute_url_into_relative_url(absolute_url):
"""
url_parts = urlsplit(absolute_url)
return urlunsplit(('', '', url_parts[2], url_parts[3], url_parts[4]))

def is_jwt_token_expired(jwt_token):
claims = get_unverified_claims(jwt_token)
return time.time() > claims['exp']