Skip to content

Commit

Permalink
add refresh token with 3leg
Browse files Browse the repository at this point in the history
  • Loading branch information
chuongmep committed Sep 4, 2024
1 parent 2ae09d0 commit e81e88c
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 2 deletions.
5 changes: 4 additions & 1 deletion APSToolkitPython/src/aps_toolkit/Auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,10 @@ def refresh_new_token(self, old_refresh_token: str) -> Token:
raise Exception(response.reason)
content = response.json()
self.access_token = content['access_token']
self.expires_in = content['expires_in']
second = content['expires_in']
now = datetime.datetime.now()
expires = now + datetime.timedelta(seconds=second)
self.expires_in = expires.timestamp()
self.token_type = content['token_type']
self.refresh_token = content.get('refresh_token')
result = Token(self.access_token, self.token_type, self.expires_in, self.refresh_token)
Expand Down
38 changes: 37 additions & 1 deletion APSToolkitPython/src/aps_toolkit/Token.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
import base64
from enum import Enum
import time

import datetime

class RevokeType(Enum):
TOKEN_PUBLIC = 1
Expand Down Expand Up @@ -73,6 +73,42 @@ def is_expired(self, buffer_minutes=0) -> bool:
return True
return False

def refresh(self, client_id: str = None, client_secret: str = None, refresh_token: str = None):
"""
Refresh the access token
:param client_id: the client id of the application
:param client_secret: the client secret of the application
:param refresh_token: the refresh token used to get a new access token
"""
host = "https://developer.api.autodesk.com"
url = "/authentication/v2/token"
if client_id is None:
client_id = os.getenv("APS_CLIENT_ID")
if client_secret is None:
client_secret = os.getenv("APS_CLIENT_SECRET")
if refresh_token is None:
refresh_token = self.refresh_token
if refresh_token is None:
raise Exception("refresh_token is not provided, please provide the refresh_token or use Auth3leg")
# body
body = {
"client_id": client_id,
"client_secret": client_secret,
"grant_type": "refresh_token",
"refresh_token": self.refresh_token
}
response = requests.post(host + url, data=body)
if response.status_code != 200:
raise Exception(response.reason)
content = response.json()
self.access_token = content['access_token']
second = content['expires_in']
now = datetime.datetime.now()
expires = now + datetime.timedelta(seconds=second)
self.expires_in = expires.timestamp()
self.token_type = content['token_type']
self.refresh_token = content.get('refresh_token')

def introspect(self, client_type: ClientType) -> dict:
"""
Examines an access token including the reference token and returns the status information of the tokens.
Expand Down
7 changes: 7 additions & 0 deletions APSToolkitPython/src/test/test_token.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import datetime
import time


class TestAuth(TestCase):

def test_revoke_token_private(self):
Expand All @@ -32,3 +33,9 @@ def test_is_expired(self):
self.assertTrue(token.is_expired())
token.expires_in = time.time() + 1 * 60
self.assertFalse(token.is_expired())

def test_refresh_token(self):
token = Auth().auth3leg()
token.refresh()
token.refresh()
self.assertNotEqual(token.access_token, "")

0 comments on commit e81e88c

Please sign in to comment.