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

ADD: b2access loginpass #33

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
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
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ Connections that Authlib Loginpass contains:
- [x] Yandex
- [x] Twitch
- [x] VK
- [x] B2Access

Usage
-----
Expand Down
4 changes: 3 additions & 1 deletion loginpass/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,13 @@
from .twitch import Twitch
from .vk import VK
from .orcid import ORCiD
from .b2access import B2Access, B2AccessDev, create_b2access_backend


OAUTH_BACKENDS = [
BattleNet, Twitter, Facebook, Google, GitHub, Dropbox, Instagram, Reddit,
Gitlab, Slack, Discord, StackOverflow, Bitbucket, Strava, Spotify, Yandex,
Twitch, VK,
Twitch, VK, B2Access
]

__all__ = [
Expand Down Expand Up @@ -56,6 +57,7 @@
'VK',
'ORCiD',
'OAUTH_BACKENDS',
'B2Access','B2AccessDev','create_b2access_backend',
]

__version__ = version
Expand Down
57 changes: 57 additions & 0 deletions loginpass/b2access.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
"""
loginpass.b2access
~~~~~~~~~~~~~~~~
Loginpass Backend of B2ACCESS

gets endpoint from B2ACCESS_API_URL environment variable, if not specified, default development url is used

development: https://unity.eudat-aai.fz-juelich.de/
production: https://b2access.eudat.eu).

:copyright: (c) 2019 by Tomas Kulhanek, ESDF
:license: MIT, see LICENSE for more details.

"""

from loginpass._core import UserInfo, OAuthBackend
import os

#Authorization Grant https://unity.eudat-aai.fz-juelich.de:443/oauth2-as/oauth2-authz https://b2access.eudat.eu:443/oauth2-as/oauth2-authz
#Access Token https://unity.eudat-aai.fz-juelich.de:443/oauth2/token https://b2access.eudat.eu:443/oauth2/token
#Token Information/validation https://unity.eudat-aai.fz-juelich.de:443/oauth2 /tokeninfo https://b2access.eudat.eu:443/oauth2/tokeninfo
#User information https://unity.eudat-aai.fz-juelich.de:443/oauth2/userinfo https://b2access.eudat.eu:443/oauth2/userinfo

# development endpoint

def create_b2access_backend(name,b2accessurl):
B2ACCESS_API_URL = b2accessurl.strip('/')
B2ACCESS_TOKEN_URL = '{b2accessurl}/oauth2/token'.format(b2accessurl=b2accessurl)
B2ACCESS_TOKENINFO_URL = '{b2accessurl}/oauth2/tokeninfo'.format(b2accessurl=b2accessurl)
B2ACCESS_AUTH_URL = '{b2accessurl}/oauth2-as/oauth2-authz'.format(b2accessurl=b2accessurl)
B2ACCESS_USERINFO_SUFFIX='/oauth2/userinfo'
B2ACCESS_USERINFO_URL = '{b2accessurl}/oauth2/userinfo'.format(b2accessurl=b2accessurl)

class B2AccessAuth(OAuthBackend):
OAUTH_TYPE = '2.0,oidc'
OAUTH_NAME = 'b2access'
OAUTH_CONFIG = {
'api_base_url': B2ACCESS_API_URL,
'access_token_url': B2ACCESS_TOKEN_URL,
'authorize_url': B2ACCESS_AUTH_URL,
'client_kwargs': {'scope': 'email profile'},
}

def profile(self, **kwargs):
print('b2access profile kwargs:',kwargs)
resp = self.get(B2ACCESS_USERINFO_SUFFIX,**kwargs)
data = resp.json()
params = {
'sub': data['sub'],
'name': data['name'],
'email': data['email'],
}
return UserInfo(params)
return B2AccessAuth

B2Access = create_b2access_backend('b2access','https://b2access.eudat.eu')
B2AccessDev = create_b2access_backend('b2access','https://unity.eudat-aai.fz-juelich.de')
5 changes: 5 additions & 0 deletions tests/data/b2access/01_response.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"sub": "37198652-1231-e2e3435-1f231242350",
"name": "John Smith",
"email": "[email protected]"
}
5 changes: 5 additions & 0 deletions tests/data/b2access/result.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"sub": "37198652-1231-e2e3435-1f231242350",
"name": "John Smith",
"email": "[email protected]"
}
4 changes: 4 additions & 0 deletions tests/test_oauth_backends.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
Gitlab,
Strava,
ORCiD,
B2Access
)


Expand Down Expand Up @@ -97,3 +98,6 @@ def test_strava(self):

def test_orcid(self):
self.run_oauth_profile(ORCiD)

def test_b2access(self):
self.run_oauth_profile(B2Access)