A game about forced loneliness, made by TACStudios
1using System;
2using System.Threading.Tasks;
3
4using UnityEditor;
5
6using Codice.CM.Common;
7using Codice.Client.Common;
8using Codice.Client.Common.Connection;
9using Codice.Client.Common.Threading;
10using PlasticGui;
11using Unity.PlasticSCM.Editor.UI;
12using Unity.PlasticSCM.Editor.WebApi;
13
14namespace Unity.PlasticSCM.Editor.Configuration
15{
16 internal class CredentialsUiImpl : AskCredentialsToUser.IGui
17 {
18 AskCredentialsToUser.DialogData AskCredentialsToUser.IGui.AskUserForCredentials(
19 string servername, SEIDWorkingMode seidWorkingMode)
20 {
21 if (!PlasticPlugin.ConnectionMonitor.IsConnected)
22 return AskCredentialsToUser.DialogData.Failure(seidWorkingMode);
23
24 if (seidWorkingMode == SEIDWorkingMode.OIDCWorkingMode
25 || seidWorkingMode == SEIDWorkingMode.SAMLWorkingMode)
26 {
27 throw new NotImplementedException(
28 string.Format("Authentication for {0} not supported yet.", seidWorkingMode));
29 }
30
31 AskCredentialsToUser.DialogData result = null;
32
33 GUIActionRunner.RunGUIAction(delegate
34 {
35 result = seidWorkingMode == SEIDWorkingMode.SSOWorkingMode
36 ? RunSSOCredentialsRequest(
37 servername, CloudProjectSettings.accessToken)
38 : CredentialsDialog.RequestCredentials(
39 servername, seidWorkingMode, ParentWindow.Get());
40 });
41
42 return result;
43 }
44
45 AskCredentialsToUser.DialogData RunSSOCredentialsRequest(
46 string cloudServer,
47 string unityAccessToken)
48 {
49 if (string.IsNullOrEmpty(unityAccessToken))
50 {
51 return SSOCredentialsDialog.RequestCredentials(
52 cloudServer, ParentWindow.Get());
53 }
54
55 TokenExchangeResponse tokenExchangeResponse =
56 WaitUntilTokenExchange(unityAccessToken);
57
58 // There is no internet connection, so no way to get credentials
59 if (tokenExchangeResponse == null)
60 {
61 return AskCredentialsToUser.DialogData.Failure(SEIDWorkingMode.SSOWorkingMode);
62 }
63
64 if (tokenExchangeResponse.Error == null)
65 {
66 return AskCredentialsToUser.DialogData.Success(
67 new Credentials(
68 new SEID(
69 tokenExchangeResponse.User,
70 false,
71 tokenExchangeResponse.AccessToken),
72 SEIDWorkingMode.SSOWorkingMode));
73 }
74
75 return SSOCredentialsDialog.RequestCredentials(
76 cloudServer, ParentWindow.Get());
77 }
78
79 static TokenExchangeResponse WaitUntilTokenExchange(
80 string unityAccessToken)
81 {
82 TokenExchangeResponse result = null;
83
84 Task.Run(() =>
85 {
86 try
87 {
88 result = WebRestApiClient
89 .PlasticScm
90 .TokenExchange(unityAccessToken);
91 }
92 catch (Exception ex)
93 {
94 ExceptionsHandler.LogException(
95 "CredentialsUiImpl", ex);
96 }
97 }).Wait();
98
99 return result;
100 }
101 }
102}