/* Copyright 2020 The Matrix.org Foundation C.I.C. 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. */ import React from "react"; import { chunk } from "lodash"; import classNames from "classnames"; import { MatrixClient } from "matrix-js-sdk/src/client"; import PlatformPeg from "../../../PlatformPeg"; import AccessibleButton from "./AccessibleButton"; import { _t } from "../../../languageHandler"; import { IdentityProviderBrand, IIdentityProvider, ISSOFlow } from "../../../Login"; import AccessibleTooltipButton from "./AccessibleTooltipButton"; import { mediaFromMxc } from "../../../customisations/Media"; import AppleSvg from '../../../../res/img/element-icons/brands/apple.svg'; import FacebookSvg from '../../../../res/img/element-icons/brands/facebook.svg'; import GithubSvg from '../../../../res/img/element-icons/brands/github.svg'; import GitlabSvg from '../../../../res/img/element-icons/brands/gitlab.svg'; import GoogleSvg from '../../../../res/img/element-icons/brands/google.svg'; import TwitterSvg from '../../../../res/img/element-icons/brands/twitter.svg'; interface ISSOButtonProps extends Omit { idp: IIdentityProvider; mini?: boolean; } const getIcon = (brand: IdentityProviderBrand | string) => { switch (brand) { case IdentityProviderBrand.Apple: return AppleSvg; case IdentityProviderBrand.Facebook: return FacebookSvg; case IdentityProviderBrand.Github: return GithubSvg; case IdentityProviderBrand.Gitlab: return GitlabSvg; case IdentityProviderBrand.Google: return GoogleSvg; case IdentityProviderBrand.Twitter: return TwitterSvg; default: return null; } }; const SSOButton: React.FC = ({ matrixClient, loginType, fragmentAfterLogin, idp, primary, mini, ...props }) => { const label = idp ? _t("Continue with %(provider)s", { provider: idp.name }) : _t("Sign in with single sign-on"); const onClick = () => { PlatformPeg.get().startSingleSignOn(matrixClient, loginType, fragmentAfterLogin, idp?.id); }; let icon; let brandClass; const brandIcon = idp ? getIcon(idp.brand) : null; if (brandIcon) { const brandName = idp.brand.split(".").pop(); brandClass = `mx_SSOButton_brand_${brandName}`; icon = {brandName}; } else if (typeof idp?.icon === "string" && idp.icon.startsWith("mxc://")) { const src = mediaFromMxc(idp.icon, matrixClient).getSquareThumbnailHttp(24); icon = {idp.name}; } const classes = classNames("mx_SSOButton", { [brandClass]: brandClass, mx_SSOButton_mini: mini, mx_SSOButton_default: !idp, mx_SSOButton_primary: primary, }); if (mini) { // TODO fallback icon return ( { icon } ); } return ( { icon } { label } ); }; interface IProps { matrixClient: MatrixClient; flow: ISSOFlow; loginType?: "sso" | "cas"; fragmentAfterLogin?: string; primary?: boolean; } const MAX_PER_ROW = 6; const SSOButtons: React.FC = ({ matrixClient, flow, loginType, fragmentAfterLogin, primary }) => { const providers = flow.identity_providers || []; if (providers.length < 2) { return
; } const rows = Math.ceil(providers.length / MAX_PER_ROW); const size = Math.ceil(providers.length / rows); return
{ chunk(providers, size).map(chunk => (
{ chunk.map(idp => ( )) }
)) }
; }; export default SSOButtons;