Skip to content

Commit 572bc3b

Browse files
committed
added revocation endpoints
1 parent c5eba40 commit 572bc3b

File tree

2 files changed

+18
-1
lines changed

2 files changed

+18
-1
lines changed

discordoauth2/__init__.py

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@ class PartialAccessToken():
55
def __init__(self, access_token, client) -> None:
66
self.client = client
77
self.token = access_token
8+
9+
def revoke(self):
10+
return self.client.revoke_token(self.token, token_type="access_token")
811

912
def fetch_identify(self):
1013
response = requests.get("https://discord.com/api/v10/users/@me", headers={
@@ -107,6 +110,9 @@ def __init__(self, data, client) -> None:
107110
self.refresh_token = data.get("refresh_token")
108111
self.webhook = data.get("webhook")
109112
self.guild = data.get("guild")
113+
114+
def revoke_refresh_token(self):
115+
return self.client.revoke_token(self.refresh_token, token_type="refresh_token")
110116

111117
class Client():
112118
def __init__(self, id, secret, redirect, bot_token=None):
@@ -151,13 +157,24 @@ def client_credentails_grant(self, scope):
151157
response = requests.post("https://discord.com/api/v10/oauth2/token", data={
152158
"grant_type": "client_credentials", "scope": " ".join(scope)},
153159
auth=(self.id, self.__secret))
154-
155160
if response.ok:
156161
return AccessToken(response.json(), self)
157162
elif response.status_code == 400: raise exceptions.HTTPException("the scope, client id or client secret is invalid/don't match.")
158163
elif response.status_code == 429: raise exceptions.RateLimited(f"You are being Rate Limited. Retry after: {response.json()['retry_after']}", retry_after=response.json()['retry_after'])
159164
else:
160165
raise exceptions.HTTPException(f"Unexpected HTTP {response.status_code}")
166+
167+
def revoke_token(self, token, token_type=None):
168+
response = requests.post("https://discord.com/api/oauth2/token/revoke",
169+
data={"token": token, "token_type_hint": token_type},
170+
auth=(self.id, self.__secret))
171+
print(response.status_code, response.text)
172+
if response.ok:
173+
return
174+
elif response.status_code == 401: raise exceptions.Forbidden(f"this AccessToken does not have the nessasary scope.")
175+
elif response.status_code == 429: raise exceptions.RateLimited(f"You are being Rate Limited. Retry after: {response.json()['retry_after']}", retry_after=response.json()['retry_after'])
176+
else:
177+
raise exceptions.HTTPException(f"Unexpected HTTP {response.status_code}")
161178

162179
class exceptions():
163180
class BaseException(Exception):
817 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)