Commit 71814de0 authored by Benedikt Heinrichs's avatar Benedikt Heinrichs
Browse files

Test Controller

parent f2933aea
......@@ -9,6 +9,9 @@ using System.Linq;
using Moq;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Routing;
using Microsoft.AspNetCore.Mvc.Infrastructure;
using Microsoft.AspNetCore.Mvc.Abstractions;
using Microsoft.AspNetCore.Mvc.Controllers;
namespace Coscine.Api.Project.Tests
{
......@@ -54,7 +57,9 @@ namespace Coscine.Api.Project.Tests
var context = new Mock<HttpContext>();
context.SetupGet(x => x.Request).Returns(request.Object);
// _projectController.ControllerContext = new ControllerContext(new ActionContext(context.Object, new RouteData(), _projectController.ControllerContext.));
var actionDescriptor = new Mock<ControllerActionDescriptor>();
_projectController.ControllerContext = new ControllerContext(new ActionContext(context.Object, new RouteData(), actionDescriptor.Object));
var project = new Coscine.Database.Model.Project()
{
......@@ -107,14 +112,13 @@ namespace Coscine.Api.Project.Tests
);
Assert.IsTrue(all.ToArray().Length == 1);
}
/*
[Test]
public void TestController()
{
var actionResult = _projectController.Index();
var task = actionResult.ExecuteResultAsync(_projectController.ControllerContext);
task.Wait();
}*/
Assert.IsTrue(actionResult.GetType() == typeof(OkObjectResult));
}
[Test]
public void CountTest()
......
......@@ -22,7 +22,7 @@ namespace Coscine.Api.Project
_jwtSecurityTokenHandler = new JwtSecurityTokenHandler();
}
public bool ValidToken(string token)
public bool ValidToken(string token, string encryptionAlgorithm = "HS256")
{
string secretKey = _configuration.GetStringAndWait("coscine/global/jwtsecret");
......@@ -34,22 +34,25 @@ namespace Coscine.Api.Project
var securityKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(secretKey));
TokenValidationParameters tokenValidationParameters = new TokenValidationParameters();
tokenValidationParameters.TokenDecryptionKey = securityKey;
tokenValidationParameters.IssuerSigningKey = securityKey;
// TODO: Validate those two
tokenValidationParameters.ValidateAudience = false;
tokenValidationParameters.ValidateIssuer = false;
try
{
_jwtSecurityTokenHandler.ValidateToken(token, tokenValidationParameters, out SecurityToken securityToken);
}
catch(Exception)
catch(Exception e)
{
return false;
}
return true;
}
public IEnumerable<Claim> GetContents(string jwt)
public IEnumerable<Claim> GetContents(string jwt, string encryptionAlgorithm = "HS256")
{
if (ValidToken(jwt))
if (ValidToken(jwt, encryptionAlgorithm))
{
var token = _jwtSecurityTokenHandler.ReadJwtToken(jwt);
return token.Claims;
......@@ -74,7 +77,16 @@ namespace Coscine.Api.Project
var payloadClaims = payloadContents.Select(c => new Claim(c.Key, c.Value));
DateTime centuryBegin = new DateTime(1970, 1, 1);
var exp = new TimeSpan(DateTime.Now.AddYears(1).Ticks - centuryBegin.Ticks).TotalSeconds;
var now = new TimeSpan(DateTime.Now.Ticks - centuryBegin.Ticks).TotalSeconds;
var payload = new JwtPayload(payloadClaims);
payload.Add("iss", "coscine");
payload.Add("aud", "coscine");
payload.Add("iat", (long) now);
payload.Add("exp", (long) exp);
var header = new JwtHeader(signingCredentials);
var securityToken = new JwtSecurityToken(header, payload);
......
......@@ -558,7 +558,7 @@
<Compile Include="DatabaseConnection.cs" />
<Compile Include="Exceptions\InvalidTokenException.cs" />
<Compile Include="Exceptions\NotAuthorizedException.cs" />
<Compile Include="Factories\ProjectFactory.cs" />
<Compile Include="Factories\ObjectFactory.cs" />
<Compile Include="JWTHandler.cs" />
<Compile Include="Models\DatabaseModel.cs" />
<Compile Include="Models\ProjectModel.cs" />
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment