#! /bin/bash
#
# Like what you see? Join us!
# https://www.univention.com/about-us/careers/vacancies/
#
# Copyright 2001-2022 Univention GmbH
#
# https://www.univention.de/
#
# All rights reserved.
#
# The source code of this program is made available
# under the terms of the GNU Affero General Public License version 3
# (GNU AGPL V3) as published by the Free Software Foundation.
#
# Binary versions of this program provided by Univention to you as
# well as other copyrighted, protected or trademarked materials like
# Logos, graphics, fonts, specific documentations and configurations,
# cryptographic keys etc. are subject to a license agreement between
# you and Univention and not subject to the GNU AGPL V3.
#
# In the case you use this program under the terms of the GNU AGPL V3,
# the program is provided in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public
# License with the Debian GNU/Linux or Univention distribution in file
# /usr/share/common-licenses/AGPL-3;

eval "$(ucr shell)"

set -e

KEYCLOAK_FQDN="${KEYCLOAK_FQDN:="$keycloak_server_sso_fqdn"}"
SIMPLESAMLPHP_FQDN="${SIMPLESAMLPHP_FQDN:="$ucs_server_sso_fqdn"}"

IDP_ALIAS="ucs-simplesamlphp"
SERVICE_URL="https://$KEYCLOAK_FQDN/realms/ucs/broker/$IDP_ALIAS/endpoint"
ID="https://$KEYCLOAK_FQDN/realms/ucs"
KEYCLOAK_USER="${keycloak_admin_user:="admin"}"
KEYCLOAK_PASSWORD_FILE="/etc/keycloak.secret"
SERVICE_METADATA_TMP="/tmp/$IDP_ALIAS.xml"

# shellcheck disable=SC2154
if [ "$server_role" == "domaincontroller_master" ]; then
    /usr/share/ucs-school-ui-common/scripts/univention-keycloak-simplesamlphp-idp \
        --admin-user "$KEYCLOAK_USER" \
        --admin-pwdfile "$KEYCLOAK_PASSWORD_FILE" \
        --simplesamlphp-fqdn "$SIMPLESAMLPHP_FQDN" \
        --keycloak-fqdn "$KEYCLOAK_FQDN" \
        --re-create \
        --idp-alias "$IDP_ALIAS"

    curl --silent --output "$SERVICE_METADATA_TMP" "$SERVICE_URL"/descriptor


    # shellcheck disable=SC2154
    udm saml/serviceprovider create --ignore_exists \
        --position "cn=saml-serviceprovider,cn=univention,$ldap_base" \
        --set serviceProviderMetadata="$(cat $SERVICE_METADATA_TMP)" \
        --set "Identifier=$ID" \
        --set isActivated=TRUE \
        --set "AssertionConsumerService=$SERVICE_URL" \
        --set "simplesamlNameIDAttribute=urn:oid:0.9.2342.19200300.100.1.1"

    udm portals/entry modify --dn "cn=login-saml,cn=entry,cn=portals,cn=univention,$ldap_base" \
        --set "activated=TRUE"

    rm "$SERVICE_METADATA_TMP"
fi

if [ "$server_role" = "domaincontroller_master" ] || [ "$server_role" = "domaincontroller_backup" ]; then
    if [ -z "${saml_apache2_content_security_policy_frame_ancestors}" ]; then
        ucr set saml/apache2/content-security-policy/frame-ancestors="'self' https://*.$(hostname -d)/"
        systemctl reload apache2.service
    fi
fi

ucr set portal/auth-mode=saml
systemctl restart univention-portal-server.service
