Skip to main content

Invenio OpenID Connect Auth Backend

Project description

Invenio OpenID Connect

image image image

Installation

Invenio OpenID Connect is on PyPI so all you need is:

$ pip install invenio-openid-connect

Configuration

At first add this client to your openid server and get key and secret. Do not forget to set the allowed redirect url to:

https://localhost:5000/api/oauth/authorized/openid/

Then configure the backend handler in invenio.cfg

from invenio_openid_connect import InvenioAuthOpenIdRemote

OPENIDC_CONFIG = dict(
    base_url='https://<openid-server>/openid/',
    consumer_key='<key from openid server>',
    consumer_secret='<secret from openid server>',
    # request_token_url = base_url
    # access_token_url = f'${base_url}/token'
    # access_token_method = 'POST'
    # authorize_url = f'${base_url}/authorize'
    # userinfo_url = f'${base_url}/userinfo'
    # scope = 'openid email profile'
    # signature_method = 'HMAC-SHA1'
    # # fields that will be used as a source of username (in this order, first field with value wins)
    # username_fields = ['username', 'preferred_username', 'sub', 'email']
)

OAUTHCLIENT_REST_REMOTE_APPS = dict(
    # the class from above, the auth method will be called "openid"
    openid=InvenioAuthOpenIdRemote().remote_app(),
)

Note that the redirect uri above ends with openid - this is the same key as in OAUTHCLIENT_REST_REMOTE_APPS.

Usage

After local configuration and allowing access at your , head in your browser to https://localhost:5000/api/oauth/login/openid?next=/api/oauth/state (openid is the key in OAUTHCLIENT_REST_REMOTE_APPS). You should log in with your openid provider and be redirected to state API which accesses your userinfo data.

OpenID backend

To extend the functionality of the backend (for example, to add a custom UserInfo class) you might want to write your own backend.

from invenio_openid_connect import InvenioAuthOpenIdRemote

class CISLoginAuthRemote(InvenioAuthOpenIdRemote):
    # the name of the config settings in invenio.cfg . Default is OPENIDC_CONFIG
    CONFIG_OPENID = 'CIS_LOGIN_CONFIG'

    # human stuff
    name = 'CIS Login Server'
    description = 'Login server at CIS UCT Prague'
    icon = ''

    # userinfo class
    userinfo_cls = CISLoginUserInfoClass

Note that if your userinfo class does not inherit from dict it must implement to_dict method that is used by the state endpoint.

class CISLoginUserInfoClass:
    sub: str = None
    name: str = None
    preferred_username: str = None
    given_name: str = None
    family_name: str = None
    zoneinfo: str = None
    locale: str = None
    email: str = None
    roles: dict = {}

    def __init__(self, userinfo: dict):
        for k, v in userinfo.items():
            setattr(self, k, v)
        self.roles = userinfo.get('http://cis.vscht.cz/openid#roles', {})

    def to_dict(self):
        return self.__dict__

    @property
    def username(self):
        if self.preferred_username:
            return self.preferred_username
        elif self.email:
            return self.email
        return self.sub

Then configure the remote as above.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

invenio-openid-connect-2.1.0.tar.gz (15.5 kB view details)

Uploaded Source

Built Distribution

invenio_openid_connect-2.1.0-py2.py3-none-any.whl (16.1 kB view details)

Uploaded Python 2 Python 3

File details

Details for the file invenio-openid-connect-2.1.0.tar.gz.

File metadata

  • Download URL: invenio-openid-connect-2.1.0.tar.gz
  • Upload date:
  • Size: 15.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.3.0 pkginfo/1.7.0 requests/2.25.1 setuptools/53.0.0 requests-toolbelt/0.9.1 tqdm/4.56.2 CPython/3.9.1

File hashes

Hashes for invenio-openid-connect-2.1.0.tar.gz
Algorithm Hash digest
SHA256 7cb17ffc077a1d9cb2c3f57dd1e03301beeb6e71449c7f09e67e4ddf14a50a8e
MD5 61e4e76a599531ebc9a9174844e21555
BLAKE2b-256 9ec2dea2b8ee677f0b8a0f3dfef5b2f6b7c93ed2231e4397109a5b5084403e52

See more details on using hashes here.

File details

Details for the file invenio_openid_connect-2.1.0-py2.py3-none-any.whl.

File metadata

  • Download URL: invenio_openid_connect-2.1.0-py2.py3-none-any.whl
  • Upload date:
  • Size: 16.1 kB
  • Tags: Python 2, Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.3.0 pkginfo/1.7.0 requests/2.25.1 setuptools/53.0.0 requests-toolbelt/0.9.1 tqdm/4.56.2 CPython/3.9.1

File hashes

Hashes for invenio_openid_connect-2.1.0-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 850674edb056d1c47c10a3c16447c43e0db1cdbcda0ea40edac08c8a16532f34
MD5 3ecb2eb8f3ad76af61cbaa3be7049068
BLAKE2b-256 3e526bfa82f43785db8a50aaed7fcb542db67cfddaa0ead2d55233ef3584b0ec

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page