-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathSAPTokenCacheContent.cs
78 lines (66 loc) · 3.38 KB
/
SAPTokenCacheContent.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
using System;
using System.Collections.Generic;
using System.IdentityModel.Tokens.Jwt;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Threading.Tasks;
using Microsoft.Extensions.Configuration;
using Simple.OData.Client;
namespace AzureSAPODataReader
{
public class SAPTokenCacheContent : ISAPTokenCacheContent
{
private readonly IConfiguration _Configuration;
public SAPTokenCacheContent(IConfiguration configuration, string userIdentifier, string url)
{
_Configuration = configuration;
this.userIdentifier = userIdentifier;
this.url = url;
}
public string accessToken { get; set; }
public string userIdentifier { get; }
public string url { get; }
public DateTime expiresAt { get; set; }
public bool IsExpired()
{
//var SAPHandler = new JwtSecurityTokenHandler();
//Todo: check SAP token type to read it
//var SAPtoken = SAPHandler.ReadJwtToken(accessToken);
return expiresAt < DateTime.UtcNow;//SAPtoken.ValidTo < now;
}
public ODataClientSettings getODataClientSettingsAsync()
{
var myClientOverride = new HttpClient() { BaseAddress = new Uri(url) };
myClientOverride.DefaultRequestHeaders.Add("Authorization", "Bearer " + accessToken);
//myClientOverride.DefaultRequestHeaders.Add("Authorization", Configuration.GetValue<string>("SAPODataAPI:BasicAuth"));
myClientOverride.DefaultRequestHeaders.Add("Ocp-Apim-Subscription-Key", _Configuration.GetValue<string>("SAPODataAPI:Ocp-Apim-Subscription-Key"));
myClientOverride.DefaultRequestHeaders.Add("Ocp-Apim-Trace", _Configuration.GetValue<string>("SAPODataAPI:Ocp-Apim-Trace"));
var oDataClientSettings = new ODataClientSettings(myClientOverride);
//ignore cookie container so we can set SAP cookies ourselves
//https://github.com/simple-odata-client/Simple.OData.Client/issues/37
//oDataClientSettings.OnApplyClientHandler = handler => handler.UseCookies = false;
/*oDataClientSettings.OnApplyClientHandler = handler =>{
//Remove this line for production
handler.ServerCertificateCustomValidationCallback = (message, certificate2, arg3, arg4) => true;
};*/
oDataClientSettings.OnTrace = (x, y) => Console.WriteLine("TRACE---->" + string.Format(x, y));
oDataClientSettings.BeforeRequest += delegate (HttpRequestMessage message)
{
//preflight x-crf-token fetch, because odata client closes http connection each time it makes a request
if (message.Method != HttpMethod.Get)
{
HttpClient csrfClient = oDataClientSettings.HttpClient;
HttpRequestMessage msg = new HttpRequestMessage(HttpMethod.Head, url + "/");
msg.Headers.Add("x-csrf-token", "Fetch");
HttpResponseMessage responseMessage = csrfClient.SendAsync(msg).Result;
if (responseMessage.IsSuccessStatusCode)
{
message.Headers.Add("x-csrf-token", responseMessage.Headers.GetValues("x-csrf-token").FirstOrDefault());
}
}
};
return oDataClientSettings;
}
}
}