-
Notifications
You must be signed in to change notification settings - Fork 4
/
LatchAsync.cs
334 lines (304 loc) · 16.2 KB
/
LatchAsync.cs
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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
/*
Latch C# SDK - Set of reusable classes to allow developers integrate Latch on their applications.
Copyright (C) 2023 Telefonica Digital.
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;
namespace LatchSDK
{
#if NETSTANDARD || NET45_OR_GREATER
public sealed class LatchAsync : LatchBase
{
public LatchAsync(string appId, string secretKey) : base(appId, secretKey)
{
}
/// <summary>
/// Pairs an account using its name
/// </summary>
/// <param name="id">Name of the account</param>
/// <returns>If everything goes well, a <code>LatchResponse</code> object containing the account ID</returns>
/// <remarks>Only works in test backend</remarks>
public async Task<LatchResponse> PairWithIdAsync(string id)
{
return await HttpPerformRequestAsync(API_PAIR_WITH_ID_URL + "/" + UrlEncode(id));
}
/// <summary>
/// Pairs an account using its name
/// </summary>
/// <param name="id">Name of the account</param>
/// <param name="commonName">Name attached to this pairing. Showed in admin panels</param>
/// <returns>If everything goes well, a <code>LatchResponse</code> object containing the account ID</returns>
/// <remarks>Only works in test backend</remarks>
public async Task<LatchResponse> PairWithIdAsync(string id, string commonName)
{
var data = new Dictionary<string, string>()
{
{ "commonName", commonName }
};
return await HttpPerformRequestAsync(API_PAIR_WITH_ID_URL + "/" + UrlEncode(id), HttpMethod.POST, data);
}
/// <summary>
/// Pairs an account using a token
/// </summary>
/// <param name="token">Pairing token obtained by the user from the mobile application</param>
/// <returns>If everything goes well, a <code>LatchResponse</code> object containing the AccountID</returns>
public async Task<LatchResponse> PairAsync(string token)
{
return await HttpPerformRequestAsync(API_PAIR_URL + "/" + UrlEncode(token));
}
/// <summary>
/// Pairs an account using a token
/// </summary>
/// <param name="token">Pairing token obtained by the user from the mobile application</param>
/// <param name="commonName">Name attached to this pairing. Showed in admin panels</param>
/// <returns>If everything goes well, a <code>LatchResponse</code> object containing the AccountID</returns>
public LatchResponse Pair(string token, string commonName)
{
var data = new Dictionary<string, string>()
{
{ "commonName", commonName }
};
return HttpPerformRequest(API_PAIR_URL + "/" + UrlEncode(token), HttpMethod.POST, data);
}
/// <summary>
/// Requests the status of the specified account ID
/// </summary>
/// <param name="accountId">The account ID of the user</param>
/// <returns>If everything goes well, a <code>LatchResponse</code> object containing the status of the account</returns>
public async Task<LatchResponse> StatusAsync(string accountId)
{
return await HttpPerformRequestAsync(API_CHECK_STATUS_URL + "/" + UrlEncode(accountId));
}
/// <summary>
/// Requests the status of the specified account ID and operation ID
/// </summary>
/// <param name="accountId">The account ID of the user</param>
/// <param name="operationId">The operation ID to be checked</param>
/// <returns>If everything goes well, a <code>LatchResponse</code> object containing the status of the operation</returns>
public async Task<LatchResponse> OperationStatusAsync(string accountId, string operationId)
{
return await HttpPerformRequestAsync(API_CHECK_STATUS_URL + "/" + UrlEncode(accountId) + "/op/" + UrlEncode(operationId));
}
/// <summary>
/// Unpairs the specified account ID
/// </summary>
/// <param name="accountId">The account ID of the user</param>
/// <returns>If everything goes well, an empty response</returns>
public async Task<LatchResponse> UnpairAsync(string accountId)
{
return await HttpPerformRequestAsync(API_UNPAIR_URL + "/" + UrlEncode(accountId));
}
/// <summary>
/// Locks the specified account ID
/// </summary>
/// <param name="accountId">The account ID of the user to be locked</param>
/// <returns>If everything goes well, an empty response</returns>
/// <remarks>Only for premium accounts</remarks>
public async Task<LatchResponse> LockAsync(string accountId)
{
return await HttpPerformRequestAsync(API_LOCK_URL + "/" + UrlEncode(accountId), HttpMethod.POST);
}
/// <summary>
/// Locks the specified operation ID of the specified account ID
/// </summary>
/// <param name="accountId">The account ID of the user</param>
/// <param name="operationId">The operation ID to be locked</param>
/// <returns>If everything goes well, an empty response</returns>
/// <remarks>Only for premium accounts</remarks>
public async Task<LatchResponse> Lock(string accountId, string operationId)
{
return await HttpPerformRequestAsync(API_LOCK_URL + "/" + UrlEncode(accountId) + "/op/" + UrlEncode(operationId), HttpMethod.POST);
}
/// <summary>
/// Unlocks the specified account ID
/// </summary>
/// <param name="accountId">The account ID of the user to be unlocked</param>
/// <returns>If everything goes well, an empty response</returns>
/// <remarks>Only for premium accounts</remarks>
public async Task<LatchResponse> Unlock(string accountId)
{
return await HttpPerformRequestAsync(API_UNLOCK_URL + "/" + UrlEncode(accountId), HttpMethod.POST);
}
/// <summary>
/// Unlocks the specified operation ID of the specified account ID
/// </summary>
/// <param name="accountId">The account ID of the user</param>
/// <param name="operationId">The operation ID to be unlocked</param>
/// <returns>If everything goes well, an empty response</returns>
/// <remarks>Only for premium accounts</remarks>
public async Task<LatchResponse> Unlock(string accountId, string operationId)
{
return await HttpPerformRequestAsync(API_UNLOCK_URL + "/" + UrlEncode(accountId) + "/op/" + UrlEncode(operationId), HttpMethod.POST);
}
/// <summary>
/// Requests the history of the specified account ID
/// </summary>
/// <param name="accountId">The account ID of the user</param>
/// <returns>If everything goes well, a <code>LatchResponse</code> object containing the requested information</returns>
/// <remarks>Only for premium accounts</remarks>
public async Task<LatchResponse> History(string accountId)
{
return await HttpPerformRequestAsync(API_HISTORY_URL + "/" + UrlEncode(accountId));
}
/// <summary>
/// Requests the history of the specified account ID between two instants
/// </summary>
/// <param name="accountId">The account ID of the user</param>
/// <param name="from">Starting date and time</param>
/// <param name="to">Ending date and time</param>
/// <returns>If everything goes well, a <code>LatchResponse</code> object containing the requested information</returns>
/// <remarks>Only for premium accounts</remarks>
public async Task<LatchResponse> History(string accountId, DateTime? from, DateTime? to)
{
long? fromMillisFromEpoch = from.HasValue ? GetMillisecondsFromEpoch(from.Value) : (long?)null;
long? toMillisFromEpoch = to.HasValue ? GetMillisecondsFromEpoch(to.Value) : (long?)null;
return await History(accountId, fromMillisFromEpoch, toMillisFromEpoch);
}
/// <summary>
/// Requests the history of the specified account ID between two instants
/// </summary>
/// <param name="accountId">The account ID of the user</param>
/// <param name="fromMillisFromEpoch">Starting date and time in milliseconds ellapsed from Epoch time</param>
/// <param name="toMillisFromEpoch">Ending date and time in milliseconds ellapsed from Epoch time</param>
/// <returns>If everything goes well, a <code>LatchResponse</code> object containing the requested information</returns>
/// <remarks>Only for premium accounts</remarks>
public async Task<LatchResponse> History(string accountId, long? fromMillisFromEpoch, long? toMillisFromEpoch)
{
return await HttpPerformRequestAsync(API_HISTORY_URL + "/" + UrlEncode(accountId) + "/" +
fromMillisFromEpoch.GetValueOrDefault(0).ToString() + "/" +
toMillisFromEpoch.GetValueOrDefault(GetMillisecondsFromEpoch(DateTime.Now)).ToString());
}
/// <summary>
/// Gets all operations of the application
/// </summary>
/// <returns>If everything goes well, a <code>LatchResponse</code> object containing all operations</returns>
public async Task<LatchResponse> GetOperations()
{
return await HttpPerformRequestAsync(API_OPERATION_URL);
}
/// <summary>
/// Gets all suboperations under the specified parent operation
/// </summary>
/// <param name="parentOperationId">Parent operation ID</param>
/// <returns>If everything goes well, a <code>LatchResponse</code> object containing all suboperations</returns>
public async Task<LatchResponse> GetOperations(string parentOperationId)
{
return await HttpPerformRequestAsync(API_OPERATION_URL + "/" + UrlEncode(parentOperationId));
}
/// <summary>
/// Creates a new operation with the specified parameters
/// </summary>
/// <param name="parentId">Parent operation ID</param>
/// <param name="name">Name of the new operation</param>
/// <param name="twoFactor">Two factor (OTP) mode (optional)</param>
/// <param name="lockOnRequest">Lock on request (LOR) mode (optional)</param>
/// <returns>If everything goes well, a <code>LatchResponse</code> object containing the new operation ID</returns>
public async Task<LatchResponse> CreateOperation(string parentId, string name, FeatureMode twoFactor = FeatureMode.DISABLED, FeatureMode lockOnRequest = FeatureMode.DISABLED)
{
var data = new Dictionary<string, string>();
data.Add("parentId", parentId);
data.Add("name", name);
data.Add("two_factor", twoFactor.ToString());
data.Add("lock_on_request", lockOnRequest.ToString());
return await HttpPerformRequestAsync(API_OPERATION_URL, HttpMethod.PUT, data);
}
/// <summary>
/// Removes an existing operation
/// </summary>
/// <param name="operationId">Operation ID to remove</param>
/// <returns>If everything goes well, an empty response</returns>
public async Task<LatchResponse> RemoveOperation(string operationId)
{
return await HttpPerformRequestAsync(API_OPERATION_URL + "/" + UrlEncode(operationId), HttpMethod.DELETE);
}
/// <summary>
/// Updates an existing operation with one or more of the specified parameters
/// </summary>
/// <param name="operationId">Operation ID to change</param>
/// <param name="name">New name of the operation (optional)</param>
/// <param name="twoFactor">New two factor (OTP) mode (optional)</param>
/// <param name="lockOnRequest">New lock on request (LOR) mode (optional)</param>
/// <returns>If everything goes well, an empty response</returns>
public async Task<LatchResponse> UpdateOperation(string operationId, string name, FeatureMode? twoFactor = null, FeatureMode? lockOnRequest = null)
{
var data = new Dictionary<string, string>();
if (!String.IsNullOrEmpty(name))
data.Add("name", name);
if (twoFactor.HasValue)
data.Add("two_factor", twoFactor.ToString());
if (lockOnRequest.HasValue)
data.Add("lock_on_request", lockOnRequest.ToString());
return await HttpPerformRequestAsync(API_OPERATION_URL + "/" + UrlEncode(operationId), HttpMethod.POST, data);
}
/// <summary>
/// Create a new TOTP for a given user
/// </summary>
/// <param name="userId">Identifier associated with the user (provider-dependent)</param>
/// <param name="commonName">Name associated with the user (provider-dependent)</param>
/// <returns>If everything goes well, a <code>LatchResponse</code> object containing the TOTP ID and all of its properties</returns>
public async Task<LatchResponse> CreateTOTP(string userId, string commonName)
{
var data = new Dictionary<string, string>()
{
{ "userId", userId },
{ "commonName", commonName }
};
return await HttpPerformRequestAsync(API_TOTP_URL, HttpMethod.POST, data);
}
/// <summary>
/// Delete a specific TOTP using its identifier totpId
/// </summary>
/// <param name="totpId">Identifier of the TOTP to be deleted</param>
/// <returns>If everything goes well, an empty response</returns>
public async Task<LatchResponse> DeleteTOTP(string totpId)
{
return await HttpPerformRequestAsync(API_TOTP_URL + "/" + UrlEncode(totpId));
}
/// <summary>
/// Returns the information of a specific TOTP based on its totpId
/// </summary>
/// <param name="totpId">Identifier of the TOTP to be retrieved</param>
/// <returns>If everything goes well, a <code>LatchResponse</code> object containing the TOTP properties</returns>
public async Task<LatchResponse> GetTOTP(string totpId)
{
return await HttpPerformRequestAsync(API_TOTP_URL + "/" + UrlEncode(totpId));
}
/// <summary>
/// Validates a specific TOTP code provided by the user. The totpId identifies the TOTP, and code is the generated code that needs to be validated
/// </summary>
/// <param name="totpId">Identifier of the TOTP that will be used to validate the algorithm against the provided code</param>
/// <param name="code">Code provided by the end-user, to be validated using the algorithm specified by the totpId</param>
/// <returns></returns>
public async Task<LatchResponse> ValidateTOTP(string totpId, string code)
{
var data = new Dictionary<string, string>()
{
{ "code", code }
};
return await HttpPerformRequestAsync(API_TOTP_URL + "/" + UrlEncode(totpId) + "/validate", HttpMethod.POST, data);
}
public async Task<LatchResponse> CreateQSecret(int length, string encoding)
{
var data = new Dictionary<string, string>()
{
{ "length", length.ToString() },
{ "encoding", encoding },
};
return await HttpPerformRequestAsync(API_Q_SECRET_URL, HttpMethod.POST, data);
}
}
#endif
}