|
| 1 | +using CaseManagement.BPMN.Domains; |
| 2 | +using CaseManagement.BPMN.Exceptions; |
| 3 | +using CaseManagement.BPMN.ProcessInstance.Processors; |
| 4 | +using IdentityModel.Client; |
| 5 | +using Newtonsoft.Json.Linq; |
| 6 | +using System; |
| 7 | +using System.Collections.Generic; |
| 8 | +using System.Linq; |
| 9 | +using System.Net.Http; |
| 10 | +using System.Threading; |
| 11 | +using System.Threading.Tasks; |
| 12 | + |
| 13 | +namespace CaseManagement.BPMN.Host.Delegates |
| 14 | +{ |
| 15 | + public class GenerateOTPDelegate : IDelegateHandler |
| 16 | + { |
| 17 | + public async Task<ICollection<MessageToken>> Execute(BPMNExecutionContext context, ICollection<MessageToken> incoming, DelegateConfigurationAggregate delegateConfiguration, CancellationToken cancellationToken) |
| 18 | + { |
| 19 | + var user = incoming.FirstOrDefault(i => i.Name == "user"); |
| 20 | + if (user == null) |
| 21 | + { |
| 22 | + throw new BPMNProcessorException("user must be passed in the request"); |
| 23 | + } |
| 24 | + |
| 25 | + var userId = user.GetProperty("userId"); |
| 26 | + if (string.IsNullOrWhiteSpace(userId)) |
| 27 | + { |
| 28 | + throw new BPMNProcessorException("userId is not passed in the request"); |
| 29 | + } |
| 30 | + |
| 31 | + var parameter = GenerateOTPPasswordParameter.Create(delegateConfiguration); |
| 32 | + using (var httpClient = new HttpClient()) |
| 33 | + { |
| 34 | + var tokenResponse = await httpClient.RequestClientCredentialsTokenAsync(new ClientCredentialsTokenRequest |
| 35 | + { |
| 36 | + Address = parameter.TokenUrl, |
| 37 | + ClientId = parameter.ClientId, |
| 38 | + ClientSecret = parameter.ClientSecret, |
| 39 | + Scope = parameter.Scope |
| 40 | + }, cancellationToken); |
| 41 | + if (tokenResponse.IsError) |
| 42 | + { |
| 43 | + throw new BPMNProcessorException(tokenResponse.Error); |
| 44 | + } |
| 45 | + |
| 46 | + var url = parameter.UserUrl.Replace("{id}", userId); |
| 47 | + var request = new HttpRequestMessage |
| 48 | + { |
| 49 | + Method = HttpMethod.Get, |
| 50 | + RequestUri = new Uri(url) |
| 51 | + }; |
| 52 | + request.Headers.Add("Authorization", $"Bearer {tokenResponse.AccessToken}"); |
| 53 | + var httpResponse = await httpClient.SendAsync(request, cancellationToken); |
| 54 | + httpResponse.EnsureSuccessStatusCode(); |
| 55 | + var content = await httpResponse.Content.ReadAsStringAsync(); |
| 56 | + var otp = long.Parse(content); |
| 57 | + ICollection<MessageToken> result = new List<MessageToken> |
| 58 | + { |
| 59 | + MessageToken.NewMessage(context.Pointer.InstanceFlowNodeId, "otp", new JObject |
| 60 | + { |
| 61 | + { "otpCode", otp } |
| 62 | + }.ToString()) |
| 63 | + }; |
| 64 | + return result; |
| 65 | + } |
| 66 | + } |
| 67 | + |
| 68 | + private class GenerateOTPPasswordParameter |
| 69 | + { |
| 70 | + public string TokenUrl { get; set; } |
| 71 | + public string UserUrl { get; set; } |
| 72 | + public string ClientId { get; set; } |
| 73 | + public string ClientSecret { get; set; } |
| 74 | + public string Scope { get; set; } |
| 75 | + |
| 76 | + public static GenerateOTPPasswordParameter Create(DelegateConfigurationAggregate delegateConfiguration) |
| 77 | + { |
| 78 | + return new GenerateOTPPasswordParameter |
| 79 | + { |
| 80 | + ClientId = delegateConfiguration.GetValue("clientId"), |
| 81 | + ClientSecret = delegateConfiguration.GetValue("clientSecret"), |
| 82 | + TokenUrl = delegateConfiguration.GetValue("tokenUrl"), |
| 83 | + UserUrl = delegateConfiguration.GetValue("userUrl"), |
| 84 | + Scope = delegateConfiguration.GetValue("scope") |
| 85 | + }; |
| 86 | + } |
| 87 | + } |
| 88 | + } |
| 89 | +} |
0 commit comments