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

Remove mixins, per-Authenticator LoginHandler classes #323

Merged
merged 19 commits into from
Jan 28, 2020
Merged
Show file tree
Hide file tree
Changes from 17 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
52 changes: 13 additions & 39 deletions docs/source/getting-started.rst
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ OAuthenticator currently supports the following authentication services:

- `Auth0 <oauthenticator/auth0.py>`__
- `Azure AD <#azure-ad-setup>`__
- `Azure AD B2C <#azure-ad-b2c-setup>`__
- `Bitbucket <oauthenticator/bitbucket.py>`__
- `CILogon <oauthenticator/cilogon.py>`__
- `GitHub <#github-setup>`__
Expand Down Expand Up @@ -180,36 +179,6 @@ See ``run.sh`` for an `example <./examples/azuread/>`__

- `Source Code <oauthenticator/azuread.py>`__

Azure AD B2C Setup
------------------

.. _prereqs-1:

*Prereqs*:
~~~~~~~~~~

- Requires: **``PyJWT>=1.5.3``**

::

> pip3 install PyJWT

- BE SURE TO SET THE **``OAUTH_ACCESS_TOKEN_URL``,
``OAUTH_AUTHORIZE_URL`` and ``OAUTH_SCOPE``** environment variables

::

> export OAUTH_ACCESS_TOKEN_URL='https://login.microsoftonline.com/YOUR_TENANT.onmicrosoft.com/oauth2/v2.0/token?p=YOUR_POLICY_NAME'
> export OAUTH_AUTHORIZE_URL='https://login.microsoftonline.com/YOUR_TENANT.onmicrosoft.com/oauth2/v2.0/authorize?p=YOUR_POLICY_NAME'
> export OAUTH_SCOPE='openid YOUR_RESOURCE'

Sample code
~~~~~~~~~~~

The sample code can be found at `examples
folder <./examples/azureadb2c/>`__ \* See ``run.sh`` for setting up
environment variables. \* See ``config.py`` for setting up such as
client id/secret and add_user_cmd.

Source code
~~~~~~~~~~~
Expand Down Expand Up @@ -507,8 +476,7 @@ Use the ``GenericOAuthenticator`` for Jupyterhub by editing your

.. code:: python

from oauthenticator.generic import GenericOAuthenticator
c.JupyterHub.authenticator_class = GenericOAuthenticator
c.JupyterHub.authenticator_class = "generic"

c.GenericOAuthenticator.oauth_callback_url = 'http://YOUR-JUPYTERHUB.com/hub/oauth_callback'
c.GenericOAuthenticator.client_id = 'MOODLE-CLIENT-ID'
Expand Down Expand Up @@ -542,12 +510,18 @@ Choose **Yandex.Passport API** in Permissions and check these options:

Set the above settings in your ``jupyterhub_config.py``:

\```python c.JupyterHub.authenticator_class =
‘oauthenticator.yandex.YandexPassportOAuthenticator’
c.YandexPassportOAuthenticator.oauth_callback_url =
‘https://[your-host]/hub/oauth_callback’
c.YandexPassportOAuthenticator.client_id = ‘[your app ID]’
c.YandexPassportOAuthenticator.client_secret = ‘[your app Password]’
.. code:: python

c.JupyterHub.authenticator_class = "generic"
c.OAuthenticator.oauth_callback_url = "https://[your-host]/hub/oauth_callback"
c.OAuthenticator.client_id = "[your app ID]""
c.OAuthenticator.client_secret = "[your app Password]"

c.GenericOAuthenticator.login_service = "Yandex.Passport"
c.GenericOAuthenticator.username_key = "login"
c.GenericOAuthenticator.authorize_url = "https://oauth.yandex.ru/authorize"
c.GenericOAuthenticator.token_url = "https://oauth.yandex.ru/token"
c.GenericOAuthenticator.userdata_url = "https://login.yandex.ru/info"

.. |PyPI| image:: https://img.shields.io/pypi/v/oauthenticator.svg
:target: https://pypi.python.org/pypi/oauthenticator
Expand Down
22 changes: 0 additions & 22 deletions examples/azureadb2c/config.py

This file was deleted.

6 changes: 0 additions & 6 deletions examples/azureadb2c/run.sh

This file was deleted.

69 changes: 39 additions & 30 deletions oauthenticator/auth0.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,26 +36,35 @@
from tornado import web
from tornado.httpclient import HTTPRequest, AsyncHTTPClient

from traitlets import Unicode, default

from jupyterhub.auth import LocalAuthenticator

from .oauth2 import OAuthLoginHandler, OAuthenticator

AUTH0_SUBDOMAIN = os.getenv('AUTH0_SUBDOMAIN')

class Auth0Mixin(OAuth2Mixin):
_OAUTH_AUTHORIZE_URL = "https://%s.auth0.com/authorize" % AUTH0_SUBDOMAIN
_OAUTH_ACCESS_TOKEN_URL = "https://%s.auth0.com/oauth/token" % AUTH0_SUBDOMAIN
class Auth0OAuthenticator(OAuthenticator):

login_service = "Auth0"

class Auth0LoginHandler(OAuthLoginHandler, Auth0Mixin):
pass
auth0_subdomain = Unicode(config=True)

@default("auth0_subdomain")
def _auth0_subdomain_default(self):
subdomain = os.getenv("AUTH0_SUBDOMAIN")
if not subdomain:
raise ValueError(
"Please specify $AUTH0_SUBDOMAIN env or %s.auth0_subdomain config"
% self.__class__.__name__
)

class Auth0OAuthenticator(OAuthenticator):
@default("authorize_url")
def _authorize_url_default(self):
return "https://%s.auth0.com/authorize" % self.auth0_subdomain

login_service = "Auth0"

login_handler = Auth0LoginHandler
@default("token_url")
def _token_url_default(self):
return "https://%s.auth0.com/oauth/token" % self.auth0_subdomain

async def authenticate(self, handler, data=None):
code = handler.get_argument("code")
Expand All @@ -66,45 +75,45 @@ async def authenticate(self, handler, data=None):
'grant_type': 'authorization_code',
'client_id': self.client_id,
'client_secret': self.client_secret,
'code':code,
'redirect_uri': self.get_callback_url(handler)
'code': code,
'redirect_uri': self.get_callback_url(handler),
}
url = "https://%s.auth0.com/oauth/token" % AUTH0_SUBDOMAIN
url = self.token_url

req = HTTPRequest(url,
method="POST",
headers={"Content-Type": "application/json"},
body=json.dumps(params)
)
req = HTTPRequest(
url,
method="POST",
headers={"Content-Type": "application/json"},
body=json.dumps(params),
)

resp = await http_client.fetch(req)
resp_json = json.loads(resp.body.decode('utf8', 'replace'))

access_token = resp_json['access_token']

# Determine who the logged in user is
headers={"Accept": "application/json",
"User-Agent": "JupyterHub",
"Authorization": "Bearer {}".format(access_token)
headers = {
"Accept": "application/json",
"User-Agent": "JupyterHub",
"Authorization": "Bearer {}".format(access_token),
}
req = HTTPRequest("https://%s.auth0.com/userinfo" % AUTH0_SUBDOMAIN,
method="GET",
headers=headers
)
req = HTTPRequest(
"https://%s.auth0.com/userinfo" % self.auth0_subdomain,
method="GET",
headers=headers,
)
resp = await http_client.fetch(req)
resp_json = json.loads(resp.body.decode('utf8', 'replace'))

return {
'name': resp_json["email"],
'auth_state': {
'access_token': access_token,
'auth0_user': resp_json,
}
'auth_state': {'access_token': access_token, 'auth0_user': resp_json},
}


class LocalAuth0OAuthenticator(LocalAuthenticator, Auth0OAuthenticator):

"""A version that mixes in local system user creation"""
pass

pass
Loading