using System; using System.Collections.Generic; using Unity.Services.Authentication.Editor.Models; using Unity.Services.Core; using UnityEditor; using UnityEngine; using UnityEngine.UIElements; namespace Unity.Services.Authentication.Editor { class IdProviderOptions { public const string IdProviderApple = "apple.com"; public const string IdProviderGoogle = "google.com"; public const string IdProviderFacebook = "facebook.com"; public const string IdProviderSteam = "steampowered.com"; public string IdProviderType { get; set; } public string DisplayName { get; set; } public string ClientIdDisplayName { get; set; } = "Client ID"; public string ClientSecretDisplayName { get; set; } = "Client Secret"; public bool NeedClientSecret {get; set; } static readonly Dictionary s_IdProviderOptions = new Dictionary { [IdProviderApple] = new IdProviderOptions { IdProviderType = IdProviderApple, DisplayName = "Sign-in with Apple", ClientIdDisplayName = "App ID", NeedClientSecret = false }, [IdProviderGoogle] = new IdProviderOptions { IdProviderType = IdProviderGoogle, DisplayName = "Google", ClientIdDisplayName = "Client ID", NeedClientSecret = false }, [IdProviderFacebook] = new IdProviderOptions { IdProviderType = IdProviderFacebook, DisplayName = "Facebook", ClientIdDisplayName = "App ID", ClientSecretDisplayName = "App Secret", NeedClientSecret = true }, [IdProviderSteam] = new IdProviderOptions { IdProviderType = IdProviderSteam, DisplayName = "Steam", ClientIdDisplayName = "App ID", ClientSecretDisplayName = "Key", NeedClientSecret = true } }; public static IdProviderOptions GetOptions(string idProviderType) { if (!s_IdProviderOptions.ContainsKey(idProviderType)) { return null; } return s_IdProviderOptions[idProviderType]; } } class IdProviderElement : VisualElement { const string k_ElementUxml = "Packages/com.unity.services.authentication/Editor/UXML/IdProviderElement.uxml"; string m_IdDomainId; IAuthenticationAdminClient m_AdminClient; IdProviderOptions m_Options; Foldout m_Container; Toggle m_Enabled; TextField m_ClientId; TextField m_ClientSecret; Button m_SaveButton; Button m_CancelButton; Button m_DeleteButton; // Whether skip the confirmation window for tests/automation. bool m_SkipConfirmation; /// /// The foldout container to show or hide the ID provider details. /// public Foldout Container => m_Container; /// /// The toggle to control whether the ID provider is enabled. /// public Toggle EnabledToggle => m_Enabled; /// /// The text field to fill the client ID. /// public TextField ClientIdField => m_ClientId; /// /// The text field to fill the client secret. /// public TextField ClientSecretField => m_ClientSecret; /// /// The button to save the changes. /// public Button SaveButton => m_SaveButton; /// /// The button to cancel changes. /// public Button CancelButton => m_CancelButton; /// /// The button to delete the current ID provider. /// public Button DeleteButton => m_DeleteButton; /// /// Event triggered when the starts or finishes waiting for an async operation. /// The first parameter of the callback is the sender. /// The second parameter is true if it starts waiting, and false if it finishes waiting. /// public event Action Waiting; /// /// Event triggered when the current needs to be deleted by the container. /// The parameter of the callback is the sender. /// public event Action Deleted; /// /// Event triggered when the current catches an error. /// The first parameter of the callback is the sender. /// The second parameter is the exception caught by the element. /// public event Action Error; /// /// The value saved on the server side. /// public IdProviderResponse SavedValue { get; set; } /// /// The value of that is about to be saved to the server. /// public IdProviderResponse CurrentValue { get; set; } public bool Changed => SavedValue?.Type != CurrentValue?.Type || SavedValue?.Disabled != CurrentValue?.Disabled || (SavedValue?.ClientId ?? "") != (CurrentValue?.ClientId ?? "") || (SavedValue?.ClientSecret ?? "") != (CurrentValue?.ClientSecret ?? ""); public bool IsValid => !string.IsNullOrEmpty(CurrentValue.ClientId) && (!m_Options.NeedClientSecret || !string.IsNullOrEmpty(CurrentValue.ClientSecret)); public IdProviderElement(string idDomain, IAuthenticationAdminClient adminClient, IdProviderResponse savedValue, IdProviderOptions options, bool skipConfirmation = false) { m_IdDomainId = idDomain; m_AdminClient = adminClient; m_Options = options; m_SkipConfirmation = skipConfirmation; SavedValue = savedValue; CurrentValue = SavedValue.Clone(); var containerAsset = AssetDatabase.LoadAssetAtPath(k_ElementUxml); if (containerAsset != null) { var containerUI = containerAsset.CloneTree().contentContainer; m_Container = containerUI.Q(className: "auth-id-provider-details"); m_Container.text = savedValue.Type; // If the ID Provider element is new, default to unfold. m_Container.value = SavedValue.New; m_Enabled = containerUI.Q("id-provider-enabled"); m_Enabled.RegisterCallback>(OnEnabledChanged); m_ClientId = containerUI.Q("id-provider-client-id"); m_ClientId.label = options.ClientIdDisplayName; m_ClientId.RegisterCallback>(OnClientIdChanged); m_ClientSecret = containerUI.Q("id-provider-client-secret"); if (options.NeedClientSecret) { m_ClientSecret.label = options.ClientSecretDisplayName; m_ClientSecret.RegisterCallback>(OnClientSecretChanged); } else { m_ClientSecret.style.display = DisplayStyle.None; } m_SaveButton = containerUI.Q