Welcome to mirror list, hosted at ThFree Co, Russian Federation.

SecretServerInterface.cs « TSS « ExternalConnectors - github.com/mRemoteNG/mRemoteNG.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 5129e4886719c20ead1469c765fce15ada1ada73 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
using Microsoft.Win32;
using SecretServerAuthentication.TSS;
using SecretServerRestClient.TSS;

namespace ExternalConnectors.TSS
{
    public class SecretServerInterface
    {
        private static class SSConnectionData
        {
            public static string ssUsername = "";
            public static string ssPassword = "";
            public static string ssUrl = "";
            public static string ssOTP = "";
            public static bool ssSSO = false;
            public static bool initdone = false;

            //token 
            public static string ssTokenBearer = "";
            public static DateTime ssTokenExpiresOn = DateTime.UtcNow;
            public static string ssTokenRefresh = "";

            public static void Init()
            {
                if (initdone == true)
                    return;

                RegistryKey key = Registry.CurrentUser.CreateSubKey(@"SOFTWARE\mRemoteSSInterface");
                try
                {
                    // display gui and ask for data
                    SSConnectionForm f = new SSConnectionForm();
                    string? un = key.GetValue("Username") as string;
                    f.tbUsername.Text = un ?? "";
                    f.tbPassword.Text = SSConnectionData.ssPassword;    // in OTP refresh cases, this value might already be filled

                    string? url = key.GetValue("URL") as string;
                    if (url == null || !url.Contains("://"))
                        url = "https://cred.domain.local/SecretServer";
                    f.tbSSURL.Text = url;

                    var b = key.GetValue("SSO");
                    if (b == null || (string)b != "True")
                        ssSSO = false;
                    else
                        ssSSO = true;
                    f.cbUseSSO.Checked = ssSSO;
                    
                    // show dialog
                    while (true)
                    {
                        _ = f.ShowDialog();

                        if (f.DialogResult != DialogResult.OK)
                            return;

                        // store values to memory
                        ssUsername = f.tbUsername.Text;
                        ssPassword = f.tbPassword.Text;
                        ssUrl = f.tbSSURL.Text;
                        ssSSO = f.cbUseSSO.Checked;
                        ssOTP = f.tbOTP.Text;
                        // check connection first
                        try
                        {
                            if (TestCredentials() == true)
                            {
                                initdone = true;
                                break;
                            }
                        }
                        catch (Exception)
                        {
                            MessageBox.Show("Test Credentials failed - please check your credentials");
                        }
                    }


                    // write values to registry
                    key.SetValue("Username", ssUsername);
                    key.SetValue("URL", ssUrl);
                    key.SetValue("SSO", ssSSO);
                }
                catch (Exception)
                {
                    throw;
                }
                finally
                {
                    key.Close();
                }

            }
        }

        private static bool TestCredentials()
        {
            if (SSConnectionData.ssSSO)
            {
                // checking creds doesn't really make sense here, as we can't modify them anyway if something is wrong
                return true;
            }
            else
            {

                if (!String.IsNullOrEmpty(GetToken()))
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
        }

        private static void FetchSecret(int secretID, out string secretUsername, out string secretPassword, out string secretDomain)
        {
            string baseURL = SSConnectionData.ssUrl;

            SecretModel secret;
            if (SSConnectionData.ssSSO)
            {
                // REQUIRES IIS CONFIG! https://docs.thycotic.com/ss/11.0.0/api-scripting/webservice-iwa-powershell
                var handler = new HttpClientHandler() { UseDefaultCredentials = true };
                using (var httpClient = new HttpClient(handler))
                {
                    // Call REST API:
                    var client = new SecretsServiceClient($"{baseURL}/winauthwebservices/api", httpClient);
                    secret = client.GetSecretAsync(false, true, secretID, null).Result;
                }
            }
            else
            {
                using (var httpClient = new HttpClient())
                {

                    var token = GetToken();
                    // Set credentials (token):
                    httpClient.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Bearer", token);

                    // Call REST API:
                    var client = new SecretsServiceClient($"{baseURL}/api", httpClient);
                    secret = client.GetSecretAsync(false, true, secretID, null).Result;
                }
            }

            // clear return variables
            secretDomain = "";
            secretUsername = "";
            secretPassword = "";

            // parse data and extract what we need
            foreach (var item in secret.Items)
            {
                if (item.FieldName.ToLower().Equals("domain"))
                    secretDomain = item.ItemValue;
                else if (item.FieldName.ToLower().Equals("username"))
                    secretUsername = item.ItemValue;
                else if (item.FieldName.ToLower().Equals("password"))
                    secretPassword = item.ItemValue;
            }

        }

        private static string GetToken()
        {
            // if there is no token, fetch a fresh one
            if (String.IsNullOrEmpty(SSConnectionData.ssTokenBearer))
            {
                return GetTokenFresh();
            }
            // if there is a token, check if it is valid
            if (SSConnectionData.ssTokenExpiresOn >= DateTime.UtcNow)
            {
                return SSConnectionData.ssTokenBearer;
            }
            else
            {
                // try using refresh token
                using (var httpClient = new HttpClient())
                {
                    var tokenClient = new OAuth2ServiceClient(SSConnectionData.ssUrl, httpClient);
                    TokenResponse token = new();
                    try
                    {
                        token = tokenClient.AuthorizeAsync(Grant_type.Refresh_token, null, null, SSConnectionData.ssTokenRefresh, null).Result;
                        var tokenResult = token.Access_token;

                        SSConnectionData.ssTokenBearer = tokenResult;
                        SSConnectionData.ssTokenRefresh = token.Refresh_token;
                        SSConnectionData.ssTokenExpiresOn = token.Expires_on;
                        return tokenResult;
                    }
                    catch (Exception)
                    {
                        // refresh token failed. clean memory and start fresh
                        SSConnectionData.ssTokenBearer = "";
                        SSConnectionData.ssTokenRefresh = "";
                        SSConnectionData.ssTokenExpiresOn = DateTime.Now;
                        // if OTP is required we need to ask user for a new OTP
                        if (!String.IsNullOrEmpty(SSConnectionData.ssOTP))
                        {
                            SSConnectionData.initdone = false;
                            // the call below executes a connection test, which fetches a valid token
                            SSConnectionData.Init();
                            // we now have a fresh token in memory. return it to caller
                            return SSConnectionData.ssTokenBearer;
                        }
                        else
                        {
                            // no user interaction required. get a fresh token and return it to caller
                            return GetTokenFresh();
                        }
                    }
                }
            }
        }
        static string GetTokenFresh()
        {
            using (var httpClient = new HttpClient())
            {
                // Authenticate:
                var tokenClient = new OAuth2ServiceClient(SSConnectionData.ssUrl, httpClient);
                // call below will throw an exception if the creds are invalid
                var token = tokenClient.AuthorizeAsync(Grant_type.Password, SSConnectionData.ssUsername, SSConnectionData.ssPassword, null, SSConnectionData.ssOTP).Result;
                // here we can be sure the creds are ok - return success state                   
                var tokenResult = token.Access_token;

                SSConnectionData.ssTokenBearer = tokenResult;
                SSConnectionData.ssTokenRefresh = token.Refresh_token;
                SSConnectionData.ssTokenExpiresOn = token.Expires_on;
                return tokenResult;
            }
        }



        // input must be in form "SSAPI:xxxx" where xxx is the secret id to fetch
        public static void FetchSecretFromServer(string input, out string username, out string password, out string domain)
        {
            // get secret id
            if (!input.StartsWith("SSAPI:"))
                throw new Exception("calling this function requires SSAPI: input");
            int secretID = Int32.Parse(input.Substring(6));

            // init connection credentials, display popup if necessary
            SSConnectionData.Init();

            // get the secret
            FetchSecret(secretID, out username, out password, out domain);
        }
    }
}