From 13a20478fd478747d16df83674992cffe89e2caf Mon Sep 17 00:00:00 2001 From: Jens Langhammer Date: Tue, 19 May 2020 21:53:46 +0200 Subject: [PATCH] sources/oauth: add OIDC client --- passbook/sources/oauth/settings.py | 1 + passbook/sources/oauth/types/oidc.py | 34 +++++++++++++++++++ .../passbook/sources/openid-connect.svg | 1 + 3 files changed, 36 insertions(+) create mode 100644 passbook/sources/oauth/types/oidc.py create mode 100644 passbook/static/static/passbook/sources/openid-connect.svg diff --git a/passbook/sources/oauth/settings.py b/passbook/sources/oauth/settings.py index 9fbaa3cf9..9678ee92e 100644 --- a/passbook/sources/oauth/settings.py +++ b/passbook/sources/oauth/settings.py @@ -12,4 +12,5 @@ PASSBOOK_SOURCES_OAUTH_TYPES = [ "passbook.sources.oauth.types.reddit", "passbook.sources.oauth.types.twitter", "passbook.sources.oauth.types.azure_ad", + "passbook.sources.oauth.types.oidc", ] diff --git a/passbook/sources/oauth/types/oidc.py b/passbook/sources/oauth/types/oidc.py new file mode 100644 index 000000000..49caa1e17 --- /dev/null +++ b/passbook/sources/oauth/types/oidc.py @@ -0,0 +1,34 @@ +"""OpenID Connect OAuth Views""" +from typing import Dict + +from passbook.sources.oauth.models import OAuthSource +from passbook.sources.oauth.types.manager import MANAGER, RequestKind +from passbook.sources.oauth.utils import user_get_or_create +from passbook.sources.oauth.views.core import OAuthCallback, OAuthRedirect + + +@MANAGER.source(kind=RequestKind.redirect, name="OpenID Connect") +class OpenIDConnectOAuthRedirect(OAuthRedirect): + """OpenIDConnect OAuth2 Redirect""" + + def get_additional_parameters(self, source: OAuthSource): + return { + "scope": "openid email", + } + + +@MANAGER.source(kind=RequestKind.callback, name="OpenID Connect") +class OpenIDConnectOAuth2Callback(OAuthCallback): + """OpenIDConnect OAuth2 Callback""" + + def get_user_id(self, source: OAuthSource, info: Dict[str, str]): + return info.get("sub") + + def get_or_create_user(self, source: OAuthSource, access, info: Dict[str, str]): + user_data = { + "username": info.get("username"), + "email": info.get("email"), + "name": info.get("username"), + "password": None, + } + return user_get_or_create(**user_data) diff --git a/passbook/static/static/passbook/sources/openid-connect.svg b/passbook/static/static/passbook/sources/openid-connect.svg new file mode 100644 index 000000000..785993703 --- /dev/null +++ b/passbook/static/static/passbook/sources/openid-connect.svg @@ -0,0 +1 @@ +Icons8 \ No newline at end of file