-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feature: create authentication class for flows user-api-token
- Loading branch information
Showing
2 changed files
with
29 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
from mozilla_django_oidc.contrib.drf import OIDCAuthentication | ||
from marketplace.connect.client import ConnectProjectClient | ||
|
||
|
||
class FlowsOIDCAuthentication(OIDCAuthentication): # pragma: no cover | ||
def is_flows_token(self, token): | ||
""" | ||
Check if the token is likely a Flows token based on its length. | ||
""" | ||
return len(token) == 40 | ||
|
||
def authenticate(self, request): | ||
access_token = self.get_access_token(request) | ||
if not access_token: | ||
return None | ||
|
||
if self.is_flows_token(access_token): | ||
client = ConnectProjectClient() | ||
response = client.get_user_api_token( | ||
request.data["user_email"], access_token | ||
) | ||
|
||
if response: | ||
return (request.user, access_token) | ||
|
||
return super().authenticate(request) |
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