#74: Add UserID to the Song model (#98)

* #74: Add UserID to the Song model

* #74: Added functionality to retrieve user id from the token

* #74: Songs will now contain the User Id when uploading

* Updated Readme and script to add migrations

* #74: Some cleanup
This commit was merged in pull request #98.
This commit is contained in:
Kun Deng
2024-06-19 15:41:55 -04:00
committed by GitHub
parent ebb15b7cdb
commit 562bc87822
25 changed files with 117 additions and 164 deletions
+6 -6
View File
@@ -29,13 +29,13 @@ public class ArtistManager : BaseManager
{
_logger.Info("Starting process to save the artist record of the song to the database");
var artist = new Artist();
var artist = new Artist
{
Name = song.Artist,
SongCount = 1
};
artist.Name = song.Artist;
artist.SongCount = 1;
var artistTitle = artist.Name;
var artistRetrieved = _artistContext.Artists.FirstOrDefault(art => art.Name.Equals(artistTitle));
var artistRetrieved = _artistContext.Artists.FirstOrDefault(art => art.Name.Equals(artist.Name));
if (artistRetrieved == null)
{
+1 -1
View File
@@ -97,7 +97,7 @@ public class CoverArtManager : BaseManager
else
{
_logger.Info("Song has no cover art, applying stock cover art");
// coverArt.ImagePath = _rootCoverArtPath + $"{segment}{defaultExtension}";
var coverArtFilePath = _rootCoverArtPath + $"{segment}{defaultExtension}";
coverArt.ImagePath = DirectoryPaths.CoverArtPath;
metaData.UpdateCoverArt(song, coverArt);
+1 -1
View File
@@ -107,7 +107,7 @@ public class SongManager : BaseManager
try
{
var songPath = songMetaData.SongPath();
System.IO.File.Delete(songPath);
File.Delete(songPath);
successful = true;
DirectoryManager dirMgr = new DirectoryManager(_config, songMetaData);
dirMgr.DeleteEmptyDirectories();
+50 -18
View File
@@ -7,6 +7,7 @@ using Newtonsoft.Json;
using RestSharp;
using Icarus.Models;
using Microsoft.VisualBasic;
namespace Icarus.Controllers.Managers;
@@ -77,11 +78,10 @@ public class TokenManager : BaseManager
public LoginResult LoginSymmetric(User user)
{
var tokenResult = new TokenTierOne();
tokenResult.TokenType = "Jwt";
var tokenResult = new TokenTierOne{ TokenType = "JWT" };
var payload = Payload();
payload.Add(new System.Security.Claims.Claim("user_id", user.UserID.ToString(), ClaimValueTypes.Integer));
payload.Add(new Claim("user_id", user.UserID.ToString(), ClaimValueTypes.Integer));
var tokenHandler = new JwtSecurityTokenHandler();
var key = Encoding.ASCII.GetBytes(_config["JWT:Secret"]);
@@ -98,10 +98,7 @@ public class TokenManager : BaseManager
Audience = _config["Jwt:Audience"]
};
var token = tokenHandler.CreateToken(tokenDescriptor);
tokenResult.AccessToken = tokenHandler.WriteToken(token);
tokenResult.AccessToken = tokenHandler.WriteToken(tokenHandler.CreateToken(tokenDescriptor));
var expClaim = payload.FirstOrDefault(cl =>
{
@@ -120,6 +117,42 @@ public class TokenManager : BaseManager
};
}
public int RetrieveUserIdFromToken(string token)
{
if (this.ContainsBearer(token))
{
token = this.StripBearer(token);
}
var tokenHandler = new JwtSecurityTokenHandler();
var readTok = tokenHandler.ReadJwtToken(token);
var userId = -1;
foreach (var item in readTok.Payload)
{
if (item.Key == "user_id")
{
userId = Convert.ToInt32(item.Value);
break;
}
}
return userId;
}
private string StripBearer(string token)
{
var start = 6;
var strippedToken = token.Substring(start);
return Strings.Trim(strippedToken);
}
private bool ContainsBearer(string token)
{
return token.Contains("Bearer");
}
private string AllScopes()
{
@@ -157,30 +190,28 @@ public class TokenManager : BaseManager
private List<Claim> Payload()
{
// TODO: Remove this hard coding
var expLimit = 30;
var currentDate = DateTime.Now;
var expiredDate = currentDate.AddMinutes(expLimit);
var issued = Math.Floor((currentDate - DateTime.UnixEpoch).TotalSeconds);
var expires = Math.Floor((expiredDate - DateTime.UnixEpoch).TotalSeconds);
var issuer = "https://soaricarus.auth0.com";
issuer = "http://localhost:5002";
var audience = "https://icarus/api";
audience = "http://localhost:5002";
var issuer = "http://localhost:5002";
var audience = "http://localhost:5002";
var subject = _config["JWT:Subject"];
var claim = new List<System.Security.Claims.Claim>()
{
new System.Security.Claims.Claim("scope", AllScopes(), "string"),
new System.Security.Claims.Claim(JwtRegisteredClaimNames.Exp, expiredDate.ToString()),
new System.Security.Claims.Claim(JwtRegisteredClaimNames.Aud, audience),
new System.Security.Claims.Claim(JwtRegisteredClaimNames.Iss, issuer),
new Claim("scope", AllScopes(), "string"),
new Claim(JwtRegisteredClaimNames.Exp, expiredDate.ToString()),
new Claim(JwtRegisteredClaimNames.Aud, audience),
new Claim(JwtRegisteredClaimNames.Iss, issuer),
new Claim(JwtRegisteredClaimNames.Sub, subject),
new System.Security.Claims.Claim(JwtRegisteredClaimNames.Iat, currentDate.ToString())
new Claim(JwtRegisteredClaimNames.Iat, currentDate.ToString())
};
return claim;
}
[Obsolete("Deprecated function")]
private async Task<string> ReadKeyContent(string filepath)
{
return await System.IO.File.ReadAllTextAsync(filepath);
@@ -234,6 +265,7 @@ public class TokenManager : BaseManager
[JsonProperty("grant_type")]
public string GrantType { get; set; }
}
private class TokenTierOne
{
[JsonProperty("access_token")]