Skip to content
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

Fix location for userinfo call; deprecate old name #865

Merged
merged 1 commit into from
Oct 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions changelog.d/20231006_154216_sirosen_fix_userinfo.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Deprecated
~~~~~~~~~~

- ``AuthClient.oauth2_userinfo`` method has been deprecated in favor of
``AuthClient.userinfo``. Callers should prefer the new method name. (:pr:`NUMBER`)
29 changes: 4 additions & 25 deletions src/globus_sdk/_testing/data/auth/oauth2_userinfo.py
Original file line number Diff line number Diff line change
@@ -1,26 +1,5 @@
from globus_sdk._testing.models import RegisteredResponse, ResponseSet
# this is a clone of the userinfo.py data for compatibility across testing
# it should be removed in a future release
from .userinfo import RESPONSES

from ._common import FORBIDDEN_AUTH_RESPONSE, UNAUTHORIZED_AUTH_RESPONSE

RESPONSES = ResponseSet(
unauthorized=RegisteredResponse(
service="auth",
path="/v2/oauth2/userinfo",
status=401,
json=UNAUTHORIZED_AUTH_RESPONSE.json,
metadata={
"error_id": UNAUTHORIZED_AUTH_RESPONSE.error_id,
**UNAUTHORIZED_AUTH_RESPONSE.metadata_include,
},
),
forbidden=RegisteredResponse(
service="auth",
path="/v2/oauth2/userinfo",
status=403,
json=FORBIDDEN_AUTH_RESPONSE.json,
metadata={
"error_id": FORBIDDEN_AUTH_RESPONSE.error_id,
**FORBIDDEN_AUTH_RESPONSE.metadata_include,
},
),
)
__all__ = ("RESPONSES",)
26 changes: 26 additions & 0 deletions src/globus_sdk/_testing/data/auth/userinfo.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
from globus_sdk._testing.models import RegisteredResponse, ResponseSet

from ._common import FORBIDDEN_AUTH_RESPONSE, UNAUTHORIZED_AUTH_RESPONSE

RESPONSES = ResponseSet(
unauthorized=RegisteredResponse(
service="auth",
path="/v2/oauth2/userinfo",
status=401,
json=UNAUTHORIZED_AUTH_RESPONSE.json,
metadata={
"error_id": UNAUTHORIZED_AUTH_RESPONSE.error_id,
**UNAUTHORIZED_AUTH_RESPONSE.metadata_include,
},
),
forbidden=RegisteredResponse(
service="auth",
path="/v2/oauth2/userinfo",
status=403,
json=FORBIDDEN_AUTH_RESPONSE.json,
metadata={
"error_id": FORBIDDEN_AUTH_RESPONSE.error_id,
**FORBIDDEN_AUTH_RESPONSE.metadata_include,
},
),
)
34 changes: 0 additions & 34 deletions src/globus_sdk/services/auth/client/base_login_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -449,37 +449,3 @@ def oauth2_token(
encoding="form",
)
)

def oauth2_userinfo(self) -> GlobusHTTPResponse:
"""
Call the Userinfo endpoint of Globus Auth.
Userinfo is specified as part of the OpenID Connect (OIDC) standard,
and Globus Auth's Userinfo is OIDC-compliant.

The exact data returned will depend upon the set of OIDC-related scopes
which were used to acquire the token being used for this call. For
details, see the **API Info** below.

.. tab-set::

.. tab-item:: Example Usage

.. code-block:: python

ac = AuthClient(...)
info = ac.oauth2_userinfo()
print(
'Effective Identity "{info["sub"]}" has '
f'Full Name "{info["name"]}" and '
f'Email "{info["email"]}"'
)

.. tab-item:: API Info

``GET /v2/oauth2/userinfo``

.. extdoclink:: Get Userinfo
:ref: auth/reference/#get_or_post_v2_oauth2_userinfo_resource
"""
log.info("Looking up OIDC-style Userinfo from Globus Auth")
return self.get("/v2/oauth2/userinfo")
43 changes: 43 additions & 0 deletions src/globus_sdk/services/auth/client/service_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,49 @@ def get_jwk(
)
return pem_decode_jwk_data(jwk_data=jwk_data) if as_pem else jwk_data

def userinfo(self) -> GlobusHTTPResponse:
"""
Call the Userinfo endpoint of Globus Auth.
Userinfo is specified as part of the OpenID Connect (OIDC) standard,
and Globus Auth's Userinfo is OIDC-compliant.

The exact data returned will depend upon the set of OIDC-related scopes
which were used to acquire the token being used for this call. For
details, see the **API Info** below.

.. tab-set::

.. tab-item:: Example Usage

.. code-block:: python

ac = AuthClient(...)
info = ac.oauth2_userinfo()
print(
'Effective Identity "{info["sub"]}" has '
f'Full Name "{info["name"]}" and '
f'Email "{info["email"]}"'
)

.. tab-item:: API Info

``GET /v2/oauth2/userinfo``

.. extdoclink:: Get Userinfo
:ref: auth/reference/#get_or_post_v2_oauth2_userinfo_resource
"""
log.info("Looking up OIDC-style Userinfo from Globus Auth")
return self.get("/v2/oauth2/userinfo")

def oauth2_userinfo(self) -> GlobusHTTPResponse:
"""
A deprecated alias for ``userinfo``.
"""
exc.warn_deprecated(
"The method `oauth2_userinfo` is deprecated. Use `userinfo` instead."
)
return self.userinfo()

def get_identities(
self,
*,
Expand Down
23 changes: 0 additions & 23 deletions tests/functional/services/auth/base/test_oauth2_userinfo.py

This file was deleted.

36 changes: 36 additions & 0 deletions tests/functional/services/auth/service_client/test_userinfo.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import pytest

import globus_sdk
from globus_sdk._testing import load_response


# TODO: add data for the success case and test it
@pytest.mark.xfail
def test_userinfo():
raise NotImplementedError


@pytest.mark.parametrize("casename", ("unauthorized", "forbidden"))
def test_userinfo_error_handling(service_client, casename):
meta = load_response(service_client.oauth2_userinfo, case=casename).metadata

with pytest.raises(globus_sdk.AuthAPIError) as excinfo:
service_client.userinfo()

err = excinfo.value
assert err.http_status == meta["http_status"]
assert err.code == meta["code"]
assert err.request_id == meta["error_id"]


def test_oauth2_userinfo_warns(service_client):
# TODO:
# if the above success case is added, this test can be changed to use it
# that would let us get rid of the try-except guard below
load_response(service_client.oauth2_userinfo, case="unauthorized")

with pytest.warns(globus_sdk.RemovedInV4Warning, match="Use `userinfo` instead."):
try:
service_client.oauth2_userinfo()
except globus_sdk.AuthAPIError:
pass