Skip to content

Commit

Permalink
Add ldap_or_authinfo identity provider (#42)
Browse files Browse the repository at this point in the history
Variant of the ldap provider with a fallback to data from the auth provider.
This is particularly useful with shibboleth where the auth provider already
has all the data and the identity provider would just pass them through anyway.
  • Loading branch information
jouvin authored Dec 13, 2023
1 parent eb99d40 commit d12c780
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 2 deletions.
4 changes: 2 additions & 2 deletions flask_multipass/providers/ldap/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
# Flask-Multipass is free software; you can redistribute it
# and/or modify it under the terms of the Revised BSD License.

from .providers import LDAPAuthProvider, LDAPGroup, LDAPIdentityProvider
from .providers import LDAPAuthProvider, LDAPGroup, LDAPIdentityProvider, AuthFallbackLDAPIdentityProvider


__all__ = ('LDAPAuthProvider', 'LDAPGroup', 'LDAPIdentityProvider')
__all__ = ('LDAPAuthProvider', 'LDAPGroup', 'LDAPIdentityProvider', 'AuthFallbackLDAPIdentityProvider')
20 changes: 20 additions & 0 deletions flask_multipass/providers/ldap/providers.py
Original file line number Diff line number Diff line change
Expand Up @@ -232,3 +232,23 @@ def search_groups(self, name, exact=False):
for group_dn, group_data in self._search_groups(search_filter):
group_name = to_unicode(group_data[self.ldap_settings['gid']][0])
yield self.group_class(self, group_name, group_dn)


class AuthFallbackLDAPIdentityProvider(LDAPIdentityProvider):
"""Provides identity information using LDAP with a fallback to auth provider data.
This identity provider is meant to be used together with an auth provider that provides
all the required data (in particular the Shibboleth provider).
By default it will use only the identifier from the auth provider and look up all the data
from LDAP.
In case the user does not have data in LDAP however, the data provided from the auth provider
will be used.
"""

def get_identity_from_auth(self, auth_info):
identifier = auth_info.data.get('identifier')
if identity := super().get_identity_from_auth(auth_info):
return identity
return IdentityInfo(self, identifier=identifier, **auth_info.data)
1 change: 1 addition & 0 deletions setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ flask_multipass.auth_providers =
static = flask_multipass.providers.static:StaticAuthProvider
flask_multipass.identity_providers =
ldap = flask_multipass.providers.ldap:LDAPIdentityProvider
ldap_or_authinfo = flask_multipass.providers.ldap:AuthFallbackLDAPIdentityProvider
authlib = flask_multipass.providers.authlib:AuthlibIdentityProvider
saml = flask_multipass.providers.saml:SAMLIdentityProvider
shibboleth = flask_multipass.providers.shibboleth:ShibbolethIdentityProvider
Expand Down

0 comments on commit d12c780

Please sign in to comment.