· 6 years ago · Oct 23, 2019, 10:54 AM
1 public class CustomAccessTokenValidator : ICustomTokenRequestValidator
2 {
3 private readonly ICmfSessionKeyClaimProvider _cmfSessionKeyClaimProvider;
4
5 public CustomAccessTokenValidator(ICmfSessionKeyClaimProvider cmfSessionKeyClaimProvider)
6 {
7 _cmfSessionKeyClaimProvider = cmfSessionKeyClaimProvider;
8 }
9
10 public Task ValidateAsync(CustomTokenRequestValidationContext context)
11 {
12 var clientClaims = context.Result.ValidatedRequest.ClientClaims.ToList();
13
14 var cmfSessionClaim = clientClaims.FirstOrDefault(x =>
15 string.Equals(x.Type, Constants.CmfSessionKeyClaimName, StringComparison.OrdinalIgnoreCase));
16
17 if (cmfSessionClaim != null)
18 {
19 var dataAccessApiUri = clientClaims.FirstOrDefault(x =>
20 string.Equals(x.Type, Constants.DataAccessApiClaimName, StringComparison.OrdinalIgnoreCase))
21 ?.Value;
22
23 if (string.IsNullOrEmpty(dataAccessApiUri))
24 throw new Exception("Cannot request cmfsession key because data access api claim does not exist.");
25
26 var cmfSessionKeyClaim = _cmfSessionKeyClaimProvider.GetCmfSessionKey(dataAccessApiUri).GetAwaiter().GetResult();
27
28 clientClaims.Remove(cmfSessionClaim);
29 clientClaims.Add(cmfSessionKeyClaim);
30 }
31
32 return Task.CompletedTask;
33 }
34 }
35
36public class CmfSessionKeyClaimProvider : ICmfSessionKeyClaimProvider
37 {
38 private readonly ICmfAuthorisationService _cmfAuthorisationService;
39
40 public CmfSessionKeyClaimProvider(ICmfAuthorisationService cmfAuthorisationService)
41 {
42 _cmfAuthorisationService = cmfAuthorisationService;
43 }
44
45 public async Task<Claim> GetCmfSessionKey(string dataAccessApiUri)
46 {
47 var sessionKey = await _cmfAuthorisationService.GetCmfSessionKey(dataAccessApiUri);
48
49 return new Claim(Constants.CmfSessionKeyClaimName, sessionKey);
50 }
51 }
52
53 public class CmfAuthorisationService : ICmfAuthorisationService
54 {
55 private readonly IHttpFactory _httpFactory;
56 private readonly ILogger<CmfAuthorisationService> _logger;
57
58 public CmfAuthorisationService(IHttpFactory httpFactory, ILogger<CmfAuthorisationService> logger)
59 {
60 _httpFactory = httpFactory;
61 _logger = logger;
62 }
63
64 public async Task<string> GetCmfSessionKey(string dataAccessUri)
65 {
66 var httpClient = _httpFactory.MakeHttpClient();
67
68 httpClient.BaseAddress = new Uri(dataAccessUri);
69
70 var requestEndpoint =
71 $"{Constants.CmfSessionKeyEndPoint}/{Constants.CloudApiUser}/{Constants.CloudApiPassword}";
72
73 _logger.LogInformation($"GetCmfSessionKey => Attempting to retrieve CMF Session key @ {dataAccessUri}/{requestEndpoint}");
74
75 var response = await httpClient.GetAsync(requestEndpoint);
76
77 _logger.LogInformation($"GetCmfSessionKey => HTTP Response {response.StatusCode}");
78
79 if (!response.IsSuccessStatusCode)
80 throw new Exception($"An error has occured trying to retrieve CMFSessionKey @ {dataAccessUri}/{requestEndpoint}, httpStatus code: {response.StatusCode}");
81
82 var sessionKeyResponse = await response.Content.ReadAsStringAsync();
83
84 if(string.IsNullOrEmpty(sessionKeyResponse))
85 throw new Exception("CMFSessionKey Response is null. Validate CMF User credentials are valid and that the user is not suspended.");
86
87 return sessionKeyResponse;
88 }
89 }
90}
91
92ublic class HttpFactory : IHttpFactory
93 {
94 private readonly string _proxyAddress;
95
96 public HttpFactory(string proxyAddress)
97 {
98 _proxyAddress = proxyAddress;
99 }
100
101 public HttpClient MakeHttpClient()
102 {
103 var handler = new HttpClientHandler
104 {
105 Proxy = new WebProxy(_proxyAddress)
106 };
107
108 var httpClient = new HttpClient(handler);
109
110 return httpClient;
111 }
112 }