|
| 1 | +using Mixer.Base.Web; |
| 2 | +using Newtonsoft.Json; |
| 3 | +using Newtonsoft.Json.Linq; |
| 4 | +using System; |
| 5 | +using System.Collections.Generic; |
| 6 | +using System.Linq; |
| 7 | +using System.Net; |
| 8 | +using System.Net.Http; |
| 9 | +using System.Threading.Tasks; |
| 10 | + |
| 11 | +namespace Mixer.Base |
| 12 | +{ |
| 13 | + public enum ClientScopeEnum |
| 14 | + { |
| 15 | + achievement__view__self, |
| 16 | + channel__analytics, |
| 17 | + channel__analytics__self, |
| 18 | + channel__costream__self, |
| 19 | + channel__deleteBanner, |
| 20 | + channel__deleteBanner__self, |
| 21 | + channel__details__self, |
| 22 | + channel__follow__self, |
| 23 | + channel__partnership, |
| 24 | + channel__partnership__self, |
| 25 | + channel__streamKey__self, |
| 26 | + channel__update__self, |
| 27 | + chat__bypass_links, |
| 28 | + chat__bypass_slowchat, |
| 29 | + chat__change_ban, |
| 30 | + chat__change_role, |
| 31 | + chat__chat, |
| 32 | + chat__clear_messages, |
| 33 | + chat__connect, |
| 34 | + chat__edit_options, |
| 35 | + chat__giveaway_start, |
| 36 | + chat__poll_start, |
| 37 | + chat__poll_vote, |
| 38 | + chat__purge, |
| 39 | + chat__remove_message, |
| 40 | + chat__timeout, |
| 41 | + chat__view_deleted, |
| 42 | + chat__whisper, |
| 43 | + interactive__manage__self, |
| 44 | + interactive__robot__self, |
| 45 | + invoice__view__self, |
| 46 | + log__view__self, |
| 47 | + notification__update__self, |
| 48 | + notification__view__self, |
| 49 | + recording__manage__self, |
| 50 | + redeemable__create__self, |
| 51 | + redeemable__redeem__self, |
| 52 | + redeemable__view__self, |
| 53 | + resource__find__self, |
| 54 | + subscription__cancel__self, |
| 55 | + subscription__create__self, |
| 56 | + subscription__renew__self, |
| 57 | + subscription__view__self, |
| 58 | + team__administer, |
| 59 | + team__manage__self, |
| 60 | + transaction__cancel__self, |
| 61 | + transaction__view__self, |
| 62 | + type__viewHidden, |
| 63 | + user__analytics__self, |
| 64 | + user__details__self, |
| 65 | + user__getDiscordInvite__self, |
| 66 | + user__log__self, |
| 67 | + user__notification__self, |
| 68 | + user__seen__self, |
| 69 | + user__update__self, |
| 70 | + user__updatePassword__self, |
| 71 | + } |
| 72 | + |
| 73 | + public class ShortCode |
| 74 | + { |
| 75 | + public string code { get; set; } |
| 76 | + public string handle { get; set; } |
| 77 | + public uint expires_in { get; set; } |
| 78 | + } |
| 79 | + |
| 80 | + public class AuthorizationToken |
| 81 | + { |
| 82 | + public static async Task<ShortCode> GenerateShortCode(string clientID, IEnumerable<ClientScopeEnum> scopes) |
| 83 | + { |
| 84 | + if (string.IsNullOrEmpty(clientID)) |
| 85 | + { |
| 86 | + throw new ArgumentException("Client ID must have a valid value"); |
| 87 | + } |
| 88 | + |
| 89 | + if (scopes.Count() == 0) |
| 90 | + { |
| 91 | + throw new ArgumentException("At least one scope must be specified"); |
| 92 | + } |
| 93 | + |
| 94 | + using (HttpClientWrapper client = new HttpClientWrapper()) |
| 95 | + { |
| 96 | + FormUrlEncodedContent content = new FormUrlEncodedContent(new[] |
| 97 | + { |
| 98 | + new KeyValuePair<string, string>("client_id", clientID), |
| 99 | + new KeyValuePair<string, string>("client_secret", null), |
| 100 | + new KeyValuePair<string, string>("scope", AuthorizationToken.ConvertClientScopesToString(scopes)), |
| 101 | + }); |
| 102 | + |
| 103 | + HttpResponseMessage response = await client.PostAsync("oauth/shortcode", content); |
| 104 | + if (response.StatusCode == HttpStatusCode.OK) |
| 105 | + { |
| 106 | + string result = await response.Content.ReadAsStringAsync(); |
| 107 | + return JsonConvert.DeserializeObject<ShortCode>(result); |
| 108 | + } |
| 109 | + else |
| 110 | + { |
| 111 | + throw new HttpRequestException(string.Format("{0} ({1})", (int)response.StatusCode, response.ReasonPhrase)); |
| 112 | + } |
| 113 | + } |
| 114 | + } |
| 115 | + |
| 116 | + public static async Task<string> ValidateShortCode(ShortCode shortCode) |
| 117 | + { |
| 118 | + if (shortCode == null) |
| 119 | + { |
| 120 | + throw new ArgumentException("Short Code is null"); |
| 121 | + } |
| 122 | + |
| 123 | + for (int i = 0; i < shortCode.expires_in; i++) |
| 124 | + { |
| 125 | + using (HttpClientWrapper client = new HttpClientWrapper()) |
| 126 | + { |
| 127 | + HttpResponseMessage response = await client.GetAsync("oauth/shortcode/check/" + shortCode.handle); |
| 128 | + if (response.StatusCode == HttpStatusCode.OK) |
| 129 | + { |
| 130 | + string result = await response.Content.ReadAsStringAsync(); |
| 131 | + JObject jobject = JObject.Parse(result); |
| 132 | + return (string)jobject["code"]; |
| 133 | + } |
| 134 | + } |
| 135 | + await Task.Delay(1000); |
| 136 | + } |
| 137 | + return null; |
| 138 | + } |
| 139 | + |
| 140 | + public static async Task<AuthorizationToken> GetAuthorizationToken(string clientID, string authorizationCode) |
| 141 | + { |
| 142 | + if (string.IsNullOrEmpty(clientID)) |
| 143 | + { |
| 144 | + throw new ArgumentException("Client ID must have a valid value"); |
| 145 | + } |
| 146 | + |
| 147 | + if (string.IsNullOrEmpty(authorizationCode)) |
| 148 | + { |
| 149 | + throw new ArgumentException("Authorization Code must have a valid value"); |
| 150 | + } |
| 151 | + |
| 152 | + using (HttpClientWrapper client = new HttpClientWrapper()) |
| 153 | + { |
| 154 | + FormUrlEncodedContent content = new FormUrlEncodedContent(new[] |
| 155 | + { |
| 156 | + new KeyValuePair<string, string>("grant_type", "authorization_code"), |
| 157 | + new KeyValuePair<string, string>("client_id", clientID), |
| 158 | + new KeyValuePair<string, string>("code", authorizationCode), |
| 159 | + }); |
| 160 | + |
| 161 | + HttpResponseMessage response = await client.PostAsync("oauth/token", content); |
| 162 | + if (response.StatusCode == HttpStatusCode.OK) |
| 163 | + { |
| 164 | + string result = await response.Content.ReadAsStringAsync(); |
| 165 | + JObject jobject = JObject.Parse(result); |
| 166 | + return new AuthorizationToken(clientID, authorizationCode, (string)jobject["access_token"], (string)jobject["refresh_token"], (int)jobject["expires_in"]); |
| 167 | + } |
| 168 | + else |
| 169 | + { |
| 170 | + throw new HttpRequestException(string.Format("{0} ({1})", (int)response.StatusCode, response.ReasonPhrase)); |
| 171 | + } |
| 172 | + } |
| 173 | + } |
| 174 | + |
| 175 | + private static string ConvertClientScopesToString(IEnumerable<ClientScopeEnum> scopes) |
| 176 | + { |
| 177 | + string result = ""; |
| 178 | + |
| 179 | + foreach (ClientScopeEnum scope in scopes) |
| 180 | + { |
| 181 | + string enumName = Enum.GetName(typeof(ClientScopeEnum), scope); |
| 182 | + if (string.IsNullOrEmpty(enumName)) |
| 183 | + { |
| 184 | + throw new ArgumentException("Invalid client scope specified: " + scope); |
| 185 | + } |
| 186 | + enumName = enumName.Replace("__", ":"); |
| 187 | + result += enumName + " "; |
| 188 | + } |
| 189 | + |
| 190 | + if (result.Length > 0) |
| 191 | + { |
| 192 | + result = result.Substring(0, result.Length - 1); |
| 193 | + } |
| 194 | + |
| 195 | + return result; |
| 196 | + } |
| 197 | + |
| 198 | + public DateTimeOffset Expiration { get; private set; } |
| 199 | + |
| 200 | + internal string AccessToken { get; private set; } |
| 201 | + |
| 202 | + private string clientID; |
| 203 | + private string authorizationCode; |
| 204 | + private string refreshToken; |
| 205 | + |
| 206 | + private AuthorizationToken(string clientID, string authorizationCode, string accessToken, string refreshToken, int expiresIn) |
| 207 | + { |
| 208 | + if (string.IsNullOrEmpty(clientID)) |
| 209 | + { |
| 210 | + throw new ArgumentException("Client ID must have a valid value"); |
| 211 | + } |
| 212 | + |
| 213 | + if (string.IsNullOrEmpty(authorizationCode)) |
| 214 | + { |
| 215 | + throw new ArgumentException("Authorization Code must have a valid value"); |
| 216 | + } |
| 217 | + |
| 218 | + if (string.IsNullOrEmpty(accessToken)) |
| 219 | + { |
| 220 | + throw new ArgumentException("Access Token must have a valid value"); |
| 221 | + } |
| 222 | + |
| 223 | + if (string.IsNullOrEmpty(refreshToken)) |
| 224 | + { |
| 225 | + throw new ArgumentException("Refresh Token must have a valid value"); |
| 226 | + } |
| 227 | + |
| 228 | + if (expiresIn <= 0) |
| 229 | + { |
| 230 | + throw new ArgumentException("Expires In must be a valid value"); |
| 231 | + } |
| 232 | + |
| 233 | + this.clientID = clientID; |
| 234 | + this.authorizationCode = authorizationCode; |
| 235 | + this.AccessToken = accessToken; |
| 236 | + this.refreshToken = refreshToken; |
| 237 | + this.Expiration = DateTimeOffset.Now.AddSeconds(expiresIn); |
| 238 | + } |
| 239 | + |
| 240 | + public async Task RefreshToken() |
| 241 | + { |
| 242 | + using (HttpClientWrapper client = new HttpClientWrapper()) |
| 243 | + { |
| 244 | + FormUrlEncodedContent content = new FormUrlEncodedContent(new[] |
| 245 | + { |
| 246 | + new KeyValuePair<string, string>("grant_type", "refresh_token"), |
| 247 | + new KeyValuePair<string, string>("client_id", this.clientID), |
| 248 | + new KeyValuePair<string, string>("refresh_token", this.refreshToken), |
| 249 | + }); |
| 250 | + |
| 251 | + HttpResponseMessage response = await client.PostAsync("oauth/token", content); |
| 252 | + if (response.StatusCode == HttpStatusCode.OK) |
| 253 | + { |
| 254 | + string result = await response.Content.ReadAsStringAsync(); |
| 255 | + JObject jobject = JObject.Parse(result); |
| 256 | + |
| 257 | + this.AccessToken = (string)jobject["access_token"]; |
| 258 | + this.refreshToken = (string)jobject["refresh_token"]; |
| 259 | + this.Expiration = DateTimeOffset.Now.AddSeconds((int)jobject["expires_in"]); |
| 260 | + } |
| 261 | + else |
| 262 | + { |
| 263 | + throw new HttpRequestException(string.Format("{0} ({1})", (int)response.StatusCode, response.ReasonPhrase)); |
| 264 | + } |
| 265 | + } |
| 266 | + } |
| 267 | + } |
| 268 | +} |
0 commit comments