Implemented Login workflow
This commit is contained in:
@@ -1,46 +1,57 @@
|
||||
using System.Net.Http.Headers;
|
||||
using System.Security.Authentication;
|
||||
using System.Text;
|
||||
using System.Text.Json.Serialization;
|
||||
using Microsoft.AspNetCore.Components;
|
||||
using SpotifyAPI.Web;
|
||||
using SpotiParty.Web.Models;
|
||||
|
||||
namespace SpotiParty.Web.Services;
|
||||
|
||||
public sealed class AuthorizationHandler {
|
||||
public sealed class AuthorizationHandler(NavigationManager navigator, DatabaseContext context) {
|
||||
|
||||
private AuthResponse? _token;
|
||||
|
||||
private class AuthResponse {
|
||||
[JsonPropertyName("access_token")]
|
||||
public string AccessToken { get; set; }
|
||||
|
||||
[JsonPropertyName("token_type")]
|
||||
public string TokenType { get; set; }
|
||||
|
||||
[JsonPropertyName("expires_in")]
|
||||
public int ExpiresIn { get; set; }
|
||||
private async Task<(string clientId, string clientSecret)> GetClientSecrets() {
|
||||
var fileLines = await File.ReadAllLinesAsync(Path.Combine(Environment.CurrentDirectory, ".dev-token"));
|
||||
return (fileLines[0], fileLines[1]);
|
||||
}
|
||||
|
||||
public async Task<SpotifyClient> ConfigureClient() {
|
||||
if (_token is null) {
|
||||
var fileLines = await File.ReadAllLinesAsync(Path.Combine(Environment.CurrentDirectory, ".dev-token"));
|
||||
var clientId = fileLines[0];
|
||||
var clientSecret = fileLines[1];
|
||||
public async Task<SpotifyClient?> ConfigureClient(Guid userId) {
|
||||
var user = await context.Users.FindAsync(userId);
|
||||
if (user is null) return null;
|
||||
|
||||
var (clientId, clientSecret) = await GetClientSecrets();
|
||||
var request = new AuthorizationCodeRefreshRequest(clientId, clientSecret, user.RefreshToken);
|
||||
var response = await new OAuthClient().RequestToken(request);
|
||||
|
||||
return new SpotifyClient(response.AccessToken);
|
||||
}
|
||||
|
||||
var basicAuthToken = Convert.ToBase64String(Encoding.UTF8.GetBytes($"{clientId}:{clientSecret}"));
|
||||
var httpClient = new HttpClient();
|
||||
httpClient.DefaultRequestHeaders.Authorization = AuthenticationHeaderValue.Parse($"Basic {basicAuthToken}");
|
||||
public async Task<Uri> ConstructLoginUri() {
|
||||
var (clientId, _) = await GetClientSecrets();
|
||||
var request = new LoginRequest(
|
||||
new Uri(navigator.BaseUri + "callback"),
|
||||
clientId,
|
||||
LoginRequest.ResponseType.Code) {
|
||||
Scope = [Scopes.UserReadPlaybackState, Scopes.UserModifyPlaybackState, Scopes.UserReadPrivate, Scopes.UserReadEmail]
|
||||
};
|
||||
|
||||
var response = await httpClient.PostAsync("https://accounts.spotify.com/api/token", new FormUrlEncodedContent(new[] {
|
||||
new KeyValuePair<string, string>("grant_type", "client_credentials")
|
||||
}));
|
||||
response.EnsureSuccessStatusCode();
|
||||
_token = await response.Content.ReadFromJsonAsync<AuthResponse>();
|
||||
return request.ToUri();
|
||||
}
|
||||
|
||||
if (_token is null) throw new AuthenticationException("Spotify auth failed!");
|
||||
}
|
||||
public async Task HandleCallback(string code) {
|
||||
var (clientId, clientSecret) = await GetClientSecrets();
|
||||
var response = await new OAuthClient().RequestToken(
|
||||
new AuthorizationCodeTokenRequest(
|
||||
clientId,
|
||||
clientSecret,
|
||||
code,
|
||||
new Uri(navigator.BaseUri + "callback")));
|
||||
|
||||
return new SpotifyClient(_token.AccessToken, _token.TokenType);
|
||||
var client = new SpotifyClient(response.AccessToken);
|
||||
var spotiUser = await client.UserProfile.Current();
|
||||
var user = new User {
|
||||
DisplayName = spotiUser.DisplayName,
|
||||
RefreshToken = response.RefreshToken
|
||||
};
|
||||
|
||||
await context.Users.AddAsync(user);
|
||||
await context.SaveChangesAsync();
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user