Implemented TokenManager #57
This commit is contained in:
@@ -0,0 +1,3 @@
|
||||
[submodule "Libs/cpr"]
|
||||
path = Libs/cpr
|
||||
url = https://github.com/whoshuu/cpr.git
|
||||
@@ -1,28 +0,0 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Configuration;
|
||||
|
||||
using Icarus.Controllers.Utilities;
|
||||
using Icarus.Models;
|
||||
using Icarus.Database.Contexts;
|
||||
|
||||
namespace Icarus.Controllers.Managers
|
||||
{
|
||||
public class AlbumManager : BaseManager
|
||||
{
|
||||
#region Fields
|
||||
#endregion
|
||||
|
||||
|
||||
#region Properties
|
||||
#endregion
|
||||
|
||||
|
||||
#region Constructors
|
||||
#endregion
|
||||
|
||||
|
||||
#region Methods
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
@@ -1,28 +0,0 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Configuration;
|
||||
|
||||
using Icarus.Controllers.Utilities;
|
||||
using Icarus.Models;
|
||||
using Icarus.Database.Contexts;
|
||||
|
||||
namespace Icarus.Controllers.Managers
|
||||
{
|
||||
public class ArtistManager : BaseManager
|
||||
{
|
||||
#region Fields
|
||||
#endregion
|
||||
|
||||
|
||||
#region Properties
|
||||
#endregion
|
||||
|
||||
|
||||
#region Constructors
|
||||
#endregion
|
||||
|
||||
|
||||
#region Methods
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
@@ -1,28 +0,0 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Configuration;
|
||||
|
||||
using Icarus.Controllers.Utilities;
|
||||
using Icarus.Models;
|
||||
using Icarus.Database.Contexts;
|
||||
|
||||
namespace Icarus.Controllers.Managers
|
||||
{
|
||||
public class GenreManager : BaseManager
|
||||
{
|
||||
#region Fields
|
||||
#endregion
|
||||
|
||||
|
||||
#region Properties
|
||||
#endregion
|
||||
|
||||
|
||||
#region Constructors
|
||||
#endregion
|
||||
|
||||
|
||||
#region Methods
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
@@ -1,6 +1,7 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Runtime.InteropServices;
|
||||
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Newtonsoft.Json;
|
||||
@@ -14,11 +15,6 @@ 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
|
||||
|
||||
|
||||
@@ -30,103 +26,26 @@ namespace Icarus.Controllers.Managers
|
||||
public TokenManager(IConfiguration config)
|
||||
{
|
||||
_config = config;
|
||||
InitializeValues();
|
||||
}
|
||||
#endregion
|
||||
|
||||
|
||||
#region Methods
|
||||
public LoginResult RetrieveLoginResult(User user)
|
||||
#region c++ Libs
|
||||
[DllImport("libicarus.so")]
|
||||
public static extern IntPtr retrieve_token(ref TokenReq req);
|
||||
#endregion
|
||||
|
||||
public static LoginResult ConvertLogResToLoginResult(ref LogRes res)
|
||||
{
|
||||
_logger.Info("Preparing Auth0 API request");
|
||||
|
||||
var client = new RestClient(_url);
|
||||
var request = new RestRequest("oauth/token", Method.POST);
|
||||
var tokenRequest = RetrieveTokenRequest();
|
||||
|
||||
_logger.Info("Serializing token object into JSON");
|
||||
var tokenObject = JsonConvert.SerializeObject(tokenRequest);
|
||||
|
||||
request.AddParameter("application/json; charset=utf-8",
|
||||
tokenObject, ParameterType.RequestBody);
|
||||
|
||||
request.RequestFormat = DataFormat.Json;
|
||||
|
||||
_logger.Info("Sending request");
|
||||
IRestResponse response = client.Execute(request);
|
||||
_logger.Info("Response received");
|
||||
|
||||
|
||||
_logger.Info("Deserializing response");
|
||||
var tokenResult = JsonConvert
|
||||
.DeserializeObject<Token>(response.Content);
|
||||
_logger.Info("Response deserialized");
|
||||
|
||||
return new LoginResult
|
||||
{
|
||||
UserId = user.Id, Username = user.Username, Token = tokenResult.AccessToken,
|
||||
TokenType = tokenResult.TokenType, Expiration = tokenResult.Expiration,
|
||||
Message = "Successfully retrieved token"
|
||||
Token = res.Token,
|
||||
TokenType = res.TokenType,
|
||||
Expiration = res.Expiration,
|
||||
Message = res.Message
|
||||
};
|
||||
}
|
||||
|
||||
private TokenRequest RetrieveTokenRequest()
|
||||
{
|
||||
_logger.Info("Retrieving token object");
|
||||
|
||||
return new TokenRequest
|
||||
{
|
||||
ClientId = _clientId, ClientSecret = _clientSecret,
|
||||
Audience = _audience, GrantType = _grantType
|
||||
};
|
||||
}
|
||||
|
||||
private void InitializeValues()
|
||||
{
|
||||
_logger.Info("Analyzing Auth0 information");
|
||||
|
||||
_clientId = _config["Auth0:ClientId"];
|
||||
_clientSecret = _config["Auth0:ClientSecret"];
|
||||
_audience = _config["Auth0:ApiIdentifier"];
|
||||
_grantType = "client_credentials";
|
||||
_url = $"https://{_config["Auth0:Domain"]}";
|
||||
}
|
||||
|
||||
#region Testing Methods
|
||||
// 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
|
||||
#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
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,28 +0,0 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Configuration;
|
||||
|
||||
using Icarus.Controllers.Utilities;
|
||||
using Icarus.Models;
|
||||
using Icarus.Database.Contexts;
|
||||
|
||||
namespace Icarus.Controllers.Managers
|
||||
{
|
||||
public class YearManager : BaseManager
|
||||
{
|
||||
#region Fields
|
||||
#endregion
|
||||
|
||||
|
||||
#region Properties
|
||||
#endregion
|
||||
|
||||
|
||||
#region Constructors
|
||||
#endregion
|
||||
|
||||
|
||||
#region Methods
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
@@ -3,6 +3,7 @@ using System.Collections.Generic;
|
||||
using System.Configuration;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
using System.Runtime.InteropServices;
|
||||
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
@@ -47,7 +48,6 @@ namespace Icarus.Controllers.V1
|
||||
_logger.LogInformation("Starting process of validating credentials");
|
||||
|
||||
var message = "Invalid credentials";
|
||||
var password = user.Password;
|
||||
|
||||
var loginRes = new LoginResult
|
||||
{
|
||||
@@ -59,7 +59,7 @@ namespace Icarus.Controllers.V1
|
||||
user = context.RetrieveUser(user);
|
||||
|
||||
var validatePass = new PasswordEncryption();
|
||||
var validated = validatePass.VerifyPassword(user, password);
|
||||
var validated = validatePass.VerifyPassword(user, user.Password);
|
||||
if (!validated)
|
||||
{
|
||||
loginRes.Message = message;
|
||||
@@ -70,9 +70,22 @@ namespace Icarus.Controllers.V1
|
||||
|
||||
_logger.LogInformation("Successfully validated user credentials");
|
||||
|
||||
TokenManager tk = new TokenManager(_config);
|
||||
|
||||
loginRes = tk.RetrieveLoginResult(user);
|
||||
var tok = new TokenReq
|
||||
{
|
||||
ClientId = _config["Auth0:ClientId"],
|
||||
ClientSecret = _config["Auth0:ClientSecret"],
|
||||
Audience = _config["Auth0:ApiIdentifier"],
|
||||
GrantType = "client_credentials",
|
||||
URI = $"https://{_config["Auth0:Domain"]}",
|
||||
Endpoint = "oauth/token"
|
||||
};
|
||||
|
||||
IntPtr logRes = TokenManager.retrieve_token(ref tok);
|
||||
LogRes lr = new LogRes();
|
||||
lr = (LogRes)Marshal.PtrToStructure(logRes, typeof(LogRes));
|
||||
loginRes = TokenManager.ConvertLogResToLoginResult(ref lr);
|
||||
loginRes.Username = user.Username;
|
||||
loginRes.UserId = user.Id;
|
||||
|
||||
return Ok(loginRes);
|
||||
}
|
||||
|
||||
+9
-3
@@ -1,6 +1,6 @@
|
||||
cmake_minimum_required(VERSION 3.10)
|
||||
|
||||
project(icarus CXX)
|
||||
project(icarus)
|
||||
|
||||
set(CMAKE_CXX_STANDARD_REQUIRED ON)
|
||||
set(CMAKE_CXX_STANDARD 17)
|
||||
@@ -9,6 +9,7 @@ set(SOURCES
|
||||
src/directory_manager.cpp
|
||||
src/imageFile.cpp
|
||||
src/metadata_retriever.cpp
|
||||
src/token_manager.cpp
|
||||
)
|
||||
set(HEADERS
|
||||
include/models.h
|
||||
@@ -17,6 +18,11 @@ set(HEADERS
|
||||
include/metadata_retriever.h
|
||||
)
|
||||
|
||||
include(${CMAKE_BINARY_DIR}/conanbuildinfo.cmake)
|
||||
conan_basic_setup()
|
||||
|
||||
add_subdirectory(cpr)
|
||||
|
||||
add_library(icarus SHARED ${SOURCES} ${HEADERS})
|
||||
include_directories(include)
|
||||
target_link_libraries(icarus "-lstdc++fs" tag)
|
||||
include_directories(${CPR_INCLUDE_DIRS} include)
|
||||
target_link_libraries(icarus "-lstdc++fs" tag ${CONAN_LIBS} ${CPR_LIBRARIES})
|
||||
|
||||
@@ -0,0 +1,5 @@
|
||||
[requires]
|
||||
jsonformoderncpp/3.7.0@vthiery/stable
|
||||
|
||||
[generators]
|
||||
cmake
|
||||
Submodule
+1
Submodule Libs/cpr added at feebd2fd54
@@ -20,4 +20,24 @@ struct Cover
|
||||
char ImagePath[1024];
|
||||
};
|
||||
|
||||
struct LoginRes
|
||||
{
|
||||
int UserId;
|
||||
char Username[1024];
|
||||
char Token[1024];
|
||||
char TokenType[1024];
|
||||
char Message[1024];
|
||||
int Expiration;
|
||||
};
|
||||
|
||||
struct TokenReq
|
||||
{
|
||||
char ClientId[1024];
|
||||
char ClientSecret[1024];
|
||||
char Audience[1024];
|
||||
char GrantType[1024];
|
||||
char URI[1024];
|
||||
char Endpoint[1024];
|
||||
};
|
||||
|
||||
#endif
|
||||
|
||||
@@ -0,0 +1,41 @@
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
#include <cstdlib>
|
||||
|
||||
#include <cpr/cpr.h>
|
||||
#include <nlohmann/json.hpp>
|
||||
|
||||
#include "models.h"
|
||||
|
||||
|
||||
extern "C"
|
||||
{
|
||||
|
||||
|
||||
LoginRes* retrieve_token(TokenReq *tok)
|
||||
{
|
||||
LoginRes *res = new LoginRes;
|
||||
|
||||
nlohmann::json reqObj;
|
||||
reqObj["client_id"] = tok->ClientId;
|
||||
reqObj["client_secret"] = tok->ClientSecret;
|
||||
reqObj["audience"] = tok->Audience;
|
||||
reqObj["grant_type"] = tok->GrantType;
|
||||
|
||||
std::string uri{tok->URI};
|
||||
uri.append("/");
|
||||
uri.append(tok->Endpoint);
|
||||
|
||||
auto r = cpr::Post(cpr::Url{uri},
|
||||
cpr::Body{reqObj.dump()},
|
||||
cpr::Header{{"Content-Type", "application/json"}});
|
||||
|
||||
auto post_res = nlohmann::json::parse(r.text);
|
||||
strcpy(res->Token, post_res["access_token"].get<std::string>().c_str());
|
||||
strcpy(res->TokenType, post_res["token_type"].get<std::string>().c_str());
|
||||
res->Expiration = post_res["expires_in"].get<int>();
|
||||
strcpy(res->Message, "Success");
|
||||
|
||||
return res;
|
||||
}
|
||||
}
|
||||
+25
-9
@@ -1,4 +1,5 @@
|
||||
using System;
|
||||
using System.Runtime.InteropServices;
|
||||
|
||||
using Newtonsoft.Json;
|
||||
|
||||
@@ -7,14 +8,29 @@ namespace Icarus.Models
|
||||
public class LoginResult : BaseResult
|
||||
{
|
||||
[JsonProperty("id")]
|
||||
public int UserId { get; set; }
|
||||
[JsonProperty("username")]
|
||||
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; }
|
||||
public int UserId { get; set; }
|
||||
[JsonProperty("username")]
|
||||
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; }
|
||||
}
|
||||
|
||||
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi), Serializable]
|
||||
public struct LogRes
|
||||
{
|
||||
public int UserId;
|
||||
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 1024)]
|
||||
public string Username;
|
||||
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 1024)]
|
||||
public string Token;
|
||||
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 1024)]
|
||||
public string TokenType;
|
||||
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 1024)]
|
||||
public string Message;
|
||||
public int Expiration;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,22 @@
|
||||
using System;
|
||||
using System.Runtime.InteropServices;
|
||||
|
||||
namespace Icarus.Models
|
||||
{
|
||||
[StructLayout(LayoutKind.Sequential)]
|
||||
public struct TokenReq
|
||||
{
|
||||
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 1024)]
|
||||
public string ClientId;
|
||||
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 1024)]
|
||||
public string ClientSecret;
|
||||
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 1024)]
|
||||
public string Audience;
|
||||
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 1024)]
|
||||
public string GrantType;
|
||||
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 1024)]
|
||||
public string URI;
|
||||
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 1024)]
|
||||
public string Endpoint;
|
||||
}
|
||||
}
|
||||
+21
-20
@@ -1,4 +1,5 @@
|
||||
using System;
|
||||
using System.Runtime.InteropServices;
|
||||
|
||||
using Newtonsoft.Json;
|
||||
|
||||
@@ -7,26 +8,26 @@ namespace Icarus.Models
|
||||
public class User
|
||||
{
|
||||
[JsonProperty("id")]
|
||||
public int Id { get; set; }
|
||||
[JsonProperty("username")]
|
||||
public string Username { get; set; }
|
||||
[JsonProperty("nickname")]
|
||||
public string Nickname { get; set; }
|
||||
[JsonProperty("password")]
|
||||
public string Password { get; set; }
|
||||
[JsonProperty("email")]
|
||||
public string Email { get; set; }
|
||||
[JsonProperty("phone_number")]
|
||||
public string PhoneNumber { get; set; }
|
||||
[JsonProperty("first_name")]
|
||||
public string Firstname { get; set; }
|
||||
[JsonProperty("last_name")]
|
||||
public string Lastname { get; set; }
|
||||
[JsonProperty("email_verified")]
|
||||
public bool EmailVerified { get; set; }
|
||||
public int Id { get; set; }
|
||||
[JsonProperty("username")]
|
||||
public string Username { get; set; }
|
||||
[JsonProperty("nickname")]
|
||||
public string Nickname { get; set; }
|
||||
[JsonProperty("password")]
|
||||
public string Password { get; set; }
|
||||
[JsonProperty("email")]
|
||||
public string Email { get; set; }
|
||||
[JsonProperty("phone_number")]
|
||||
public string PhoneNumber { get; set; }
|
||||
[JsonProperty("first_name")]
|
||||
public string Firstname { get; set; }
|
||||
[JsonProperty("last_name")]
|
||||
public string Lastname { get; set; }
|
||||
[JsonProperty("email_verified")]
|
||||
public bool EmailVerified { get; set; }
|
||||
[JsonProperty("date_created")]
|
||||
public DateTime DateCreated { get; set; }
|
||||
[JsonProperty("last_login")]
|
||||
public DateTime? LastLogin { get; set; }
|
||||
public DateTime DateCreated { get; set; }
|
||||
[JsonProperty("last_login")]
|
||||
public DateTime? LastLogin { get; set; }
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user