Giter Club home page Giter Club logo

invenio-openid-connect's Introduction

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.

invenio-openid-connect's People

Contributors

dependabot-preview[bot] avatar dependabot[bot] avatar mesemus avatar mirekys avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

invenio-openid-connect's Issues

Dependabot couldn't fetch all your path-based dependencies

Dependabot couldn't fetch one or more of your project's path-based Python dependencies. The affected dependencies were docs/setup.py.

To use path-based dependencies with Dependabot the paths must be relative and resolve to a directory in this project's source code.

You can mention @dependabot in the comments below to contact the Dependabot team.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.