Skip to content

Commit fae7381

Browse files
authored
Merge pull request #45 from XeroAPI/token-revoke-method
WIP: adds token revoke method, ready for test pypi
2 parents 1c53f7d + c7809c5 commit fae7381

File tree

6 files changed

+16144
-94267
lines changed

6 files changed

+16144
-94267
lines changed

docs/v1/accounting/index.html

Lines changed: 16077 additions & 94264 deletions
Large diffs are not rendered by default.

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,5 +48,5 @@ def read_file(filename):
4848
keywords="xero python sdk API oAuth",
4949
name="xero_python",
5050
packages=find_packages(include=["xero_python", "xero_python.*"]),
51-
version="1.5.2",
51+
version="1.5.3",
5252
)

xero_python/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@
22

33
__author__ = """Xero Developer API"""
44
__email__ = "[email protected]"
5-
__version__ = "1.5.2"
5+
__version__ = "1.5.3"

xero_python/api_client/__init__.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -748,6 +748,16 @@ def refresh_oauth2_token(self):
748748
if oauth2_token.refresh_access_token(self):
749749
return self.get_oauth2_token()
750750

751+
def revoke_oauth2_token(self):
752+
"""
753+
Force revoke oauth2 token
754+
:return: empty oauth2 token
755+
"""
756+
oauth2_token = self.configuration.oauth2_token
757+
oauth2_token.update_token(**self.get_oauth2_token())
758+
if oauth2_token.revoke_access_token(self):
759+
return self.get_oauth2_token()
760+
751761
def oauth2_token_getter(self, token_getter):
752762
"""
753763
A decorator to register a callback function for getting oauth2 token

xero_python/api_client/oauth2.py

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ class TokenApi:
1111
"""
1212

1313
refresh_token_url = "https://identity.xero.com/connect/token"
14+
revoke_token_url = "https://identity.xero.com/connect/revocation"
1415

1516
def __init__(self, api_client, client_id, client_secret):
1617
self.api_client = api_client
@@ -50,6 +51,35 @@ def refresh_token(self, refresh_token, scope):
5051
# todo validate response is json
5152
return self.parse_token_response(response)
5253

54+
def revoke_token(self, refresh_token):
55+
"""
56+
Call xero identity API to revoke access tokens and remove all a user's connections using refresh token
57+
:param refresh_token: str auth2 refresh token
58+
:return: status response
59+
"""
60+
post_data = {
61+
"token": refresh_token,
62+
"client_id": self.client_id,
63+
"client_secret": self.client_secret,
64+
}
65+
response, status, headers = self.api_client.call_api(
66+
self.revoke_token_url,
67+
"POST",
68+
header_params={
69+
"Accept": "application/json",
70+
"Content-Type": "application/x-www-form-urlencoded",
71+
},
72+
post_params=post_data,
73+
auth_settings=None, # important to prevent infinite recursive loop
74+
_preload_content=False,
75+
)
76+
if status != 200:
77+
# todo improve error handling
78+
raise Exception(
79+
"refresh token status {} {} {!r}".format(status, response, headers)
80+
)
81+
return status
82+
5383
def parse_token_response(self, response):
5484
"""
5585
Parse token data from http response
@@ -171,6 +201,30 @@ def refresh_access_token(self, api_client):
171201
api_client.set_oauth2_token(new_token)
172202
return True
173203

204+
def revoke_access_token(self, api_client):
205+
"""
206+
Perform auth2 revoke token call.
207+
:param api_client: ApiClient instance used to perform refresh token API call.
208+
:return: bool - True if success
209+
:raise: http request related errors
210+
"""
211+
if not self.can_refresh_access_token():
212+
return False
213+
token_api = TokenApi(api_client, self.client_id, self.client_secret)
214+
token_api.revoke_token(self.refresh_token)
215+
new_token = {
216+
"access_token": None,
217+
"refresh_token": None,
218+
"scope": None,
219+
"expires_at": None,
220+
"expires_in": None,
221+
"token_type": "Bearer",
222+
"id_token": None,
223+
}
224+
self.update_token(**new_token)
225+
api_client.set_oauth2_token(new_token)
226+
return True
227+
174228
def update_token(
175229
self,
176230
access_token,

xero_python/docs/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ These endpoints are related to managing authentication tokens and identity for X
44
The `xero_python` package is automatically generated by the [XeroAPI SDK 2.0 Codegen](https://github.com/xero-github/xeroapi-sdk-codegen) project:
55

66
- API version: 2.10.4
7-
- Package version: 1.5.2
7+
- Package version: 1.5.3
88
- Build package: org.openapitools.codegen.languages.PythonClientCodegen
99
For more information, please visit [https://developer.xero.com](https://developer.xero.com)
1010

0 commit comments

Comments
 (0)