Skip to content
Snippets Groups Projects
auth_saml.py 2.49 KiB
Newer Older
Florent Aide's avatar
Florent Aide committed
# -*- encoding: utf-8 -*-
Florent Aide's avatar
Florent Aide committed
from openerp import models
from openerp import api
from openerp import fields
Houzefa Abbasbhay's avatar
Houzefa Abbasbhay committed
import lasso
import simplejson


Florent Aide's avatar
Florent Aide committed
class auth_saml_provider(models.Model):
Houzefa Abbasbhay's avatar
Houzefa Abbasbhay committed
    """Class defining the configuration values of an Saml2 provider"""

    _name = 'auth.saml.provider'
    _description = 'SAML2 provider'
    _order = 'name'

Florent Aide's avatar
Florent Aide committed
    @api.multi
    def _get_lasso_for_provider(self):
Florent Aide's avatar
Florent Aide committed
        """internal helper to get a configured lasso.Login object for the
        given provider id"""

        # user is not connected yet... so use SUPERUSER_ID
Houzefa Abbasbhay's avatar
Houzefa Abbasbhay committed

        # TODO: we should cache those results somewhere because it is
Florent Aide's avatar
Florent Aide committed
        # really costly to always recreate a login variable from buffers
Houzefa Abbasbhay's avatar
Houzefa Abbasbhay committed
        server = lasso.Server.newFromBuffers(
Florent Aide's avatar
Florent Aide committed
            self.sp_metadata,
            self.sp_pkey
Houzefa Abbasbhay's avatar
Houzefa Abbasbhay committed
        )
        server.addProviderFromBuffer(
            lasso.PROVIDER_ROLE_IDP,
Florent Aide's avatar
Florent Aide committed
            self.idp_metadata
Houzefa Abbasbhay's avatar
Houzefa Abbasbhay committed
        )
        return lasso.Login(server)

Florent Aide's avatar
Florent Aide committed
    @api.multi
    def _get_matching_attr_for_provider(self):
        """internal helper to fetch the matching attribute for this SAML
        provider. Returns a unicode object.
        """

        self.ensure_one()

        return self.matching_attribute

    @api.multi
Florent Aide's avatar
Florent Aide committed
    def _get_auth_request(self, state):
Houzefa Abbasbhay's avatar
Houzefa Abbasbhay committed
        """build an authentication request and give it back to our client
        WARNING: this method cannot be used for multiple ids
        """
Florent Aide's avatar
Florent Aide committed
        login = self._get_lasso_for_provider()
Houzefa Abbasbhay's avatar
Houzefa Abbasbhay committed

        # ! -- this is the part that MUST be performed on each call and
        # cannot be cached
        login.initAuthnRequest()
        login.request.nameIdPolicy.format = None
        login.request.nameIdPolicy.allowCreate = True
        login.msgRelayState = simplejson.dumps(state)
        login.buildAuthnRequestMsg()

        # msgUrl is a fully encoded url ready for redirect use
        # obtained after the buildAuthnRequestMsg() call
        return login.msgUrl

Florent Aide's avatar
Florent Aide committed
    # Name of the OAuth2 entity, authentic, xcg...
    name = fields.Char('Provider name')
    idp_metadata = fields.Text('IDP Configuration')
    sp_metadata = fields.Text('SP Configuration')
    sp_pkey = fields.Text(
        'Private key of our service provider (this openerpserver)'
    )
    matching_attribute = fields.Text(
        string='Matching Attribute',
        default='subject.nameId',
        required=True,
    ),
Florent Aide's avatar
Florent Aide committed
    enabled = fields.Boolean('Enabled', default=False)
    sequence = fields.Integer('Sequence')
    css_class = fields.Char('CSS Class')
    body = fields.Char('Body')