You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
128 lines
4.1 KiB
Python
128 lines
4.1 KiB
Python
5 years ago
|
# -*- coding: utf-8 -*-
|
||
5 years ago
|
# Copyright 2020 The Matrix.org Foundation C.I.C.
|
||
5 years ago
|
#
|
||
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
||
|
# you may not use this file except in compliance with the License.
|
||
|
# You may obtain a copy of the License at
|
||
|
#
|
||
|
# http://www.apache.org/licenses/LICENSE-2.0
|
||
|
#
|
||
|
# Unless required by applicable law or agreed to in writing, software
|
||
|
# distributed under the License is distributed on an "AS IS" BASIS,
|
||
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||
|
# See the License for the specific language governing permissions and
|
||
|
# limitations under the License.
|
||
5 years ago
|
import logging
|
||
|
import random
|
||
|
import string
|
||
|
import time
|
||
5 years ago
|
from typing import Tuple
|
||
5 years ago
|
|
||
5 years ago
|
import attr
|
||
5 years ago
|
import saml2.response
|
||
5 years ago
|
|
||
5 years ago
|
import synapse.module_api
|
||
|
from synapse.module_api.errors import RedirectException
|
||
|
|
||
|
from matrix_synapse_saml_mozilla._sessions import (
|
||
|
UsernameMappingSession,
|
||
|
username_mapping_sessions,
|
||
|
expire_old_sessions,
|
||
|
SESSION_COOKIE_NAME,
|
||
|
)
|
||
|
|
||
|
logger = logging.getLogger(__name__)
|
||
|
|
||
|
|
||
|
MAPPING_SESSION_VALIDITY_PERIOD_MS = 15 * 60 * 1000
|
||
5 years ago
|
|
||
|
|
||
5 years ago
|
@attr.s
|
||
|
class SamlConfig(object):
|
||
5 years ago
|
pass
|
||
5 years ago
|
|
||
|
|
||
5 years ago
|
class SamlMappingProvider(object):
|
||
5 years ago
|
def __init__(
|
||
|
self, parsed_config: SamlConfig, module_api: synapse.module_api.ModuleApi
|
||
|
):
|
||
5 years ago
|
"""A Mozilla-flavoured, Synapse user mapping provider
|
||
|
|
||
|
Args:
|
||
|
parsed_config: A configuration object. The result of self.parse_config
|
||
|
"""
|
||
5 years ago
|
self._random = random.SystemRandom()
|
||
5 years ago
|
|
||
5 years ago
|
def saml_response_to_user_attributes(
|
||
5 years ago
|
self,
|
||
|
saml_response: saml2.response.AuthnResponse,
|
||
|
failures: int,
|
||
|
client_redirect_url: str,
|
||
5 years ago
|
) -> dict:
|
||
|
"""Maps some text from a SAML response to attributes of a new user
|
||
5 years ago
|
Args:
|
||
5 years ago
|
saml_response: A SAML auth response object
|
||
5 years ago
|
|
||
5 years ago
|
failures: How many times a call to this function with this
|
||
|
saml_response has resulted in a failure
|
||
5 years ago
|
|
||
5 years ago
|
client_redirect_url: where the client wants to redirect back to
|
||
|
|
||
5 years ago
|
Returns:
|
||
5 years ago
|
dict: A dict containing new user attributes. Possible keys:
|
||
|
* mxid_localpart (str): Required. The localpart of the user's mxid
|
||
|
* displayname (str): The displayname of the user
|
||
5 years ago
|
"""
|
||
5 years ago
|
remote_user_id = saml_response.ava["uid"][0]
|
||
5 years ago
|
displayname = saml_response.ava.get("displayName", [None])[0]
|
||
|
|
||
5 years ago
|
expire_old_sessions()
|
||
5 years ago
|
|
||
5 years ago
|
# make up a cryptorandom session id
|
||
|
session_id = "".join(
|
||
|
self._random.choice(string.ascii_letters) for _ in range(16)
|
||
|
)
|
||
5 years ago
|
|
||
5 years ago
|
now = int(time.time() * 1000)
|
||
|
session = UsernameMappingSession(
|
||
|
remote_user_id=remote_user_id,
|
||
|
displayname=displayname,
|
||
|
client_redirect_url=client_redirect_url,
|
||
|
expiry_time_ms=now + MAPPING_SESSION_VALIDITY_PERIOD_MS,
|
||
|
)
|
||
5 years ago
|
|
||
5 years ago
|
username_mapping_sessions[session_id] = session
|
||
|
logger.info("Recorded registration session id %s", session_id)
|
||
5 years ago
|
|
||
5 years ago
|
# Redirect to the username picker
|
||
|
e = RedirectException(b"/_matrix/saml2/pick_username/")
|
||
|
e.cookies.append(
|
||
|
b"%s=%s; path=/" % (SESSION_COOKIE_NAME, session_id.encode("ascii"),)
|
||
|
)
|
||
|
raise e
|
||
5 years ago
|
|
||
|
@staticmethod
|
||
5 years ago
|
def parse_config(config: dict) -> SamlConfig:
|
||
5 years ago
|
"""Parse the dict provided by the homeserver's config
|
||
|
Args:
|
||
|
config: A dictionary containing configuration options for this provider
|
||
|
Returns:
|
||
5 years ago
|
SamlConfig: A custom config object
|
||
5 years ago
|
"""
|
||
5 years ago
|
return SamlConfig()
|
||
5 years ago
|
|
||
|
@staticmethod
|
||
5 years ago
|
def get_saml_attributes(config: SamlConfig) -> Tuple[set, set]:
|
||
5 years ago
|
"""Returns the required and optional attributes of a SAML auth response object
|
||
5 years ago
|
|
||
|
Args:
|
||
5 years ago
|
config: A SamlConfig object containing configuration options for this provider
|
||
5 years ago
|
|
||
|
Returns:
|
||
5 years ago
|
tuple[set,set]: The first set equates to the saml auth response
|
||
|
attributes that are required for the module to function, whereas the
|
||
|
second set consists of those attributes which can be used if
|
||
|
available, but are not necessary
|
||
5 years ago
|
"""
|
||
5 years ago
|
return {"uid"}, {"displayName"}
|