diff --git a/MrBigHead.Func/GetTwitchUserInfo.cs b/MrBigHead.Func/GetTwitchUserInfo.cs index 3d7f96f..250e5b2 100644 --- a/MrBigHead.Func/GetTwitchUserInfo.cs +++ b/MrBigHead.Func/GetTwitchUserInfo.cs @@ -1,21 +1,14 @@ -using System.ComponentModel.DataAnnotations; -using System.Net; -using Azure.Core; -using System.Net.Http.Headers; using Azure.Identity; using Azure.Security.KeyVault.Secrets; using Microsoft.Azure.Functions.Worker; using Microsoft.Azure.Functions.Worker.Http; +using Microsoft.Extensions.Configuration; using Microsoft.Extensions.Logging; -using Microsoft.VisualBasic; using MrBigHead.Shared; -using static System.Net.WebRequestMethods; -using System.Net.Http.Json; -using Microsoft.Extensions.Configuration; -using System.Text.Json.Nodes; -using System.Text.Json.Serialization; +using System.Net; +using System.Net.Http.Headers; using System.Text.Json; -using System; +using System.Text.Json.Nodes; namespace MrBigHead.Func { @@ -57,12 +50,12 @@ public async Task Run([HttpTrigger(AuthorizationLevel.Anonymou try { secretCliendId = secretClient.GetSecret(secretName); - _logger.LogInformation("Got a secret"); + _logger.LogInformation($"Got a secret: {secretCliendId}"); clientId = secretCliendId.Value; // my broadcasterId - secretBroadcasterId = secretClient.GetSecret(secretName); - _logger.LogInformation("Got a broadcasterId"); + secretBroadcasterId = secretClient.GetSecret(secretBroadcasterName); + _logger.LogInformation($"Got a broadcasterId: {secretBroadcasterId}"); broadcasterId = secretBroadcasterId.Value; } catch (Exception ex) @@ -79,12 +72,9 @@ public async Task Run([HttpTrigger(AuthorizationLevel.Anonymou using (HttpClient client = new()) { - var testUserId = req.Query["userId"]; - client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", req.Query["accessToken"]); client.DefaultRequestHeaders.Add("Client-Id", clientId); - try { var responseString = await client.GetStringAsync("https://api.twitch.tv/helix/users"); @@ -96,11 +86,13 @@ public async Task Run([HttpTrigger(AuthorizationLevel.Anonymou catch (Exception ex) { var test = ex.Message; + _logger.LogInformation($"threw users execption: {ex.Message}"); }; + _logger.LogInformation("going for sub level..."); try { - var responseString = await client.GetStringAsync($"https://api.twitch.tv/helix/subscriptions/user?broadcaster_id={broadcasterId}&user_id={loggedInUserId}"); + var responseString = await client.GetStringAsync($"https://api.twitch.tv/helix/subscriptions/user?broadcaster_id={broadcasterId}&user_id={twitchUserResponse?.Id}"); var parsedResponse = JsonObject.Parse(responseString); var responseData = parsedResponse["data"]; @@ -109,6 +101,7 @@ public async Task Run([HttpTrigger(AuthorizationLevel.Anonymou catch (Exception ex) { var test = ex.Message; + _logger.LogInformation($"threw tier level execption: {ex.Message}"); } } @@ -118,7 +111,7 @@ public async Task Run([HttpTrigger(AuthorizationLevel.Anonymou DisplayName = twitchUserResponse?.DisplayName, Email = twitchUserResponse?.Email, ImageUrl = twitchUserResponse?.ProfileImageUrl, - Tier = twitchSubscriptionResponse.Tier, + Tier = twitchSubscriptionResponse?.Tier, }; var response = req.CreateResponse(HttpStatusCode.OK); diff --git a/MrBigHead.Shared/TwitchSubscriptionInformation.cs b/MrBigHead.Shared/TwitchSubscriptionInformation.cs new file mode 100644 index 0000000..9d1df88 --- /dev/null +++ b/MrBigHead.Shared/TwitchSubscriptionInformation.cs @@ -0,0 +1,24 @@ +using System.Text.Json.Serialization; + +namespace MrBigHead.Shared +{ + public class TwitchSubscriptionInformation + { + [JsonPropertyName("broadcaster_id ")] + public string BroadcasterId { get; set; } + [JsonPropertyName("broadcaster_login")] + public string BroadcasterLogin { get; set; } + [JsonPropertyName("broadcaster_name")] + public string BroadcasterName { get; set; } + [JsonPropertyName("gifter_id")] + public string GifterId { get; set; } + [JsonPropertyName("gifter_login")] + public string GifterLogin { get; set; } + [JsonPropertyName("gifter_name")] + public string GifterName { get; set; } + [JsonPropertyName("is_gift")] + public bool IsGift { get; set; } + [JsonPropertyName("tier")] + public string Tier { get; set; } + } +} diff --git a/MrBigHead.Web/Services/UserInformationProvider.cs b/MrBigHead.Web/Services/UserInformationProvider.cs index dc01fb1..05ac66f 100644 --- a/MrBigHead.Web/Services/UserInformationProvider.cs +++ b/MrBigHead.Web/Services/UserInformationProvider.cs @@ -1,5 +1,6 @@ using Microsoft.Extensions.Configuration.UserSecrets; using MrBigHead.Shared; +using System; using System.Linq; using System.Net.Http; using System.Net.Http.Json; @@ -12,7 +13,6 @@ public class UserInformationProvider(HttpClient http) { private readonly HttpClient http = http; private string AccessToken; - private string UserId; private UserInformation UserInformation = new(); @@ -20,14 +20,26 @@ public async Task GetUserInformation(ClaimsPrincipal principal) { if (principal == null) return new UserInformation(); - AccessToken = principal.Claims.FirstOrDefault(c => c.Type == "idp_access_token").Value; - UserId = principal.Claims.FirstOrDefault(c => c.Type == "id").Value; + await Console.Out.WriteLineAsync("Principle is not null"); + AccessToken = principal?.Claims?.FirstOrDefault(c => c.Type == "idp_access_token")?.Value; + await Console.Out.WriteLineAsync($"AccessToken: {AccessToken}|"); + await Console.Out.WriteLineAsync($"principal: {principal}, Claims: {principal?.Claims}"); + await Console.Out.WriteLineAsync("claims:"); + foreach (var claim in principal.Claims) + { + await Console.Out.WriteLineAsync($"claim: {claim.Type} Value: {claim.Value}"); + } - if (string.IsNullOrEmpty(UserInformation.UserName)) + if (string.IsNullOrEmpty(UserInformation?.UserName)) + { + UserInformation = await http.GetFromJsonAsync($"https://bigheadfuncs.azurewebsites.net/api/GetTwitchUserInfo?accessToken={AccessToken}"); + } + else { - UserInformation = await http.GetFromJsonAsync($"https://bigheadfuncs.azurewebsites.net/api/GetTwitchUserInfo?accessToken={AccessToken}?userId={UserId}"); + await Console.Out.WriteLineAsync($"no username or other information"); } + await Console.Out.WriteLineAsync($"Image: {UserInformation.ImageUrl}"); return UserInformation; } } diff --git a/MrBigHead.Web/Shared/LoginDisplay.razor b/MrBigHead.Web/Shared/LoginDisplay.razor index 6ff9629..5aaa7a5 100644 --- a/MrBigHead.Web/Shared/LoginDisplay.razor +++ b/MrBigHead.Web/Shared/LoginDisplay.razor @@ -2,10 +2,14 @@ @using Microsoft.AspNetCore.Components.WebAssembly.Authentication @using Microsoft.AspNetCore.Components.WebAssembly.Hosting @using Microsoft.Extensions.Logging +@using System.Security.Claims +@using MrBigHead.Shared +@using MrBigHead.Web.Services @inject ILogger logger @inject NavigationManager Navigation @inject SignOutSessionStateManager SignOutManager +@inject UserInformationProvider UserInformationProvider @@ -21,9 +25,32 @@ @code{ [CascadingParameter] private Task? authenticationState { get; set; } + private ClaimsPrincipal User { get; set; } + private UserInformation userInformation { get; set; } protected override async Task OnInitializedAsync() { + + var state = await authenticationState; + User = state.User; + + foreach (var claim in User.Claims) + { + await Console.Out.WriteLineAsync($"claim: {claim.Type} Value: {claim.Value}"); + } + + userInformation = await UserInformationProvider.GetUserInformation(User); + + if (userInformation is not null) + { + Console.WriteLine($"username: {userInformation.UserName}"); + Console.WriteLine($"image: {userInformation.ImageUrl}"); + } + else + { + Console.WriteLine("null userinformation"); + } + await base.OnInitializedAsync(); }