项目作者: oarepo

项目描述 :
OpenID Connect Auth Backend for Invenio
高级语言: Python
项目地址: git://github.com/oarepo/invenio-openid-connect.git
创建时间: 2019-02-01T16:37:32Z
项目社区:https://github.com/oarepo/invenio-openid-connect

开源协议:MIT License

下载


Invenio OpenID Connect

image
image
image

Installation

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

  1. $ 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

  1. from invenio_openid_connect import InvenioAuthOpenIdRemote
  2. OPENIDC_CONFIG = dict(
  3. base_url='https://<openid-server>/openid/',
  4. consumer_key='<key from openid server>',
  5. consumer_secret='<secret from openid server>',
  6. # request_token_url = base_url
  7. # access_token_url = f'${base_url}/token'
  8. # access_token_method = 'POST'
  9. # authorize_url = f'${base_url}/authorize'
  10. # userinfo_url = f'${base_url}/userinfo'
  11. # scope = 'openid email profile'
  12. # signature_method = 'HMAC-SHA1'
  13. # # fields that will be used as a source of username (in this order, first field with value wins)
  14. # username_fields = ['username', 'preferred_username', 'sub', 'email']
  15. )
  16. OAUTHCLIENT_REST_REMOTE_APPS = dict(
  17. # the class from above, the auth method will be called "openid"
  18. openid=InvenioAuthOpenIdRemote().remote_app(),
  19. )

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.

  1. from invenio_openid_connect import InvenioAuthOpenIdRemote
  2. class CISLoginAuthRemote(InvenioAuthOpenIdRemote):
  3. # the name of the config settings in invenio.cfg . Default is OPENIDC_CONFIG
  4. CONFIG_OPENID = 'CIS_LOGIN_CONFIG'
  5. # human stuff
  6. name = 'CIS Login Server'
  7. description = 'Login server at CIS UCT Prague'
  8. icon = ''
  9. # userinfo class
  10. 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.

  1. class CISLoginUserInfoClass:
  2. sub: str = None
  3. name: str = None
  4. preferred_username: str = None
  5. given_name: str = None
  6. family_name: str = None
  7. zoneinfo: str = None
  8. locale: str = None
  9. email: str = None
  10. roles: dict = {}
  11. def __init__(self, userinfo: dict):
  12. for k, v in userinfo.items():
  13. setattr(self, k, v)
  14. self.roles = userinfo.get('http://cis.vscht.cz/openid#roles', {})
  15. def to_dict(self):
  16. return self.__dict__
  17. @property
  18. def username(self):
  19. if self.preferred_username:
  20. return self.preferred_username
  21. elif self.email:
  22. return self.email
  23. return self.sub

Then configure the remote as above.