From 0d074b1cd5c7645cee213e642dc8b3578c23dcf0 Mon Sep 17 00:00:00 2001 From: Ryan Chard Date: Fri, 1 Sep 2023 10:04:47 +1200 Subject: [PATCH] Add an authorizer login manager --- .../sdk/login_manager/__init__.py | 2 + .../login_manager/authorizer_login_manager.py | 45 +++++++++++++++++++ 2 files changed, 47 insertions(+) create mode 100644 compute_sdk/globus_compute_sdk/sdk/login_manager/authorizer_login_manager.py diff --git a/compute_sdk/globus_compute_sdk/sdk/login_manager/__init__.py b/compute_sdk/globus_compute_sdk/sdk/login_manager/__init__.py index 4381cd2c1..292ae66d9 100644 --- a/compute_sdk/globus_compute_sdk/sdk/login_manager/__init__.py +++ b/compute_sdk/globus_compute_sdk/sdk/login_manager/__init__.py @@ -1,5 +1,6 @@ from .decorators import requires_login from .manager import ComputeScopes, LoginManager +from .authorizer_login_manager import AuthorizerLoginManager from .protocol import LoginManagerProtocol __all__ = ( @@ -7,4 +8,5 @@ "ComputeScopes", "LoginManagerProtocol", "requires_login", + "AuthorizerLoginManager", ) diff --git a/compute_sdk/globus_compute_sdk/sdk/login_manager/authorizer_login_manager.py b/compute_sdk/globus_compute_sdk/sdk/login_manager/authorizer_login_manager.py new file mode 100644 index 000000000..7029e37e5 --- /dev/null +++ b/compute_sdk/globus_compute_sdk/sdk/login_manager/authorizer_login_manager.py @@ -0,0 +1,45 @@ +from __future__ import annotations + +import logging + +import globus_sdk +from globus_sdk.scopes import AuthScopes + +from globus_compute_sdk.sdk.login_manager.protocol import LoginManagerProtocol +from globus_compute_sdk.sdk.web_client import WebClient + +from .manager import ComputeScopeBuilder + +log = logging.getLogger(__name__) + +ComputeScopes = ComputeScopeBuilder() + + +class AuthorizerLoginManager(LoginManagerProtocol): + """ + Implements a LoginManager that can be instantiated with authorizers. + This manager can be used to create an Executor with authorizers created + from previously acquired tokens, rather than requiring a Native App login + flow or Client credentials. + """ + + def __init__(self, authorizers: dict[str, globus_sdk.RefreshTokenAuthorizer]): + self.authorizers = authorizers + + def get_auth_client(self) -> globus_sdk.AuthClient: + return globus_sdk.AuthClient(authorizer=self.authorizers[AuthScopes.openid]) + + def get_web_client( + self, *, base_url: str | None = None, app_name: str | None = None + ) -> WebClient: + return WebClient( + base_url=base_url, + app_name=app_name, + authorizer=self.authorizers[ComputeScopes.resource_server], + ) + + def ensure_logged_in(self): + return True + + def logout(self): + log.warning("Logout cannot be invoked from a TokenLoginManager.")