Skip to content

Commit

Permalink
#218 - Base work for client authentication.
Browse files Browse the repository at this point in the history
  • Loading branch information
maraf committed Mar 5, 2019
1 parent adf7cd2 commit cb50c5a
Show file tree
Hide file tree
Showing 8 changed files with 72 additions and 16 deletions.
8 changes: 5 additions & 3 deletions src/Money.Api/Domain/Controllers/ApiController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using Neptuo.Commands;
using Neptuo.Formatters;
using Neptuo.Queries;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.IO;
Expand All @@ -18,6 +19,7 @@
namespace Money.Controllers
{
[Authorize]
[ApiController]
[Route("[controller]/[action]")]
public class ApiController : Controller
{
Expand Down Expand Up @@ -56,13 +58,13 @@ public ActionResult Query([FromBody] Request request)

[HttpPost]
[Route("{*url}")]
public ActionResult Query(string url, [FromBody] string payload)
public ActionResult Query(string url, JObject rawQuery)
{
Ensure.NotNullOrEmpty(url, "url");
Ensure.NotNullOrEmpty(payload, "payload");
Ensure.NotNull(rawQuery, "rawQuery");

Type type = queryMapper.FindTypeByUrl(url);
return Query(payload, type);
return Query(rawQuery.ToString(), type);
}

private ActionResult Query(string payload, Type type)
Expand Down
17 changes: 15 additions & 2 deletions src/Money.UI.Blazor/Layouts/Layout.cshtml
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
@inherits BlazorLayoutComponent
@using Neptuo.Exceptions.Handlers
@inherits BlazorLayoutComponent
@inject ExceptionHandlerBuilder ExceptionHandlerBuilder
@inject Navigator Navigator

<nav class="navbar navbar-inverse navbar-fixed-top">
<div class="container">
Expand All @@ -13,4 +16,14 @@
<footer>
<p>&copy; 2018 - Neptuo</p>
</footer>
</div>
</div>

@functions
{
protected override void OnInit()
{
base.OnInit();

ExceptionHandlerBuilder.Handler<UnauthorizedAccessException>(e => Navigator.OpenLogin());
}
}
1 change: 1 addition & 0 deletions src/Money.UI.Blazor/Pages/Account/Login.cshtml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
@page "/account/login"
45 changes: 41 additions & 4 deletions src/Money.UI.Blazor/Services/ApiClient.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
using Microsoft.AspNetCore.Blazor;
using Money.Models.Api;
using Neptuo;
using Neptuo.Exceptions.Handlers;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Text;
using System.Threading.Tasks;

Expand All @@ -15,27 +18,43 @@ public class ApiClient
private readonly HttpClient http;
private readonly CommandMapper commandMapper;
private readonly QueryMapper queryMapper;
private readonly IExceptionHandler exceptionHandler;

public ApiClient(HttpClient http, CommandMapper commandMapper, QueryMapper queryMapper)
public ApiClient(HttpClient http, CommandMapper commandMapper, QueryMapper queryMapper, IExceptionHandler exceptionHandler)
{
Ensure.NotNull(http, "http");
Ensure.NotNull(commandMapper, "commandMapper");
Ensure.NotNull(queryMapper, "queryMapper");
Ensure.NotNull(exceptionHandler, "exceptionHandler");
this.http = http;
this.commandMapper = commandMapper;
this.queryMapper = queryMapper;
this.exceptionHandler = exceptionHandler;
http.BaseAddress = new Uri("http://localhost:63803");
//http.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJodHRwOi8vc2NoZW1hcy54bWxzb2FwLm9yZy93cy8yMDA1LzA1L2lkZW50aXR5L2NsYWltcy9uYW1lIjoiZGVtbyIsImh0dHA6Ly9zY2hlbWFzLnhtbHNvYXAub3JnL3dzLzIwMDUvMDUvaWRlbnRpdHkvY2xhaW1zL25hbWVpZGVudGlmaWVyIjoiMjhmNGQxNzYtNjg5ZS00ZDRkLTlhMzgtYTg3MGQ5NzFhZDc5IiwiZXhwIjoxNTUyNzI2NDU2LCJpc3MiOiJodHRwczovL2xvY2FsaG9zdCIsImF1ZCI6Imh0dHBzOi8vbG9jYWxob3N0In0.4tSJlngLynld3Ul_HuicpO4zUERjYZ4FFjTrJxfE8Po");
}

private Request CreateRequest(Type type, string payload)
=> new Request() { Type = type.AssemblyQualifiedName, Payload = payload };

public Task<Response> QueryAsync(Type type, string payload)
public async Task<Response> QueryAsync(Type type, string payload)
{
string url = queryMapper.FindUrlByType(type);
if (url != null)
return http.PostJsonAsync<Response>($"/api/query/{url}", payload);
{
HttpResponseMessage response = await http.PostAsync($"/api/query/{url}", new StringContent(payload, Encoding.UTF8, "text/json"));
if (response.StatusCode == HttpStatusCode.Unauthorized)
{
UnauthorizedAccessException exception = new UnauthorizedAccessException();
exceptionHandler.Handle(exception);
throw exception;
}

string responseContent = await response.Content.ReadAsStringAsync();
return SimpleJson.SimpleJson.DeserializeObject<JsResponse>(responseContent);
}
else
return http.PostJsonAsync<Response>($"/api/query", CreateRequest(type, payload));
return await http.PostJsonAsync<Response>($"/api/query", CreateRequest(type, payload));
}

public Task CommandAsync(Type type, string payload)
Expand All @@ -46,5 +65,23 @@ public Task CommandAsync(Type type, string payload)
else
return http.PostJsonAsync($"/api/command", CreateRequest(type, payload));
}

public class JsResponse : Response
{
public string payload
{
set => Payload = value;
}

public string type
{
set => Type = value;
}

public ResponseType responseType
{
set => ResponseType = value;
}
}
}
}
3 changes: 3 additions & 0 deletions src/Money.UI.Blazor/Services/Navigator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -65,5 +65,8 @@ public void OpenUserManage()

public void OpenUserPassword()
=> uri.NavigateTo(UrlUserPassword());

public void OpenLogin()
=> uri.NavigateTo("/account/login");
}
}
2 changes: 1 addition & 1 deletion src/Money.UI.Blazor/wwwroot/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,6 @@ <h2>Neptuo</h2>
<script src="lib/bootstrap/dist/js/bootstrap.min.js"></script>
<script src="lib/signalr/signalr.min.js"></script>
<script src="_framework/blazor.webassembly.js"></script>
<script src="js/site.min.js"></script>
<script src="js/site.js"></script>
</body>
</html>
10 changes: 5 additions & 5 deletions src/Money.UI.Blazor/wwwroot/js/site.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ window.Money = {
};

var connection = new signalR.HubConnectionBuilder()
.withUrl("/api")
.build();;
.withUrl("http://localhost:63803/api", { accessTokenFactory: function () { return "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJodHRwOi8vc2NoZW1hcy54bWxzb2FwLm9yZy93cy8yMDA1LzA1L2lkZW50aXR5L2NsYWltcy9uYW1lIjoiZGVtbyIsImh0dHA6Ly9zY2hlbWFzLnhtbHNvYXAub3JnL3dzLzIwMDUvMDUvaWRlbnRpdHkvY2xhaW1zL25hbWVpZGVudGlmaWVyIjoiMjhmNGQxNzYtNjg5ZS00ZDRkLTlhMzgtYTg3MGQ5NzFhZDc5IiwiZXhwIjoxNTUyNzI2NDU2LCJpc3MiOiJodHRwczovL2xvY2FsaG9zdCIsImF1ZCI6Imh0dHBzOi8vbG9jYWxob3N0In0.4tSJlngLynld3Ul_HuicpO4zUERjYZ4FFjTrJxfE8Po" }})
.build();

connection.on("RaiseEvent", function (e) {
console.log("JS: Event: " + e);
Expand All @@ -54,9 +54,9 @@ connection.onclose(function () {
alert('Underlaying connection to the server has closed. Reloading the page...');
}

setTimeout(function () {
window.location.reload();
}, 2000);
//setTimeout(function () {
// window.location.reload();
//}, 2000);
});
connection.start().then(function () {
isStarted = true;
Expand Down
2 changes: 1 addition & 1 deletion src/Money.UI.Blazor/wwwroot/js/site.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit cb50c5a

Please sign in to comment.