-
-
Notifications
You must be signed in to change notification settings - Fork 159
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add the start of OAuth functionality..
- Loading branch information
Showing
10 changed files
with
283 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
""" | ||
MIT License | ||
Copyright (c) 2017 - Present PythonistaGuild | ||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. | ||
""" | ||
|
||
from .oauth import OAuth as OAuth | ||
from .payloads import * |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
""" | ||
MIT License | ||
Copyright (c) 2017 - Present PythonistaGuild | ||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. | ||
""" | ||
from __future__ import annotations | ||
|
||
from typing import TYPE_CHECKING | ||
|
||
from ..http import HTTPClient | ||
from .payloads import * | ||
|
||
|
||
if TYPE_CHECKING: | ||
from ..types_.responses import RefreshTokenResponse, ValidateTokenResponse | ||
|
||
|
||
class OAuth(HTTPClient): | ||
def __init__(self, *, client_id: str, client_secret: str) -> None: | ||
super().__init__() | ||
|
||
self.client_id = client_id | ||
self.client_secret = client_secret | ||
|
||
async def validate_token(self, token: str, /) -> ValidateTokenPayload: | ||
token = token.removeprefix("Bearer ").removeprefix("OAuth ") | ||
|
||
headers: dict[str, str] = {"Authorization": f"OAuth {token}"} | ||
data: ValidateTokenResponse = await self.request_json("GET", "/oauth2/validate", use_id=True, headers=headers) | ||
|
||
return ValidateTokenPayload(data) | ||
|
||
async def refresh_token(self, refresh_token: str, /) -> RefreshTokenPayload: | ||
headers: dict[str, str] = {"Content-Type": "application/x-www-form-urlencoded"} | ||
|
||
params: dict[str, str] = { | ||
"grant_type": "refresh_token", | ||
"refresh_token": refresh_token, | ||
"client_id": self.client_id, | ||
"client_secret": self.client_secret, | ||
} | ||
|
||
data: RefreshTokenResponse = await self.request_json( | ||
"POST", "/oauth2/token", use_id=True, headers=headers, params=params | ||
) | ||
|
||
return RefreshTokenPayload(data) | ||
|
||
async def revoke_token(self, token: str, /) -> ...: | ||
raise NotImplementedError |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
""" | ||
MIT License | ||
Copyright (c) 2017 - Present PythonistaGuild | ||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. | ||
""" | ||
from __future__ import annotations | ||
|
||
from collections.abc import Iterator, Mapping | ||
from typing import TYPE_CHECKING, Any | ||
|
||
|
||
if TYPE_CHECKING: | ||
from ..types_.responses import * | ||
|
||
|
||
__all__ = ( | ||
"RefreshTokenPayload", | ||
"ValidateTokenPayload", | ||
) | ||
|
||
|
||
class BasePayload(Mapping[str, Any]): | ||
__slots__ = ("raw_data",) | ||
|
||
def __init__(self, raw: OAuthResponses, /) -> None: | ||
self.raw_data = raw | ||
|
||
def __getitem__(self, key: str) -> Any: | ||
return self.raw_data[key] # type: ignore | ||
|
||
def __iter__(self) -> Iterator[str]: | ||
return iter(self.raw_data) | ||
|
||
def __len__(self) -> int: | ||
return len(self.raw_data) | ||
|
||
|
||
class RefreshTokenPayload(BasePayload): | ||
__slots__ = ("access_token", "refresh_token", "expires_in", "scope", "token_type") | ||
|
||
def __init__(self, raw: RefreshTokenResponse, /) -> None: | ||
super().__init__(raw) | ||
|
||
self.access_token: str = raw["access_token"] | ||
self.refresh_token: str = raw["refresh_token"] | ||
self.expires_in: int = raw["expires_in"] | ||
self.scope: str | list[str] = raw["scope"] | ||
self.token_type: str = raw["token_type"] | ||
|
||
|
||
class ValidateTokenPayload(BasePayload): | ||
__slots__ = ("client_id", "login", "scopes", "user_id", "expires_in") | ||
|
||
def __init__(self, raw: ValidateTokenResponse, /) -> None: | ||
super().__init__(raw) | ||
|
||
self.client_id: str = raw["client_id"] | ||
self.login: str = raw["login"] | ||
self.scopes: list[str] = raw["scopes"] | ||
self.user_id: str = raw["user_id"] | ||
self.expires_in: int = raw["expires_in"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
""" | ||
MIT License | ||
Copyright (c) 2017 - Present PythonistaGuild | ||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. | ||
""" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
""" | ||
MIT License | ||
Copyright (c) 2017 - Present PythonistaGuild | ||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. | ||
""" | ||
from typing import TypeAlias, TypedDict | ||
|
||
|
||
__all__ = ( | ||
"RefreshTokenResponse", | ||
"ValidateTokenResponse", | ||
"OAuthResponses", | ||
) | ||
|
||
|
||
class RefreshTokenResponse(TypedDict): | ||
access_token: str | ||
refresh_token: str | ||
expires_in: int | ||
scope: str | list[str] | ||
token_type: str | ||
|
||
|
||
class ValidateTokenResponse(TypedDict): | ||
client_id: str | ||
login: str | ||
scopes: list[str] | ||
user_id: str | ||
expires_in: int | ||
|
||
|
||
OAuthResponses: TypeAlias = RefreshTokenResponse | ValidateTokenResponse |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters