/
AppKeyAndSessionProvider.cs
129 lines (114 loc) · 4.54 KB
/
AppKeyAndSessionProvider.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
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Net;
using System.Runtime.Serialization;
using System.Security.Authentication;
using System.Text;
using System.Threading.Tasks;
namespace Betfair.ESAClient.Auth {
/// <summary>
/// Utility class to provide a session & token via identity SSO
/// </summary>
public class AppKeyAndSessionProvider {
private string _appkey;
private string _host;
private string _password;
private string _username;
private AppKeyAndSession _session;
public const string SSO_HOST_COM = "identitysso.betfair.com";
public const string SSO_HOST_IT = "identitysso.betfair.it";
public const string SSO_HOST_ES = "identitysso.betfair.es";
public AppKeyAndSessionProvider(string ssoHost, string appkey, string username, string password) {
_host = ssoHost;
_appkey = appkey;
_username = username;
_password = password;
Timeout = TimeSpan.FromSeconds(30);
//4hrs is normal expire time
SessionExpireTime = TimeSpan.FromHours(3);
}
/// <summary>
/// AppKey being used
/// </summary>
public string Appkey {
get { return _appkey; }
}
/// <summary>
/// Session expire time (default 3hrs)
/// </summary>
public TimeSpan SessionExpireTime { get; set; }
/// <summary>
/// Specifies the timeout
/// </summary>
public TimeSpan Timeout { get; set; }
/// <summary>
/// Constructs a new session token via identity SSO.
/// Note: These are not cached.
/// </summary>
/// <exception cref="InvalidCredentialException">Thrown if authentication response is fail</exception>
/// <exception cref="IOException">Thrown if authentication call fails</exception>
/// <returns></returns>
public AppKeyAndSession GetOrCreateNewSession() {
if (_session != null) {
//have a cached session - is it expired
if ((_session.CreateTime + SessionExpireTime) > DateTime.UtcNow) {
Trace.TraceInformation("SSO Login - session not expired - re-using");
return _session;
}
else {
Trace.TraceInformation("SSO Login - session expired");
}
}
Trace.TraceInformation("SSO Login host={0}, appkey={1}, username={2}",
_host,
_appkey,
_username);
SessionDetails sessionDetails;
try {
string uri = string.Format("https://{0}/api/login?username={1}&password={2}",
_host,
_username,
_password);
HttpWebRequest loginRequest = (HttpWebRequest) WebRequest.Create(uri);
loginRequest.Headers.Add("X-Application", _appkey);
loginRequest.Accept = "application/json";
loginRequest.Method = "POST";
loginRequest.Timeout = (int) Timeout.TotalMilliseconds;
WebResponse thePage = loginRequest.GetResponse();
using (StreamReader reader = new StreamReader(thePage.GetResponseStream())) {
string response = reader.ReadToEnd();
Trace.TraceInformation("{0}: Response: {1}", _host, response);
sessionDetails = JsonConvert.DeserializeObject<SessionDetails>(response);
}
}
catch (Exception e) {
throw new IOException("SSO Authentication - call failed:", e);
}
//got a response - decode
if (sessionDetails != null && "SUCCESS".Equals(sessionDetails.status)) {
_session = new AppKeyAndSession(_appkey, sessionDetails.token);
}
else {
throw new InvalidCredentialException("SSO Authentication - response is fail: " + sessionDetails.error);
}
return _session;
}
/// <summary>
/// Expires cached token
/// </summary>
public void ExpireTokenNow() {
Trace.TraceInformation("SSO Login - expiring session token now");
_session = null;
}
}
class SessionDetails {
public string token;
public string product;
public string status;
public string error;
}
}