Skip to content

Commit

Permalink
add hydra support (#48)
Browse files Browse the repository at this point in the history
  • Loading branch information
matthdsm authored and lepture committed Oct 21, 2019
1 parent 62ab79c commit b6f49f6
Show file tree
Hide file tree
Showing 6 changed files with 96 additions and 1 deletion.
1 change: 1 addition & 0 deletions README.md
Expand Up @@ -75,6 +75,7 @@ Connections that Authlib Loginpass contains:
- [x] Yandex
- [x] Twitch
- [x] VK
- [x] [Ory Hydra](https://www.ory.sh/docs/hydra/)

Usage
-----
Expand Down
4 changes: 3 additions & 1 deletion loginpass/__init__.py
Expand Up @@ -23,12 +23,13 @@
from .twitch import Twitch
from .vk import VK
from .orcid import ORCiD
from .hydra import create_hydra_backend


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

__all__ = [
Expand All @@ -55,6 +56,7 @@
'Twitch',
'VK',
'ORCiD',
'create_hydra_backend',
'OAUTH_BACKENDS',
]

Expand Down
45 changes: 45 additions & 0 deletions loginpass/hydra.py
@@ -0,0 +1,45 @@
"""
loginpass.hydra
~~~~~~~~~~~~~~~~
Loginpass Backend of Ory Hydra (https://ory.sh) and its
enterprise endpoints.
Useful Links: https://www.ory.sh/docs/hydra/sdk/api
- API documentation: https://www.ory.sh/docs/hydra/sdk/api
:license: BSD, see LICENSE for more details.
"""

from ._core import UserInfo, OAuthBackend


def create_hydra_backend(name, hostname):
"""Build Hydra OAuth Backend."""
api_base_url = 'https://{hostname}'.format(hostname=hostname)
authorize_url = 'https://{hostname}/oauth2/auth'.format(hostname=hostname)
token_url = 'https://{hostname}/oauth2/token'.format(hostname=hostname)

class Hydra(OAuthBackend):
OAUTH_TYPE = '2.0,oidc'
OAUTH_NAME = name
OAUTH_CONFIG = {
'api_base_url': api_base_url,
'access_token_url': token_url,
'authorize_url': authorize_url
# customisations can added in client kwargs
}

def profile(self, **kwargs):
resp = self.get('userinfo', **kwargs)
resp.raise_for_status()
data = resp.json()
if not kwargs.get('param_keys'):
params = data
else:
params = {k: data[k] for k in kwargs.get('param_keys')}

return UserInfo(params)

return Hydra
21 changes: 21 additions & 0 deletions tests/data/hydra_response.json
@@ -0,0 +1,21 @@
{
"birthdate": "string",
"email": "string",
"email_verified": true,
"family_name": "string",
"gender": "string",
"given_name": "string",
"locale": "string",
"middle_name": "string",
"name": "string",
"nickname": "string",
"phone_number": "string",
"phone_number_verified": true,
"picture": "string",
"preferred_username": "string",
"profile": "string",
"sub": "string",
"updated_at": 0,
"website": "string",
"zoneinfo": "string"
}
21 changes: 21 additions & 0 deletions tests/data/hydra_result.json
@@ -0,0 +1,21 @@
{
"birthdate": "string",
"email": "string",
"email_verified": true,
"family_name": "string",
"gender": "string",
"given_name": "string",
"locale": "string",
"middle_name": "string",
"name": "string",
"nickname": "string",
"phone_number": "string",
"phone_number_verified": true,
"picture": "string",
"preferred_username": "string",
"profile": "string",
"sub": "string",
"updated_at": 0,
"website": "string",
"zoneinfo": "string"
}
5 changes: 5 additions & 0 deletions tests/test_oauth_backends.py
Expand Up @@ -17,6 +17,7 @@
Strava,
LinkedIn,
ORCiD,
create_hydra_backend
)


Expand Down Expand Up @@ -114,3 +115,7 @@ def test_linkedin(self):

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

def test_hydra(self):
hydra = create_hydra_backend('hydra', 'localhost')
self.run_oauth_profile(hydra)

0 comments on commit b6f49f6

Please sign in to comment.