This commit is contained in:
@@ -38,6 +38,12 @@ namespace Icarus.Controllers
|
||||
#region HTTP endpoints
|
||||
public IActionResult Post([FromBody] User user)
|
||||
{
|
||||
UserStoreContext context = HttpContext
|
||||
.RequestServices
|
||||
.GetService(typeof(UserStoreContext))
|
||||
as UserStoreContext;
|
||||
|
||||
user = context.RetrieveUser(user);
|
||||
Console.WriteLine($"Username: {user.Username}");
|
||||
|
||||
TokenManager tk = new TokenManager(_config);
|
||||
|
||||
@@ -3,6 +3,7 @@ using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Newtonsoft.Json;
|
||||
using RestSharp;
|
||||
|
||||
using Icarus.Models;
|
||||
@@ -13,6 +14,11 @@ namespace Icarus.Controllers.Managers
|
||||
{
|
||||
#region Fields
|
||||
private IConfiguration _config;
|
||||
private string _clientId;
|
||||
private string _clientSecret;
|
||||
private string _audience;
|
||||
private string _grantType;
|
||||
private string _url;
|
||||
#endregion
|
||||
|
||||
|
||||
@@ -32,22 +38,84 @@ namespace Icarus.Controllers.Managers
|
||||
#region Methods
|
||||
public LoginResult RetrieveLoginResult(User user)
|
||||
{
|
||||
// TODO: Request a token from Auth0
|
||||
var client = new RestClient(_url);
|
||||
var request = new RestRequest("oauth/token", Method.POST);
|
||||
var tokenRequest = RetrieveTokenRequest();
|
||||
var tokenObject = JsonConvert.SerializeObject(tokenRequest);
|
||||
request.AddParameter("application/json; charset=utf-8",
|
||||
tokenObject, ParameterType.RequestBody);
|
||||
request.RequestFormat = DataFormat.Json;
|
||||
|
||||
IRestResponse response = client.Execute(request);
|
||||
|
||||
|
||||
var tokenResult = JsonConvert
|
||||
.DeserializeObject<Token>(response.Content);
|
||||
|
||||
return new LoginResult
|
||||
{
|
||||
UserId = 0,
|
||||
UserId = user.Id,
|
||||
Username = user.Username,
|
||||
Token = "gggg",
|
||||
Expiration = 500
|
||||
Token = tokenResult.AccessToken,
|
||||
TokenType = tokenResult.TokenType,
|
||||
Expiration = tokenResult.Expiration
|
||||
};
|
||||
}
|
||||
|
||||
private TokenRequest RetrieveTokenRequest()
|
||||
{
|
||||
return new TokenRequest
|
||||
{
|
||||
ClientId = _clientId,
|
||||
ClientSecret = _clientSecret,
|
||||
Audience = _audience,
|
||||
GrantType = _grantType
|
||||
};
|
||||
}
|
||||
|
||||
private void InitializeValues()
|
||||
{
|
||||
// TODO: implement parse values necessary to
|
||||
// retrieve a token from the appSettings using
|
||||
// the _config object
|
||||
_clientId = _config["Auth0:ClientId"];
|
||||
_clientSecret = _config["Auth0:ClientSecret"];
|
||||
_audience = _config["Auth0:ApiIdentifier"];
|
||||
_grantType = "client_credentials";
|
||||
_url = $"https://{_config["Auth0:Domain"]}";
|
||||
|
||||
//PrintCredentials();
|
||||
}
|
||||
|
||||
// For testing purposes
|
||||
private void PrintCredentials()
|
||||
{
|
||||
Console.WriteLine("Auth0 credentials:");
|
||||
Console.WriteLine($"Client Id: {_clientId}");
|
||||
Console.WriteLine($"Client Secret: {_clientSecret}");
|
||||
Console.WriteLine($"Audience: {_audience}");
|
||||
Console.WriteLine($"Url: {_url}");
|
||||
}
|
||||
#endregion
|
||||
|
||||
|
||||
#region Classes
|
||||
private class TokenRequest
|
||||
{
|
||||
[JsonProperty("client_id")]
|
||||
public string ClientId { get; set; }
|
||||
[JsonProperty("client_secret")]
|
||||
public string ClientSecret { get; set; }
|
||||
[JsonProperty("audience")]
|
||||
public string Audience { get; set; }
|
||||
[JsonProperty("grant_type")]
|
||||
public string GrantType { get; set; }
|
||||
}
|
||||
private class Token
|
||||
{
|
||||
[JsonProperty("access_token")]
|
||||
public string AccessToken { get; set; }
|
||||
[JsonProperty("expires_in")]
|
||||
public int Expiration { get; set; }
|
||||
[JsonProperty("token_type")]
|
||||
public string TokenType { get; set; }
|
||||
}
|
||||
#endregion
|
||||
}
|
||||
|
||||
@@ -4,6 +4,7 @@ using System.Configuration;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
|
||||
@@ -13,7 +14,7 @@ using Icarus.Models.Context;
|
||||
|
||||
namespace Icarus.Controllers
|
||||
{
|
||||
[Route("api/Song")]
|
||||
[Route("api/song")]
|
||||
[ApiController]
|
||||
public class SongController : ControllerBase
|
||||
{
|
||||
@@ -38,28 +39,30 @@ namespace Icarus.Controllers
|
||||
|
||||
|
||||
[HttpGet]
|
||||
[Authorize("read:song_details")]
|
||||
public ActionResult<IEnumerable<Song>> Get()
|
||||
{
|
||||
List<Song> songs = new List<Song>();
|
||||
//songs = _songMgr.RetrieveAllSongDetails().Result;
|
||||
Console.WriteLine("Attemtping to retrieve songs");
|
||||
|
||||
MusicStoreContext context = HttpContext.RequestServices
|
||||
MusicStoreContext context = HttpContext
|
||||
.RequestServices
|
||||
.GetService(typeof(MusicStoreContext))
|
||||
as MusicStoreContext;
|
||||
|
||||
songs = context.GetAllSongs();
|
||||
|
||||
|
||||
return songs;
|
||||
}
|
||||
|
||||
[HttpGet("{id}")]
|
||||
public ActionResult<Song> Get(int id)
|
||||
{
|
||||
MusicStoreContext context = HttpContext.RequestServices
|
||||
MusicStoreContext context = HttpContext
|
||||
.RequestServices
|
||||
.GetService(typeof(MusicStoreContext))
|
||||
as MusicStoreContext;
|
||||
|
||||
Song song = context.GetSong(id);
|
||||
|
||||
return song;
|
||||
@@ -68,7 +71,8 @@ namespace Icarus.Controllers
|
||||
[HttpPost]
|
||||
public void Post([FromBody] Song song)
|
||||
{
|
||||
MusicStoreContext context = HttpContext.RequestServices
|
||||
MusicStoreContext context = HttpContext
|
||||
.RequestServices
|
||||
.GetService(typeof(MusicStoreContext))
|
||||
as MusicStoreContext;
|
||||
|
||||
@@ -83,7 +87,8 @@ namespace Icarus.Controllers
|
||||
[HttpDelete("{id}")]
|
||||
public void Delete(int id)
|
||||
{
|
||||
MusicStoreContext context = HttpContext.RequestServices
|
||||
MusicStoreContext context = HttpContext
|
||||
.RequestServices
|
||||
.GetService(typeof(MusicStoreContext))
|
||||
as MusicStoreContext;
|
||||
|
||||
|
||||
@@ -5,6 +5,7 @@ using System.IO;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
@@ -41,11 +42,12 @@ namespace Icarus.Controllers
|
||||
|
||||
|
||||
[HttpGet("{id}")]
|
||||
//[Route("private-scoped")]
|
||||
//[Authorize("download:songs")]
|
||||
[Route("private-scoped")]
|
||||
[Authorize("download:songs")]
|
||||
public async Task<IActionResult> Get(int id)
|
||||
{
|
||||
MusicStoreContext context = HttpContext.RequestServices
|
||||
MusicStoreContext context = HttpContext
|
||||
.RequestServices
|
||||
.GetService(typeof(MusicStoreContext))
|
||||
as MusicStoreContext;
|
||||
var songMetaData = context.GetSong(id);
|
||||
@@ -56,11 +58,13 @@ namespace Icarus.Controllers
|
||||
}
|
||||
|
||||
[HttpPost]
|
||||
[Authorize("upload:songs")]
|
||||
public async Task Post([FromForm(Name = "file")] List<IFormFile> songData)
|
||||
{
|
||||
try
|
||||
{
|
||||
MusicStoreContext context = HttpContext.RequestServices
|
||||
MusicStoreContext context = HttpContext
|
||||
.RequestServices
|
||||
.GetService(typeof(MusicStoreContext))
|
||||
as MusicStoreContext;
|
||||
|
||||
@@ -90,9 +94,11 @@ namespace Icarus.Controllers
|
||||
}
|
||||
|
||||
[HttpDelete("{id}")]
|
||||
[Authorize("delete:songs")]
|
||||
public void Delete(int id)
|
||||
{
|
||||
MusicStoreContext context = HttpContext.RequestServices
|
||||
MusicStoreContext context = HttpContext
|
||||
.RequestServices
|
||||
.GetService(typeof(MusicStoreContext))
|
||||
as MusicStoreContext;
|
||||
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Globalization;
|
||||
|
||||
using MySql.Data.MySqlClient;
|
||||
|
||||
@@ -81,19 +82,25 @@ namespace Icarus.Models.Context
|
||||
{
|
||||
while (reader.Read())
|
||||
{
|
||||
var dateCreated = reader["DateCreated"].ToString();
|
||||
var lastLogin = reader["LastLogin"].ToString();
|
||||
var parsedC = DateTime.Parse(dateCreated);
|
||||
var parsedL = DateTime.Parse(lastLogin);
|
||||
|
||||
user.Id = Convert.ToInt32(reader["Id"]);
|
||||
user.Nickname = reader["Nickname"].ToString();
|
||||
user.Email = reader["Email"].ToString();
|
||||
user.PhoneNumber = reader["PhoneNumber"].ToString();
|
||||
user.EmailVerified = Boolean.Parse(reader["EmailVerified"].ToString());
|
||||
user.EmailVerified = (reader["EmailVerified"].ToString()) == "1";
|
||||
user.Firstname = reader["Firstname"].ToString();
|
||||
user.Lastname = reader["Lastname"].ToString();
|
||||
user.DateCreated = DateTime.ParseExact(reader["DateCreated"].ToString(), "yyyy-MM-dd HH:mm:ss", System.Globalization.CultureInfo.InvariantCulture);
|
||||
user.LastLogin = DateTime.ParseExact(reader["LastLogin"].ToString(), "yyyy-MM-dd HH:mm:ss", System.Globalization.CultureInfo.InvariantCulture);
|
||||
user.DateCreated = DateTime.Parse(parsedC.ToString("yyyy-MM-dd HH:mm:ss"));
|
||||
user.LastLogin = DateTime.Parse(parsedL.ToString("yyyy-MM-dd HH:mm:ss"));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return user;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
|
||||
@@ -12,6 +12,8 @@ namespace Icarus.Models
|
||||
public string Username { get; set; }
|
||||
[JsonProperty("token")]
|
||||
public string Token { get; set; }
|
||||
[JsonProperty("token_type")]
|
||||
public string TokenType { get; set; }
|
||||
[JsonProperty("expiration")]
|
||||
public int Expiration { get; set; }
|
||||
}
|
||||
|
||||
+18
-2
@@ -55,8 +55,24 @@ namespace Icarus
|
||||
services.AddAuthorization(options =>
|
||||
{
|
||||
options.AddPolicy("download:songs", policy =>
|
||||
policy.Requirements.Add(new HasScopeRequirement
|
||||
("download:songs", domain)));
|
||||
policy
|
||||
.Requirements
|
||||
.Add(new HasScopeRequirement("download:songs", domain)));
|
||||
|
||||
options.AddPolicy("upload:songs", policy =>
|
||||
policy
|
||||
.Requirements
|
||||
.Add(new HasScopeRequirement("upload:songs", domain)));
|
||||
|
||||
options.AddPolicy("delete:songs", policy =>
|
||||
policy
|
||||
.Requirements
|
||||
.Add(new HasScopeRequirement("delete:songs", domain)));
|
||||
|
||||
options.AddPolicy("read:song_details", policy =>
|
||||
policy
|
||||
.Requirements
|
||||
.Add(new HasScopeRequirement("read:song_details", domain)));
|
||||
});
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user