From 91ee4ef4dc4cce072db17ca9f672d6845875c504 Mon Sep 17 00:00:00 2001 From: Nashuim Date: Tue, 27 Aug 2019 09:11:52 +0100 Subject: [PATCH 1/2] Update Twitch to latest API --- .../Provider/TwitchAuthenticatedContext.cs | 4 ++-- .../TwitchAuthenticationHandler.cs | 3 ++- .../TwitchAuthenticationOptions.cs | 14 +++++++------- 3 files changed, 11 insertions(+), 10 deletions(-) diff --git a/src/Owin.Security.Providers.Twitch/Provider/TwitchAuthenticatedContext.cs b/src/Owin.Security.Providers.Twitch/Provider/TwitchAuthenticatedContext.cs index 9a07c8cc..900f6a60 100644 --- a/src/Owin.Security.Providers.Twitch/Provider/TwitchAuthenticatedContext.cs +++ b/src/Owin.Security.Providers.Twitch/Provider/TwitchAuthenticatedContext.cs @@ -22,7 +22,7 @@ public class TwitchAuthenticatedContext : BaseContext public TwitchAuthenticatedContext(IOwinContext context, JObject user, string accessToken) : base(context) { - User = user; + User = (JObject) ((JArray) users.GetValue("data")).First; AccessToken = accessToken; Id = TryGetValue(user, "_id"); @@ -36,7 +36,7 @@ public TwitchAuthenticatedContext(IOwinContext context, JObject user, string acc /// Gets the JSON-serialized user /// /// - /// Contains the Twitch user obtained from the User Info endpoint. By default this is https://api.Twitch.com/user but it can be + /// Contains the Twitch user obtained from the User Info endpoint. By default this is https://api.twitch.tv/helix/users but it can be /// overridden in the options /// public JObject User { get; private set; } diff --git a/src/Owin.Security.Providers.Twitch/TwitchAuthenticationHandler.cs b/src/Owin.Security.Providers.Twitch/TwitchAuthenticationHandler.cs index 69040490..00d59d7b 100644 --- a/src/Owin.Security.Providers.Twitch/TwitchAuthenticationHandler.cs +++ b/src/Owin.Security.Providers.Twitch/TwitchAuthenticationHandler.cs @@ -87,8 +87,9 @@ protected override async Task AuthenticateCoreAsync() var accessToken = (string)response.access_token; // Get the Twitch user - var userRequest = new HttpRequestMessage(HttpMethod.Get, Options.Endpoints.UserInfoEndpoint + "?oauth_token=" + Uri.EscapeDataString(accessToken)); + var userRequest = new HttpRequestMessage(HttpMethod.Get, Options.Endpoints.UserInfoEndpoint); userRequest.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); + userRequest.Headers.Add("Authorization", "Bearer " + accessToken); var userResponse = await _httpClient.SendAsync(userRequest, Request.CallCancelled); userResponse.EnsureSuccessStatusCode(); text = await userResponse.Content.ReadAsStringAsync(); diff --git a/src/Owin.Security.Providers.Twitch/TwitchAuthenticationOptions.cs b/src/Owin.Security.Providers.Twitch/TwitchAuthenticationOptions.cs index 009512a2..b6b8c3cd 100644 --- a/src/Owin.Security.Providers.Twitch/TwitchAuthenticationOptions.cs +++ b/src/Owin.Security.Providers.Twitch/TwitchAuthenticationOptions.cs @@ -14,7 +14,7 @@ public class TwitchAuthenticationEndpoints /// Endpoint which is used to redirect users to request Twitch access /// /// - /// Defaults to https://api.twitch.tv/kraken/oauth2/authorize + /// Defaults to https://id.twitch.tv/oauth2/authorize /// public string AuthorizationEndpoint { get; set; } @@ -22,7 +22,7 @@ public class TwitchAuthenticationEndpoints /// Endpoint which is used to exchange code for access token /// /// - /// Defaults to https://api.twitch.tv/kraken/oauth2/token + /// Defaults to https://id.twitch.tv/oauth2/token /// public string TokenEndpoint { get; set; } @@ -30,14 +30,14 @@ public class TwitchAuthenticationEndpoints /// Endpoint which is used to obtain user information after authentication /// /// - /// Defaults to https://api.twitch.tv/kraken/user + /// Defaults to https://api.twitch.tv/helix/users /// public string UserInfoEndpoint { get; set; } } - private const string AuthorizationEndPoint = "https://api.twitch.tv/kraken/oauth2/authorize"; - private const string TokenEndpoint = "https://api.twitch.tv/kraken/oauth2/token"; - private const string UserInfoEndpoint = "https://api.twitch.tv/kraken/user"; + private const string AuthorizationEndPoint = "https://id.twitch.tv/oauth2/authorize"; + private const string TokenEndpoint = "https://id.twitch.tv/oauth2/token"; + private const string UserInfoEndpoint = "https://api.twitch.tv/helix/users"; /// /// Gets or sets the a pinned certificate validator to use to validate the endpoints used @@ -141,7 +141,7 @@ public TwitchAuthenticationOptions() AuthenticationMode = AuthenticationMode.Passive; Scope = new List { - "user_read" + "user:read:email" }; BackchannelTimeout = TimeSpan.FromSeconds(60); Endpoints = new TwitchAuthenticationEndpoints From f9fdd3017f6d4f057321ae8c041f2e27e246609f Mon Sep 17 00:00:00 2001 From: Nashuim Date: Tue, 27 Aug 2019 09:22:20 +0100 Subject: [PATCH 2/2] Fix incorrect references --- .../Provider/TwitchAuthenticatedContext.cs | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/src/Owin.Security.Providers.Twitch/Provider/TwitchAuthenticatedContext.cs b/src/Owin.Security.Providers.Twitch/Provider/TwitchAuthenticatedContext.cs index 900f6a60..daef9a39 100644 --- a/src/Owin.Security.Providers.Twitch/Provider/TwitchAuthenticatedContext.cs +++ b/src/Owin.Security.Providers.Twitch/Provider/TwitchAuthenticatedContext.cs @@ -17,19 +17,19 @@ public class TwitchAuthenticatedContext : BaseContext /// Initializes a /// /// The OWIN environment - /// The JSON-serialized user + /// The JSON-serialized users /// Twitch Access token - public TwitchAuthenticatedContext(IOwinContext context, JObject user, string accessToken) + public TwitchAuthenticatedContext(IOwinContext context, JObject users, string accessToken) : base(context) { User = (JObject) ((JArray) users.GetValue("data")).First; AccessToken = accessToken; - Id = TryGetValue(user, "_id"); - Name = TryGetValue(user, "name"); - Link = TryGetValue(user, "url"); - UserName = TryGetValue(user, "name"); - Email = TryGetValue(user, "email"); + Id = TryGetValue(User, "_id"); + Name = TryGetValue(User, "name"); + Link = TryGetValue(User, "url"); + UserName = TryGetValue(User, "name"); + Email = TryGetValue(User, "email"); } ///