-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathPasswordGenerator.cs
61 lines (51 loc) · 2.14 KB
/
PasswordGenerator.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
using System.Linq;
using System.Security.Cryptography;
using System.Threading;
using System.Threading.Tasks;
using Amazon.Lambda.Core;
using Lambdajection.Attributes;
using Lambdajection.CustomResource;
using Microsoft.Extensions.Options;
namespace Lambdajection.Examples.CustomResource
{
[CustomResourceProvider(typeof(Startup))]
public partial class PasswordGenerator
{
private readonly Options options;
private readonly RNGCryptoServiceProvider cryptoServiceProvider;
public PasswordGenerator(
RNGCryptoServiceProvider cryptoServiceProvider,
IOptions<Options> options
)
{
this.options = options.Value;
this.cryptoServiceProvider = cryptoServiceProvider;
}
private string GeneratePassword(uint length)
{
var bytes = new byte[length];
cryptoServiceProvider.GetBytes(bytes);
var chars = bytes.Select(@byte => $"{@byte:X2}");
return string.Join(string.Empty, chars);
}
public Task<Response> Create(CustomResourceRequest<Request> request, CancellationToken cancellationToken = default)
{
cancellationToken.ThrowIfCancellationRequested();
var password = GeneratePassword(request.ResourceProperties?.Length ?? options.DefaultLength);
var response = new Response(password);
return Task.FromResult(response);
}
public Task<Response> Update(CustomResourceRequest<Request> request, CancellationToken cancellationToken = default)
{
cancellationToken.ThrowIfCancellationRequested();
var password = GeneratePassword(request.ResourceProperties?.Length ?? options.DefaultLength);
var response = new Response(password);
return Task.FromResult(response);
}
public Task<Response> Delete(CustomResourceRequest<Request> request, CancellationToken cancellationToken = default)
{
var response = new Response(string.Empty);
return Task.FromResult(response);
}
}
}