-
Notifications
You must be signed in to change notification settings - Fork 771
feat: oauth sdk implementation #799
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
Merged
Merged
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
1de9d95
chore: oauth sdk implementation
manisha1997 451ff0a
chore: oauth sdk implementation
manisha1997 f36f785
chore: oauth sdk implementation
manisha1997 8cb3d47
chore: oauth sdk implementation
manisha1997 691071d
chore: oauth sdk implementation
manisha1997 6204eae
chore: oauth sdk implementation
manisha1997 26fd590
Merge branch 'main' into oauth-sdk
manisha1997 f429196
chore: oauth sdk implementation
manisha1997 a1521dd
chore: oauth sdk implementation
manisha1997 404b81b
feat: bearer token client
manisha1997 67c8697
chore: oauth sdk implementation
manisha1997 2f329c7
chore: oauth sdk implementation
manisha1997 8f026a0
chore: oauth sdk implementation
manisha1997 81ec687
chore: oauth sdk implementation
manisha1997 db266db
chore: oauth sdk implementation
manisha1997 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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,24 @@ | ||
from twilio.http.token_manager_initializer import TokenManagerInitializer | ||
|
||
# Dynamic import utility function | ||
def dynamic_import(module_name, class_name): | ||
from importlib import import_module | ||
module = import_module(module_name) | ||
return getattr(module, class_name) | ||
|
||
class OauthTokenBase: | ||
def get_oauth_token(self, domain: str, version: str, username: str, password: str): | ||
Domain = dynamic_import("twilio.base.domain", "Domain") | ||
Version = dynamic_import("twilio.base.version", "Version") | ||
BearerTokenHTTPClient = dynamic_import("twilio.http.bearer_token_http_client", "BearerTokenHTTPClient") | ||
OrgTokenManager = dynamic_import("twilio.http.orgs_token_manager", "OrgTokenManager") | ||
Client = dynamic_import("twilio.rest", "Client") | ||
try: | ||
orgs_token_manager = TokenManagerInitializer.get_token_manager() | ||
return BearerTokenHTTPClient(orgs_token_manager).get_access_token(Version(Domain(Client(username, password), domain), version)) | ||
except Exception: | ||
orgs_token_manager = OrgTokenManager(grant_type='client_credentials', | ||
client_id=username, | ||
client_secret=password) | ||
TokenManagerInitializer().set_token_manager(orgs_token_manager) | ||
return BearerTokenHTTPClient(orgs_token_manager).get_access_token(Version(Domain(Client(username, password), domain), version)) |
This file contains hidden or 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 hidden or 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,30 @@ | ||
import datetime | ||
import jwt | ||
|
||
from twilio.base.version import Version | ||
from twilio.http.token_manager import TokenManager | ||
from twilio.twilio_bearer_token_auth import TwilioBearerTokenAuth | ||
|
||
|
||
class BearerTokenHTTPClient: | ||
def __init__(self, orgs_token_manager: TokenManager): | ||
self.orgs_token_manager = orgs_token_manager | ||
|
||
def get_access_token(self, version: Version): | ||
if TwilioBearerTokenAuth.get_access_token() is None or self.is_token_expired( | ||
TwilioBearerTokenAuth.get_access_token() | ||
): | ||
access_token = self.orgs_token_manager.fetch_access_token(version) | ||
TwilioBearerTokenAuth.init(access_token) | ||
else: | ||
access_token = TwilioBearerTokenAuth.get_access_token() | ||
|
||
return access_token | ||
|
||
def is_token_expired(self, token): | ||
decoded_jwt = jwt.decode(token, options={"verify_signature": True}) | ||
expires_at = decoded_jwt.get("exp") | ||
# Add a buffer of 30 seconds | ||
buffer_seconds = 30 | ||
buffer_expires_at = expires_at - buffer_seconds | ||
return buffer_expires_at < datetime.datetime.now().timestamp() |
This file contains hidden or 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 hidden or 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,4 @@ | ||
class NoAuthHTTPClient: | ||
def get_headers(self): | ||
headers = {} | ||
return headers |
This file contains hidden or 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,42 @@ | ||
from twilio.base.version import Version | ||
from twilio.http.token_manager import TokenManager | ||
from twilio.rest.preview_iam.organizations.token import TokenList | ||
|
||
|
||
class OrgTokenManager(TokenManager): | ||
""" | ||
Orgs Token Manager | ||
""" | ||
|
||
def __init__( | ||
self, | ||
grant_type: str, | ||
client_id: str, | ||
client_secret: str, | ||
code: str = None, | ||
redirect_uri: str = None, | ||
audience: str = None, | ||
refreshToken: str = None, | ||
scope: str = None, | ||
): | ||
self.grant_type = grant_type | ||
self.client_id = client_id | ||
self.client_secret = client_secret | ||
self.code = code | ||
self.redirect_uri = redirect_uri | ||
self.audience = audience | ||
self.refreshToken = refreshToken | ||
self.scope = scope | ||
|
||
def fetch_access_token(self, version: Version): | ||
token_list = TokenList(version) | ||
token_instance = token_list.create( | ||
grant_type=self.grant_type, | ||
client_id=self.client_id, | ||
client_secret=self.client_secret, | ||
code=self.code, | ||
redirect_uri=self.redirect_uri, | ||
audience=self.audience, | ||
scope=self.scope, | ||
) | ||
return token_instance.access_token |
This file contains hidden or 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,7 @@ | ||
from twilio.base.version import Version | ||
|
||
|
||
class TokenManager: | ||
|
||
def fetch_access_token(self, version: Version): | ||
pass |
This file contains hidden or 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,16 @@ | ||
from twilio.http.token_manager import TokenManager | ||
|
||
|
||
class TokenManagerInitializer: | ||
|
||
org_token_manager = None | ||
|
||
@classmethod | ||
def set_token_manager(cls, token_manager: TokenManager): | ||
cls.org_token_manager = token_manager | ||
|
||
@classmethod | ||
def get_token_manager(cls): | ||
if cls.org_token_manager is None: | ||
raise Exception('Token Manager not initialized') | ||
return cls.org_token_manager |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.