Skip to content

Commit

Permalink
New backend for the WLCG IAM testing site (#820)
Browse files Browse the repository at this point in the history
* New backend for the WLCG IAM testing site

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* Update wlcg.py

Adding email scope in default scope

* Adding test for wlcg backend

Co-authored-by: Maiken Pedersen <[email protected]>
  • Loading branch information
maikenp and Maiken Pedersen authored Sep 19, 2023
1 parent 26d89db commit ca841a0
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 0 deletions.
38 changes: 38 additions & 0 deletions social_core/backends/wlcg.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
from urllib.parse import urlencode

from .oauth import BaseOAuth2


class WLCGOAuth2(BaseOAuth2):
"""
WLCG IAM Authentication Backend
"""

name = "wlcg"
API_URL = "https://wlcg.cloud.cnaf.infn.it"
AUTHORIZATION_URL = "https://wlcg.cloud.cnaf.infn.it/authorize"
ACCESS_TOKEN_URL = "https://wlcg.cloud.cnaf.infn.it/token"
REFRESH_TOKEN_URL = "https://wlcg.cloud.cnaf.infn.it/token"
ACCESS_TOKEN_METHOD = "POST"
DEFAULT_SCOPE = ["openid", "email", "profile", "wlcg", "offline_access"]
REDIRECT_STATE = False

def get_user_details(self, response):
"""Return user details from WLCG IAM service"""
fullname, first_name, last_name = self.get_user_names(
first_name=response.get("given_name"), last_name=response.get("family_name")
)
return {
"username": response.get("email"),
"email": response.get("email"),
"fullname": fullname,
"first_name": first_name,
"last_name": last_name,
}

def user_data(self, access_token, *args, **kwargs):
"""Loads user data from service"""
url = "https://wlcg.cloud.cnaf.infn.it/userinfo?" + urlencode(
{"access_token": access_token}
)
return self.get_json(url)
30 changes: 30 additions & 0 deletions social_core/tests/backends/test_wlcg.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import json

from .oauth import OAuth2Test


class WLCGOAuth2Test(OAuth2Test):
backend_path = "social_core.backends.wlcg.WLCGOAuth2"
user_data_url = "https://wlcg.cloud.cnaf.infn.it/userinfo"
expected_username = "[email protected]"
access_token_body = json.dumps(
{
"access_token": "foobar",
"token_type": "bearer",
}
)
user_data_body = json.dumps(
{
"email": "[email protected]",
"family_name": "Bar",
"given_name": "Foo",
"name": "Foo Bar",
"email_verified": True,
}
)

def test_login(self):
self.do_login()

def test_partial_pipeline(self):
self.do_partial_pipeline()

0 comments on commit ca841a0

Please sign in to comment.