| 1 |
from __future__ import annotations |
| 2 |
|
| 3 |
import logging |
| 4 |
import typing as t |
| 5 |
import urllib.parse |
| 6 |
|
| 7 |
from globus_sdk._internal.utils import slash_join |
| 8 |
from globus_sdk._missing import filter_missing |
| 9 |
from globus_sdk.scopes import Scope, ScopeParser |
| 10 |
|
| 11 |
from ..response import OAuthAuthorizationCodeResponse |
| 12 |
from .base import GlobusOAuthFlowManager |
| 13 |
|
| 14 |
if t.TYPE_CHECKING: |
| 15 |
import globus_sdk |
| 16 |
|
| 17 |
log = logging.getLogger(__name__) |
| 18 |
|
| 19 |
|
| 20 |
class GlobusAuthorizationCodeFlowManager(GlobusOAuthFlowManager): |
| 21 |
""" |
| 22 |
This is the OAuth flow designated for use by Clients wishing to |
| 23 |
authenticate users in a web application backed by a server-side component |
| 24 |
(e.g. an API). The key constraint is that there is a server-side system |
| 25 |
that can keep a Client Secret without exposing it to the web client. |
| 26 |
For example, a Django application can rely on the webserver to own the |
| 27 |
secret, so long as it doesn't embed it in any of the pages it generates. |
| 28 |
|
| 29 |
The application sends the user to get a temporary credential (an |
| 30 |
``auth_code``) associated with its Client ID. It then exchanges that |
| 31 |
temporary credential for a token, protecting the exchange with its Client |
| 32 |
Secret (to prove that it really is the application that the user just |
| 33 |
authorized). |
| 34 |
|
| 35 |
:param auth_client: The client used to extract default values for the flow, |
| 36 |
and also to make calls to the Auth service. |
| 37 |
:param redirect_uri: The page that users should be directed to after authenticating |
| 38 |
at the authorize URL. |
| 39 |
:param requested_scopes: The scopes on the token(s) being requested. |
| 40 |
:param state: This string allows an application to pass information back to itself |
| 41 |
in the course of the OAuth flow. Because the user will navigate away from the |
| 42 |
application to complete the flow, this parameter lets the app pass an arbitrary |
| 43 |
string from the starting page to the ``redirect_uri`` |
| 44 |
:param refresh_tokens: When True, request refresh tokens in addition to access |
| 45 |
tokens. [Default: ``False``] |
| 46 |
""" |
| 47 |
|
| 48 |
def __init__( |
| 49 |
self, |
| 50 |
auth_client: globus_sdk.ConfidentialAppAuthClient, |
| 51 |
redirect_uri: str, |
| 52 |
requested_scopes: str | Scope | t.Iterable[str | Scope], |
| 53 |
state: str = "_default", |
| 54 |
refresh_tokens: bool = False, |
| 55 |
) -> None: |
| 56 |
# convert a scope object or iterable to string immediately on load |
| 57 |
# and default to the default requested scopes |
| 58 |
self.requested_scopes: str = ScopeParser.serialize(requested_scopes) |
| 59 |
|
| 60 |
# store the remaining parameters directly, with no transformation |
| 61 |
self.client_id = auth_client.client_id |
| 62 |
self.auth_client = auth_client |
| 63 |
self.redirect_uri = redirect_uri |
| 64 |
self.refresh_tokens = refresh_tokens |
| 65 |
self.state = state |
| 66 |
|
| 67 |
log.debug( |
| 68 |
"Starting Authorization Code Flow with params: " |
| 69 |
f"auth_client.client_id={auth_client.client_id} , " |
| 70 |
f"redirect_uri={redirect_uri} , " |
| 71 |
f"refresh_tokens={refresh_tokens} , " |
| 72 |
f"state={state} , " |
| 73 |
f"requested_scopes={self.requested_scopes}" |
| 74 |
) |
| 75 |
|
| 76 |
def get_authorize_url(self, query_params: dict[str, t.Any] | None = None) -> str: |
| 77 |
""" |
| 78 |
Start a Authorization Code flow by getting the authorization URL to |
| 79 |
which users should be sent. |
| 80 |
|
| 81 |
:param query_params: Additional parameters to include in the authorize URL. |
| 82 |
Primarily for internal use |
| 83 |
|
| 84 |
The returned URL string is encoded to be suitable to display to users |
| 85 |
in a link or to copy into their browser. Users will be redirected |
| 86 |
either to your provided ``redirect_uri`` or to the default location, |
| 87 |
with the ``auth_code`` embedded in a query parameter. |
| 88 |
""" |
| 89 |
authorize_base_url = slash_join( |
| 90 |
self.auth_client.base_url, "/v2/oauth2/authorize" |
| 91 |
) |
| 92 |
log.debug(f"Building authorization URI. Base URL: {authorize_base_url}") |
| 93 |
log.debug(f"query_params={query_params}") |
| 94 |
|
| 95 |
params = { |
| 96 |
"client_id": self.client_id, |
| 97 |
"redirect_uri": self.redirect_uri, |
| 98 |
"scope": self.requested_scopes, |
| 99 |
"state": self.state, |
| 100 |
"response_type": "code", |
| 101 |
"access_type": (self.refresh_tokens and "offline") or "online", |
| 102 |
**(query_params or {}), |
| 103 |
} |
| 104 |
params = filter_missing(params) |
| 105 |
encoded_params = urllib.parse.urlencode(params) |
| 106 |
return f"{authorize_base_url}?{encoded_params}" |
| 107 |
|
| 108 |
def exchange_code_for_tokens( |
| 109 |
self, auth_code: str |
| 110 |
) -> OAuthAuthorizationCodeResponse: |
| 111 |
""" |
| 112 |
The second step of the Authorization Code flow, exchange an |
| 113 |
authorization code for access tokens (and refresh tokens if specified) |
| 114 |
|
| 115 |
:param auth_code: The short-lived code to exchange for tokens |
| 116 |
""" |
| 117 |
log.debug( |
| 118 |
"Performing Authorization Code auth_code exchange. " |
| 119 |
"Sending client_id and client_secret" |
| 120 |
) |
| 121 |
return self.auth_client.oauth2_token( |
| 122 |
{ |
| 123 |
"grant_type": "authorization_code", |
| 124 |
"code": auth_code.encode("utf-8"), |
| 125 |
"redirect_uri": self.redirect_uri, |
| 126 |
}, |
| 127 |
response_class=OAuthAuthorizationCodeResponse, |
| 128 |
) |