Skip to content

Commit

Permalink
Merge pull request #160 from pulcher/add_subscription_tier
Browse files Browse the repository at this point in the history
Add subscription tier
  • Loading branch information
pulcher authored Feb 19, 2024
2 parents bb21980 + 6f152a2 commit c0a664b
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 24 deletions.
31 changes: 12 additions & 19 deletions MrBigHead.Func/GetTwitchUserInfo.cs
Original file line number Diff line number Diff line change
@@ -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
{
Expand Down Expand Up @@ -57,12 +50,12 @@ public async Task<HttpResponseData> 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)
Expand All @@ -79,12 +72,9 @@ public async Task<HttpResponseData> 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");
Expand All @@ -96,11 +86,13 @@ public async Task<HttpResponseData> 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"];

Expand All @@ -109,6 +101,7 @@ public async Task<HttpResponseData> Run([HttpTrigger(AuthorizationLevel.Anonymou
catch (Exception ex)
{
var test = ex.Message;
_logger.LogInformation($"threw tier level execption: {ex.Message}");
}
}

Expand All @@ -118,7 +111,7 @@ public async Task<HttpResponseData> 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);
Expand Down
24 changes: 24 additions & 0 deletions MrBigHead.Shared/TwitchSubscriptionInformation.cs
Original file line number Diff line number Diff line change
@@ -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; }
}
}
22 changes: 17 additions & 5 deletions MrBigHead.Web/Services/UserInformationProvider.cs
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -12,22 +13,33 @@ public class UserInformationProvider(HttpClient http)
{
private readonly HttpClient http = http;
private string AccessToken;
private string UserId;

private UserInformation UserInformation = new();

public async Task<UserInformation> 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<UserInformation>($"https://bigheadfuncs.azurewebsites.net/api/GetTwitchUserInfo?accessToken={AccessToken}");
}
else
{
UserInformation = await http.GetFromJsonAsync<UserInformation>($"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;
}
}
Expand Down
27 changes: 27 additions & 0 deletions MrBigHead.Web/Shared/LoginDisplay.razor
Original file line number Diff line number Diff line change
Expand Up @@ -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<LoginDisplay> logger
@inject NavigationManager Navigation
@inject SignOutSessionStateManager SignOutManager
@inject UserInformationProvider UserInformationProvider

<AuthorizeView>
<Authorized>
Expand All @@ -21,9 +25,32 @@
@code{
[CascadingParameter]
private Task<AuthenticationState>? 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();
}

Expand Down

0 comments on commit c0a664b

Please sign in to comment.