| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241 |
- using Codice.Client.Common.Servers;
- using Codice.Client.Common.Threading;
- using Codice.LogWrapper;
- using PlasticGui.Configuration.CloudEdition.Welcome;
- using PlasticGui.WebApi;
- using System;
- using System.Collections;
- using System.Collections.Generic;
- using Unity.PlasticSCM.Editor.UI;
- using Unity.PlasticSCM.Editor.WebApi;
- using UnityEditor;
- using UnityEngine;
- namespace Unity.PlasticSCM.Editor.Configuration.CloudEdition.Welcome
- {
- internal class AutoLogin : OAuthSignIn.INotify
- {
- internal enum State : byte
- {
- Off = 0,
- Started = 1,
- Running = 2,
- ResponseInit = 3,
- ResponseEnd = 6,
- ResponseSuccess = 7,
- OrganizationChoosed = 8,
- InitializingPlastic = 9,
- ErrorNoToken = 20,
- ErrorTokenException = 21,
- ErrorResponseNull = 22,
- ErrorResponseError = 23,
- ErrorTokenEmpty = 24,
- ErrorResponseCancel = 25
- }
- void OAuthSignIn.INotify.SuccessForConfigure(
- List<string> organizations,
- bool canCreateAnOrganization,
- string userName,
- string accessToken)
- {
- mPlasticWindow.GetWelcomeView().autoLoginState = AutoLogin.State.ResponseSuccess;
- ChooseOrganization(organizations, canCreateAnOrganization);
- }
- void OAuthSignIn.INotify.SuccessForSSO(string organization)
- {
- }
- void OAuthSignIn.INotify.SuccessForProfile(string email)
- {
- }
- void OAuthSignIn.INotify.SuccessForCredentials(
- string email,
- string accessToken)
- {
- }
- void OAuthSignIn.INotify.Cancel(string errorMessage)
- {
- mPlasticWindow.GetWelcomeView().autoLoginState = AutoLogin.State.ErrorResponseCancel;
- }
- internal void Run()
- {
- mPlasticWindow = GetPlasticWindow();
- if (!string.IsNullOrEmpty(CloudProjectSettings.accessToken))
- {
- ExchangeTokensAndJoinOrganization(CloudProjectSettings.accessToken);
- }
- else
- {
- mPlasticWindow.GetWelcomeView().autoLoginState = AutoLogin.State.ErrorNoToken;
- }
- }
- void ExchangeTokensAndJoinOrganization(string unityAccessToken)
- {
- int ini = Environment.TickCount;
- TokenExchangeResponse response = null;
- IThreadWaiter waiter = ThreadWaiter.GetWaiter(10);
- waiter.Execute(
- /*threadOperationDelegate*/ delegate
- {
- mPlasticWindow.GetWelcomeView().autoLoginState = AutoLogin.State.ResponseInit;
- response = PlasticScmRestApiClient.TokenExchange(unityAccessToken);
- },
- /*afterOperationDelegate*/ delegate
- {
- mLog.DebugFormat(
- "TokenExchange time {0} ms",
- Environment.TickCount - ini);
- if (waiter.Exception != null)
- {
- mPlasticWindow.GetWelcomeView().autoLoginState = AutoLogin.State.ErrorTokenException;
- ExceptionsHandler.LogException(
- "TokenExchangeSetting",
- waiter.Exception);
- return;
- }
- if (response == null)
- {
- mPlasticWindow.GetWelcomeView().autoLoginState = AutoLogin.State.ErrorResponseNull;
- Debug.Log("response null");
- return;
- }
-
- if (response.Error != null)
- {
- mPlasticWindow.GetWelcomeView().autoLoginState = AutoLogin.State.ErrorResponseError;
- mLog.ErrorFormat(
- "Unable to exchange token: {0} [code {1}]",
- response.Error.Message, response.Error.ErrorCode);
- return;
- }
- if (string.IsNullOrEmpty(response.AccessToken))
- {
- mPlasticWindow.GetWelcomeView().autoLoginState = AutoLogin.State.ErrorTokenEmpty;
- mLog.InfoFormat(
- "Access token is empty for user: {0}",
- response.User);
- return;
- }
- mPlasticWindow.GetWelcomeView().autoLoginState = AutoLogin.State.ResponseEnd;
- sAccessToken = response.AccessToken;
- sUserName = response.User;
- GetOrganizationList();
- });
- }
- internal void ExchangeTokens(string unityAccessToken)
- {
- int ini = Environment.TickCount;
- TokenExchangeResponse response = null;
- IThreadWaiter waiter = ThreadWaiter.GetWaiter(10);
- waiter.Execute(
- /*threadOperationDelegate*/ delegate
- {
- response = PlasticScmRestApiClient.TokenExchange(unityAccessToken);
- },
- /*afterOperationDelegate*/ delegate
- {
- mLog.DebugFormat(
- "TokenExchange time {0} ms",
- Environment.TickCount - ini);
-
- if (waiter.Exception != null)
- {
- ExceptionsHandler.LogException(
- "TokenExchangeSetting",
- waiter.Exception);
- return;
- }
-
- if (response == null)
- {
- Debug.Log("response null");
- return;
- }
-
- if (response.Error != null)
- {
- mLog.ErrorFormat(
- "Unable to exchange token: {0} [code {1}]",
- response.Error.Message,
- response.Error.ErrorCode);
- return;
- }
-
- if (string.IsNullOrEmpty(response.AccessToken))
- {
- mLog.InfoFormat(
- "Access token is empty for user: {0}",
- response.User);
- return;
- }
-
- sAccessToken = response.AccessToken;
- sUserName = response.User;
- });
- }
- internal void GetOrganizationList()
- {
- OAuthSignIn.GetOrganizationsFromAccessToken(
- mPlasticWindow.PlasticWebRestApiForTesting,
- new Editor.UI.Progress.ProgressControlsForDialogs(),
- this,
- CloudProjectSettings.userName,
- sAccessToken
- );
- }
- PlasticWindow GetPlasticWindow()
- {
- var windows = Resources.FindObjectsOfTypeAll<PlasticWindow>();
- PlasticWindow plasticWindow = windows.Length > 0 ? windows[0] : null;
- if (plasticWindow == null)
- plasticWindow = ShowWindow.Plastic();
- return plasticWindow;
- }
- internal void ChooseOrganization(List<string> organizations,
- bool canCreateAnOrganization)
- {
- mPlasticWindow = GetPlasticWindow();
- CloudEditionWelcomeWindow.ShowWindow(
- mPlasticWindow.PlasticWebRestApiForTesting,
- mPlasticWindow.CmConnectionForTesting,null, true);
- mCloudEditionWelcomeWindow = CloudEditionWelcomeWindow.GetWelcomeWindow();
- mCloudEditionWelcomeWindow.FillUserAndToken(sUserName, sAccessToken);
- if (organizations.Count == 1)
- {
- mCloudEditionWelcomeWindow.JoinOrganizationAndWelcomePage(organizations[0]);
- return;
- }
- mCloudEditionWelcomeWindow.ShowOrganizationPanelFromAutoLogin(organizations, canCreateAnOrganization);
- }
- internal static string sAccessToken = string.Empty;
- internal static string sUserName = string.Empty;
- PlasticWindow mPlasticWindow;
- CloudEditionWelcomeWindow mCloudEditionWelcomeWindow;
- static readonly ILog mLog = LogManager.GetLogger("TokensExchange");
- }
- }
|