From f8d9c8e4a79715dadf8fa7cc659b51e71dffc122 Mon Sep 17 00:00:00 2001 From: kdeng00 Date: Fri, 24 Dec 2021 16:45:40 -0500 Subject: [PATCH] Authorization functioning --- Authorization/Handlers/HasScopeHandler.cs | 26 ++++++------- Authorization/HasScopeRequirement.cs | 12 +++--- Controllers/Managers/SongManager.cs | 13 +++---- Controllers/Utilities/SongCompression.cs | 2 +- Controllers/v1/SongDataController.cs | 1 + Icarus.csproj | 2 - Models/Song.cs | 5 +++ Startup.cs | 47 +++++++---------------- 8 files changed, 44 insertions(+), 64 deletions(-) diff --git a/Authorization/Handlers/HasScopeHandler.cs b/Authorization/Handlers/HasScopeHandler.cs index 98a018f..8f51065 100644 --- a/Authorization/Handlers/HasScopeHandler.cs +++ b/Authorization/Handlers/HasScopeHandler.cs @@ -11,21 +11,21 @@ namespace Icarus.Authorization.Handlers public class HasScopeHandler : AuthorizationHandler { protected override Task HandleRequirementAsync(AuthorizationHandlerContext context, HasScopeRequirement requirement) - { - if (!context.User.HasClaim(c => c.Type == "scope" && c.Issuer == requirement.Issuer)) - { - return Task.CompletedTask; - } + { + if (!context.User.HasClaim(c => c.Type == "scope" && c.Issuer == requirement.Issuer)) + { + return Task.CompletedTask; + } - var scopes = context.User.FindFirst(c => - c.Type == "scope" && c.Issuer == requirement.Issuer).Value.Split(' '); + var scopes = context.User.FindFirst(c => + c.Type == "scope" && c.Issuer == requirement.Issuer).Value.Split(' '); - if (scopes.Any(s => s == requirement.Scope)) - { - context.Succeed(requirement); - } + if (scopes.Any(s => s == requirement.Scope)) + { + context.Succeed(requirement); + } - return Task.CompletedTask; - } + return Task.CompletedTask; + } } } diff --git a/Authorization/HasScopeRequirement.cs b/Authorization/HasScopeRequirement.cs index 1e91f11..eb2e7b2 100644 --- a/Authorization/HasScopeRequirement.cs +++ b/Authorization/HasScopeRequirement.cs @@ -7,13 +7,13 @@ namespace Icarus.Authorization public class HasScopeRequirement : IAuthorizationRequirement { public string Issuer { get; } - public string Scope { get; } + public string Scope { get; } - public HasScopeRequirement(string scope, string issuer) - { - Scope = scope ?? throw new ArgumentNullException(nameof(scope)); - Issuer = issuer ?? throw new ArgumentNullException(nameof(issuer)); - } + public HasScopeRequirement(string scope, string issuer) + { + Scope = scope ?? throw new ArgumentNullException(nameof(scope)); + Issuer = issuer ?? throw new ArgumentNullException(nameof(issuer)); + } } } diff --git a/Controllers/Managers/SongManager.cs b/Controllers/Managers/SongManager.cs index 88bf6ba..e8f68b0 100644 --- a/Controllers/Managers/SongManager.cs +++ b/Controllers/Managers/SongManager.cs @@ -168,10 +168,7 @@ namespace Icarus.Controllers.Managers - public async Task SaveSongToFileSystem(IFormFile songFile/**, SongRepository songStore, - AlbumRepository albumStore, ArtistRepository artistStore, - GenreRepository genreStore, YearRepository yearStore, - CoverArtRepository coverArtStore*/) + public async Task SaveSongToFileSystem(IFormFile songFile) { try { @@ -208,9 +205,8 @@ namespace Icarus.Controllers.Managers var coverMgr = new CoverArtManager(_config); var coverArt = coverMgr.SaveCoverArt(song); - coverMgr.SaveCoverArtToDatabase(ref song, ref coverArt);//, - SaveSongToDatabase(song);/**, songStore, albumStore, artistStore, genreStore, - yearStore);*/ + coverMgr.SaveCoverArtToDatabase(ref song, ref coverArt); + SaveSongToDatabase(song); } catch (Exception ex) { @@ -242,7 +238,7 @@ namespace Icarus.Controllers.Managers private async Task RetrieveSongFromFileSystem(Song details) { - byte[] uncompressedSong = System.IO.File.ReadAllBytes(details.SongPath); + byte[] uncompressedSong = await System.IO.File.ReadAllBytesAsync(details.SongPath); return new SongData { @@ -265,6 +261,7 @@ namespace Icarus.Controllers.Managers _logger.Info("Assigning song filename"); song.Filename = songFile.FileName; + song.DateCreated = DateTime.Now; return song; } diff --git a/Controllers/Utilities/SongCompression.cs b/Controllers/Utilities/SongCompression.cs index 937a6ba..c93cdc2 100644 --- a/Controllers/Utilities/SongCompression.cs +++ b/Controllers/Utilities/SongCompression.cs @@ -52,7 +52,7 @@ namespace Icarus.Controllers.Utilities var archivePath = RetrieveCompressesSongPath(song); Console.WriteLine($"Compressed song saved to: {archivePath}"); - songData.Data = System.IO.File.ReadAllBytes(archivePath); + songData.Data = await System.IO.File.ReadAllBytesAsync(archivePath); } catch(Exception ex) { diff --git a/Controllers/v1/SongDataController.cs b/Controllers/v1/SongDataController.cs index af7d54a..a5f6f34 100644 --- a/Controllers/v1/SongDataController.cs +++ b/Controllers/v1/SongDataController.cs @@ -61,6 +61,7 @@ namespace Icarus.Controllers.V1 } [HttpPost] + [Route("private-scoped")] [Authorize("upload:songs")] public async Task Post([FromForm(Name = "file")] List songData) { diff --git a/Icarus.csproj b/Icarus.csproj index adfa6c0..f0ea170 100644 --- a/Icarus.csproj +++ b/Icarus.csproj @@ -10,9 +10,7 @@ - - runtime; build; native; contentfiles; analyzers diff --git a/Models/Song.cs b/Models/Song.cs index b6866eb..97b16b1 100644 --- a/Models/Song.cs +++ b/Models/Song.cs @@ -12,6 +12,7 @@ namespace Icarus.Models [JsonProperty("title")] public string Title { get; set; } [JsonProperty("album")] + [Column("Album")] public string AlbumTitle { get; set; } [JsonProperty("artist")] public string Artist { get; set; } @@ -25,6 +26,10 @@ namespace Icarus.Models public string Filename { get; set; } [JsonProperty("song_path")] public string SongPath { get; set; } + [JsonProperty("track")] + public int Track { get; set; } = 0; + [JsonProperty("disc")] + public int Disc { get; set; } = 0; // [JsonIgnore] // public Album Album { get; set; } diff --git a/Startup.cs b/Startup.cs index c83f009..689635c 100644 --- a/Startup.cs +++ b/Startup.cs @@ -3,6 +3,7 @@ using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using System.IdentityModel.Tokens.Jwt; +using System.Security.Claims; using Microsoft.AspNetCore.Authentication.JwtBearer; using Microsoft.AspNetCore.Authorization; @@ -15,6 +16,7 @@ using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Logging; using Microsoft.Extensions.Options; using Microsoft.EntityFrameworkCore; +using Microsoft.IdentityModel.Tokens; using NLog; using NLog.Web; using NLog.Web.AspNetCore; @@ -22,7 +24,6 @@ using NLog.Web.AspNetCore; using Icarus.Authorization; using Icarus.Authorization.Handlers; using Icarus.Database.Contexts; -// using Icarus.Database.Repositories; namespace Icarus { @@ -40,17 +41,17 @@ namespace Icarus { services.AddControllers(); - string domain = $"https://{Configuration["Auth0:Domain"]}/"; + var auth_id = Configuration["Auth0:Domain"]; + var domain = $"https://{auth_id}/"; + var audience = Configuration["Auth0:ApiIdentifier"]; - services.AddAuthentication(options => - { - options.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme; - options.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme; - }).AddJwtBearer(options => - { - options.Authority = domain; - options.Audience = Configuration["Auth0:ApiIdentifier"]; - }); + services + .AddAuthentication(JwtBearerDefaults.AuthenticationScheme) + .AddJwtBearer(options => + { + options.Authority = domain; + options.Audience = audience; + }); services.AddAuthorization(options => { @@ -104,35 +105,12 @@ namespace Icarus var connString = Configuration.GetConnectionString("DefaultConnection"); - /** - services.Add(new ServiceDescriptor(typeof(SongRepository), - new SongRepository(connString))); - - services.Add(new ServiceDescriptor(typeof(AlbumRepository), - new AlbumRepository(connString))); - - services.Add(new ServiceDescriptor(typeof(ArtistRepository), - new ArtistRepository(connString))); - - services.Add(new ServiceDescriptor(typeof(GenreRepository), - new GenreRepository(connString))); - - services.Add(new ServiceDescriptor(typeof(YearRepository), - new YearRepository(connString))); - - services.Add(new ServiceDescriptor(typeof(CoverArtRepository), - new CoverArtRepository(connString))); - - services.Add(new ServiceDescriptor(typeof(UserRepository), - new UserRepository(connString))); - */ services.AddDbContext(options => options.UseMySQL(connString)); services.AddDbContext(options => options.UseMySQL(connString)); services.AddDbContext(options => options.UseMySQL(connString)); services.AddDbContext(options => options.UseMySQL(connString)); services.AddDbContext(options => options.UseMySQL(connString)); - // services.AddDbContext(options => options.UseMySQL(connString)); services.AddDbContext(options => options.UseMySQL(connString)); } @@ -142,6 +120,7 @@ namespace Icarus // NOTE: Dev-related configuration can be done when env.IsDevelopment() evaluated to true app.UseRouting(); + app.UseAuthentication(); app.UseAuthorization(); app.UseEndpoints(endpoints => {