|
7 | 7 | using XboxWebApi.Extensions;
|
8 | 8 | using XboxWebApi.Authentication.Model;
|
9 | 9 | using System.Text;
|
| 10 | +using System.Threading.Tasks; |
10 | 11 |
|
11 | 12 | namespace XboxWebApi.Authentication
|
12 | 13 | {
|
13 |
| - public class AuthenticationService |
14 |
| - { |
15 |
| - public AccessToken AccessToken { get; set; } |
16 |
| - public RefreshToken RefreshToken { get; set; } |
17 |
| - public UserToken UserToken { get; set; } |
18 |
| - public DeviceToken DeviceToken { get; set; } |
19 |
| - public TitleToken TitleToken { get; set; } |
20 |
| - public XToken XToken { get; set; } |
21 |
| - public XboxUserInformation UserInformation { get; set; } |
22 |
| - |
23 |
| - public AuthenticationService() |
24 |
| - { |
25 |
| - } |
26 |
| - |
27 |
| - public AuthenticationService(WindowsLiveResponse wlResponse) |
28 |
| - { |
29 |
| - AccessToken = new AccessToken(wlResponse); |
30 |
| - RefreshToken = new RefreshToken(wlResponse); |
31 |
| - } |
32 |
| - |
33 |
| - public AuthenticationService(AccessToken accessToken, RefreshToken refreshToken) |
34 |
| - { |
35 |
| - AccessToken = accessToken; |
36 |
| - RefreshToken = refreshToken; |
37 |
| - } |
38 |
| - |
39 |
| - public static RestClientEx ClientFactory(string baseUrl, |
40 |
| - JsonNamingStrategy naming = JsonNamingStrategy.Default) |
41 |
| - { |
42 |
| - return new RestClientEx(baseUrl, naming); |
43 |
| - } |
44 |
| - |
45 |
| - public bool Authenticate() |
46 |
| - { |
47 |
| - WindowsLiveResponse windowsLiveTokens = RefreshLiveToken(RefreshToken); |
| 14 | + public class AuthenticationService |
| 15 | + { |
| 16 | + public AccessToken AccessToken { get; set; } |
| 17 | + public RefreshToken RefreshToken { get; set; } |
| 18 | + public UserToken UserToken { get; set; } |
| 19 | + public DeviceToken DeviceToken { get; set; } |
| 20 | + public TitleToken TitleToken { get; set; } |
| 21 | + public XToken XToken { get; set; } |
| 22 | + public XboxUserInformation UserInformation { get; set; } |
| 23 | + |
| 24 | + public AuthenticationService() |
| 25 | + { |
| 26 | + } |
| 27 | + |
| 28 | + public AuthenticationService(WindowsLiveResponse wlResponse) |
| 29 | + { |
| 30 | + AccessToken = new AccessToken(wlResponse); |
| 31 | + RefreshToken = new RefreshToken(wlResponse); |
| 32 | + } |
| 33 | + |
| 34 | + public AuthenticationService(AccessToken accessToken, RefreshToken refreshToken) |
| 35 | + { |
| 36 | + AccessToken = accessToken; |
| 37 | + RefreshToken = refreshToken; |
| 38 | + } |
| 39 | + |
| 40 | + public static RestClientEx ClientFactory(string baseUrl, |
| 41 | + JsonNamingStrategy naming = JsonNamingStrategy.Default) |
| 42 | + { |
| 43 | + return new RestClientEx(baseUrl, naming); |
| 44 | + } |
| 45 | + |
| 46 | + public bool Authenticate() |
| 47 | + { |
| 48 | + return AuthenticateAsync().GetAwaiter().GetResult(); |
| 49 | + } |
| 50 | + |
| 51 | + public async Task<bool> AuthenticateAsync() |
| 52 | + { |
| 53 | + WindowsLiveResponse windowsLiveTokens = await RefreshLiveTokenAsync(RefreshToken); |
48 | 54 | AccessToken = new AccessToken(windowsLiveTokens);
|
49 | 55 | RefreshToken = new RefreshToken(windowsLiveTokens);
|
50 |
| - UserToken = AuthenticateXASU(AccessToken); |
51 |
| - XToken = AuthenticateXSTS(UserToken, DeviceToken, TitleToken); |
52 |
| - UserInformation = XToken.UserInformation; |
53 |
| - return true; |
54 |
| - } |
55 |
| - |
56 |
| - public static WindowsLiveResponse RefreshLiveToken( |
57 |
| - RefreshToken refreshToken) |
58 |
| - { |
59 |
| - RestClientEx client = ClientFactory("https://login.live.com", |
60 |
| - JsonNamingStrategy.SnakeCase); |
61 |
| - RestRequestEx request = new RestRequestEx("oauth20_token.srf", Method.GET); |
62 |
| - NameValueCollection nv = new Model.WindowsLiveRefreshQuery(refreshToken).GetQuery(); |
63 |
| - request.AddQueryParameters(nv); |
64 |
| - IRestResponse<WindowsLiveResponse> response = client.Execute<WindowsLiveResponse>(request); |
65 |
| - return response.Data; |
66 |
| - } |
67 |
| - |
68 |
| - public static UserToken AuthenticateXASU(AccessToken accessToken) |
69 |
| - { |
70 |
| - RestClientEx client = ClientFactory("https://user.auth.xboxlive.com"); |
71 |
| - RestRequestEx request = new RestRequestEx("user/authenticate", Method.POST); |
72 |
| - request.AddHeader("x-xbl-contract-version", "1"); |
73 |
| - request.AddJsonBody(new XASURequest(accessToken)); |
74 |
| - IRestResponse<XASResponse> response = client.Execute<XASResponse>(request); |
75 |
| - return new UserToken(response.Data); |
76 |
| - } |
77 |
| - |
78 |
| - public static DeviceToken AuthenticateXASD(AccessToken accessToken) |
79 |
| - { |
80 |
| - RestClientEx client = ClientFactory("https://device.auth.xboxlive.com"); |
81 |
| - RestRequestEx request = new RestRequestEx("device/authenticate", Method.POST); |
| 56 | + UserToken = await AuthenticateXASUAsync(AccessToken); |
| 57 | + XToken = await AuthenticateXSTSAsync(UserToken, DeviceToken, TitleToken); |
| 58 | + UserInformation = XToken.UserInformation; |
| 59 | + return true; |
| 60 | + } |
| 61 | + |
| 62 | + public static WindowsLiveResponse RefreshLiveToken(RefreshToken token) |
| 63 | + { |
| 64 | + return RefreshLiveTokenAsync(token).GetAwaiter().GetResult(); |
| 65 | + } |
| 66 | + |
| 67 | + public static async Task<WindowsLiveResponse> RefreshLiveTokenAsync( |
| 68 | + RefreshToken refreshToken) |
| 69 | + { |
| 70 | + RestClientEx client = ClientFactory("https://login.live.com", |
| 71 | + JsonNamingStrategy.SnakeCase); |
| 72 | + RestRequestEx request = new RestRequestEx("oauth20_token.srf", Method.GET); |
| 73 | + NameValueCollection nv = new Model.WindowsLiveRefreshQuery(refreshToken).GetQuery(); |
| 74 | + request.AddQueryParameters(nv); |
| 75 | + IRestResponse<WindowsLiveResponse> response = await client.ExecuteTaskAsync<WindowsLiveResponse>(request); |
| 76 | + return response.Data; |
| 77 | + } |
| 78 | + |
| 79 | + public static UserToken AuthenticateXASU(AccessToken accessToken) |
| 80 | + { |
| 81 | + return AuthenticateXASUAsync(accessToken).GetAwaiter().GetResult(); |
| 82 | + } |
| 83 | + |
| 84 | + public static async Task<UserToken> AuthenticateXASUAsync(AccessToken accessToken) |
| 85 | + { |
| 86 | + RestClientEx client = ClientFactory("https://user.auth.xboxlive.com"); |
| 87 | + RestRequestEx request = new RestRequestEx("user/authenticate", Method.POST); |
| 88 | + request.AddHeader("x-xbl-contract-version", "1"); |
| 89 | + request.AddJsonBody(new XASURequest(accessToken)); |
| 90 | + IRestResponse<XASResponse> response = await client.ExecuteTaskAsync<XASResponse>(request); |
| 91 | + return new UserToken(response.Data); |
| 92 | + } |
| 93 | + |
| 94 | + public DeviceToken AuthenticateXASD(AccessToken accessToken) |
| 95 | + { |
| 96 | + return AuthenticateXASDAsync(accessToken).GetAwaiter().GetResult(); |
| 97 | + } |
| 98 | + |
| 99 | + public static async Task<DeviceToken> AuthenticateXASDAsync(AccessToken accessToken) |
| 100 | + { |
| 101 | + RestClientEx client = ClientFactory("https://device.auth.xboxlive.com"); |
| 102 | + RestRequestEx request = new RestRequestEx("device/authenticate", Method.POST); |
82 | 103 | request.AddHeader("x-xbl-contract-version", "1");
|
83 | 104 | request.AddJsonBody(new XASDRequest(accessToken));
|
84 |
| - IRestResponse<XASResponse> response = client.Execute<XASResponse>(request); |
| 105 | + IRestResponse<XASResponse> response = await client.ExecuteTaskAsync<XASResponse>(request); |
85 | 106 | return new DeviceToken(response.Data);
|
86 |
| - } |
87 |
| - |
88 |
| - public static TitleToken AuthenticateXAST(AccessToken accessToken, |
89 |
| - DeviceToken deviceToken) |
90 |
| - { |
91 |
| - RestClientEx client = ClientFactory("https://title.auth.xboxlive.com"); |
| 107 | + } |
| 108 | + |
| 109 | + public static TitleToken AuthenticateXAST(AccessToken accessToken, DeviceToken deviceToken) |
| 110 | + { |
| 111 | + return AuthenticateXASTAsync(accessToken, deviceToken).GetAwaiter().GetResult(); |
| 112 | + } |
| 113 | + |
| 114 | + public static async Task<TitleToken> AuthenticateXASTAsync(AccessToken accessToken, |
| 115 | + DeviceToken deviceToken) |
| 116 | + { |
| 117 | + RestClientEx client = ClientFactory("https://title.auth.xboxlive.com"); |
92 | 118 | RestRequestEx request = new RestRequestEx("title/authenticate", Method.POST);
|
93 | 119 | request.AddHeader("x-xbl-contract-version", "1");
|
94 |
| - request.AddJsonBody(new XASTRequest(accessToken, deviceToken)); |
95 |
| - IRestResponse<XASResponse> response = client.Execute<XASResponse>(request); |
| 120 | + request.AddJsonBody(new XASTRequest(accessToken, deviceToken)); |
| 121 | + IRestResponse<XASResponse> response = await client.ExecuteTaskAsync<XASResponse>(request); |
96 | 122 | return new TitleToken(response.Data);
|
97 |
| - } |
98 |
| - |
99 |
| - public static XToken AuthenticateXSTS(UserToken userToken, |
100 |
| - DeviceToken deviceToken=null, |
101 |
| - TitleToken titleToken=null) |
102 |
| - { |
103 |
| - RestClientEx client = ClientFactory("https://xsts.auth.xboxlive.com"); |
104 |
| - RestRequestEx request = new RestRequestEx("xsts/authorize", Method.POST); |
| 123 | + } |
| 124 | + |
| 125 | + public static XToken AuthenticateXSTS(UserToken userToken, |
| 126 | + DeviceToken deviceToken = null, |
| 127 | + TitleToken titleToken = null) |
| 128 | + { |
| 129 | + return AuthenticateXSTSAsync(userToken, deviceToken, titleToken).GetAwaiter().GetResult(); |
| 130 | + } |
| 131 | + |
| 132 | + public static async Task<XToken> AuthenticateXSTSAsync(UserToken userToken, |
| 133 | + DeviceToken deviceToken = null, |
| 134 | + TitleToken titleToken = null) |
| 135 | + { |
| 136 | + RestClientEx client = ClientFactory("https://xsts.auth.xboxlive.com"); |
| 137 | + RestRequestEx request = new RestRequestEx("xsts/authorize", Method.POST); |
105 | 138 | request.AddHeader("x-xbl-contract-version", "1");
|
106 |
| - request.AddJsonBody(new XSTSRequest(userToken, |
| 139 | + request.AddJsonBody(new XSTSRequest(userToken, |
107 | 140 | deviceToken: deviceToken,
|
108 | 141 | titleToken: titleToken));
|
109 |
| - IRestResponse<XASResponse> response = client.Execute<XASResponse>(request); |
| 142 | + IRestResponse<XASResponse> response = await client.ExecuteTaskAsync<XASResponse>(request); |
110 | 143 | return new XToken(response.Data);
|
111 |
| - } |
112 |
| - |
113 |
| - public static string GetWindowsLiveAuthenticationUrl() |
114 |
| - { |
115 |
| - RestClientEx client = ClientFactory("https://login.live.com"); |
116 |
| - RestRequestEx request = new RestRequestEx("oauth20_authorize.srf", Method.GET); |
117 |
| - NameValueCollection nv = new Model.WindowsLiveAuthenticationQuery().GetQuery(); |
118 |
| - request.AddQueryParameters(nv); |
119 |
| - return client.BuildUri(request).ToString(); |
120 |
| - } |
121 |
| - |
122 |
| - public static WindowsLiveResponse ParseWindowsLiveResponse(string url) |
123 |
| - { |
124 |
| - if (!url.StartsWith("https://login.live.com/oauth20_desktop.srf")) |
125 |
| - { |
126 |
| - throw new InvalidDataException(String.Format("Invalid URL to parse: {0}", url)); |
127 |
| - } |
128 |
| - |
129 |
| - string urlFragment = new Uri(url).Fragment; |
130 |
| - if (String.IsNullOrEmpty(urlFragment) || !urlFragment.StartsWith("#access_token")) |
131 |
| - { |
132 |
| - throw new InvalidDataException(String.Format("Invalid URL fragment: {0}", urlFragment)); |
133 |
| - } |
134 |
| - |
135 |
| - // Cut off leading '#' |
136 |
| - urlFragment = urlFragment.Substring(1); |
137 |
| - |
138 |
| - NameValueCollection queryParams = System.Web.HttpUtility.ParseQueryString( |
139 |
| - urlFragment, System.Text.Encoding.UTF8); |
140 |
| - |
141 |
| - string[] expectedKeys = { |
142 |
| - "expires_in", "access_token", "token_type", |
143 |
| - "scope", "refresh_token", "user_id"}; |
144 |
| - |
145 |
| - foreach (string key in expectedKeys) |
146 |
| - { |
147 |
| - string val = queryParams[key]; |
148 |
| - if (String.IsNullOrEmpty(val)) |
149 |
| - throw new InvalidDataException( |
150 |
| - String.Format("Key not found: {0} || Invalid value: {1}", key, val)); |
151 |
| - } |
152 |
| - |
153 |
| - return new WindowsLiveResponse(queryParams); |
154 |
| - } |
155 |
| - |
156 |
| - public static AuthenticationService LoadFromFile(FileStream fs) |
157 |
| - { |
158 |
| - byte[] buf = new byte[fs.Length]; |
159 |
| - fs.Read(buf, 0, buf.Length); |
160 |
| - string s = Encoding.UTF8.GetString(buf); |
161 |
| - return JsonConvert.DeserializeObject<AuthenticationService>(s); |
162 |
| - } |
163 |
| - |
164 |
| - public void DumpToFile(FileStream fs) |
165 |
| - { |
166 |
| - string s = JsonConvert.SerializeObject(this, Formatting.Indented); |
167 |
| - byte[] bytes = Encoding.UTF8.GetBytes(s); |
168 |
| - fs.Write(bytes, 0, bytes.Length); |
169 |
| - } |
| 144 | + } |
| 145 | + |
| 146 | + public static string GetWindowsLiveAuthenticationUrl() |
| 147 | + { |
| 148 | + RestClientEx client = ClientFactory("https://login.live.com"); |
| 149 | + RestRequestEx request = new RestRequestEx("oauth20_authorize.srf", Method.GET); |
| 150 | + NameValueCollection nv = new Model.WindowsLiveAuthenticationQuery().GetQuery(); |
| 151 | + request.AddQueryParameters(nv); |
| 152 | + return client.BuildUri(request).ToString(); |
| 153 | + } |
| 154 | + |
| 155 | + public static WindowsLiveResponse ParseWindowsLiveResponse(string url) |
| 156 | + { |
| 157 | + if (!url.StartsWith(WindowsLiveConstants.RedirectUrl)) |
| 158 | + { |
| 159 | + throw new InvalidDataException(String.Format("Invalid URL to parse: {0}", url)); |
| 160 | + } |
| 161 | + |
| 162 | + string urlFragment = new Uri(url).Fragment; |
| 163 | + if (String.IsNullOrEmpty(urlFragment) || !urlFragment.StartsWith("#access_token")) |
| 164 | + { |
| 165 | + throw new InvalidDataException(String.Format("Invalid URL fragment: {0}", urlFragment)); |
| 166 | + } |
| 167 | + |
| 168 | + // Cut off leading '#' |
| 169 | + urlFragment = urlFragment.Substring(1); |
| 170 | + |
| 171 | + NameValueCollection queryParams = System.Web.HttpUtility.ParseQueryString( |
| 172 | + urlFragment, System.Text.Encoding.UTF8); |
| 173 | + |
| 174 | + string[] expectedKeys = { |
| 175 | + "expires_in", "access_token", "token_type", |
| 176 | + "scope", "refresh_token", "user_id"}; |
| 177 | + |
| 178 | + foreach (string key in expectedKeys) |
| 179 | + { |
| 180 | + string val = queryParams[key]; |
| 181 | + if (String.IsNullOrEmpty(val)) |
| 182 | + throw new InvalidDataException( |
| 183 | + String.Format("Key not found: {0} || Invalid value: {1}", key, val)); |
| 184 | + } |
| 185 | + |
| 186 | + return new WindowsLiveResponse(queryParams); |
| 187 | + } |
| 188 | + |
| 189 | + public static AuthenticationService LoadFromFile(FileStream fs) |
| 190 | + { |
| 191 | + byte[] buf = new byte[fs.Length]; |
| 192 | + fs.Read(buf, 0, buf.Length); |
| 193 | + string s = Encoding.UTF8.GetString(buf); |
| 194 | + return JsonConvert.DeserializeObject<AuthenticationService>(s); |
| 195 | + } |
| 196 | + |
| 197 | + public void DumpToFile(FileStream fs) |
| 198 | + { |
| 199 | + string s = JsonConvert.SerializeObject(this, Formatting.Indented); |
| 200 | + byte[] bytes = Encoding.UTF8.GetBytes(s); |
| 201 | + fs.Write(bytes, 0, bytes.Length); |
| 202 | + } |
170 | 203 | }
|
171 | 204 | }
|
0 commit comments