Skip to content

Commit

Permalink
feat: user tokens management
Browse files Browse the repository at this point in the history
  • Loading branch information
aguacongas committed Mar 7, 2020
1 parent 443fe61 commit 7d0884d
Show file tree
Hide file tree
Showing 16 changed files with 202 additions and 61 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,14 @@
<input class="form-check-input" type="radio" name="access-token-type"
value="0"
checked="@(Model.AccessTokenType == (int)Models.AccessTokenType.Jwt)"
@onchange="@(() => Model.AccessTokenType = (int)Models.AccessTokenType.Jwt)" />
@onchange="@(() => SetTokenType(Models.AccessTokenType.Jwt))" />
<label class="form-check-label" for="jwt">JWT</label>
</div>
<div class="form-check form-check-inline">
<input class="form-check-input" type="radio" name="access-token-type"
value="1"
checked="@(Model.AccessTokenType == (int)Models.AccessTokenType.Reference)"
@onchange="@(() => Model.AccessTokenType = (int)Models.AccessTokenType.Reference)" />
@onchange="@(() => SetTokenType(Models.AccessTokenType.Reference))" />
<label class="form-check-label" for="reference">Reference</label>
</div>
</Authorized>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Microsoft.AspNetCore.Components;
using Aguacongas.TheIdServer.BlazorApp.Models;
using Microsoft.AspNetCore.Components;
using System;
using System.Collections.Generic;
using Entity = Aguacongas.IdentityServer.Store.Entity;
Expand Down Expand Up @@ -52,5 +53,13 @@ public partial class ClientTokens

[Parameter]
public Entity.Client Model { get; set; }

[Parameter]
public EventCallback<AccessTokenType> TokenTypeChanged { get; set; }

private void SetTokenType(AccessTokenType accessTokenType)
{
TokenTypeChanged.InvokeAsync(accessTokenType);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
using Entity = Aguacongas.IdentityServer.Store.Entity;

namespace Aguacongas.TheIdServer.BlazorApp.Components
{
public class ReferenceToken: TokensGrid<Entity.ReferenceToken>
{
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
using Entity = Aguacongas.IdentityServer.Store.Entity;

namespace Aguacongas.TheIdServer.BlazorApp.Components
{
public class RefreshToken: TokensGrid<Entity.RefreshToken>
{
}
}
63 changes: 63 additions & 0 deletions src/Aguacongas.TheIdServer.BlazorApp/Components/TokensGrid.razor
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
@typeparam T
@inherits UserEntitesGridModel<T>
@inject IJSRuntime _jsRuntime
@inject NavigationManager _navigationManager

<EntitiesGrid Items="@Model" TableClass="table table-hover" Context="token">
<TableHeader>
<th scope="col">
<SortableHeader Property="ClientId"
Text="client id"
GridState="GridState" />
</th>
<th scope="col">
<SortableHeader Property="Data"
Text="data"
GridState="GridState" />
</th>
<th>
<SortableHeader Property="Expiration"
Text="expire at"
GridState="GridState" />
</th>
<th></th>
</TableHeader>
<RowTemplate>
<td>
<a href="@(_navigationManager.BaseUri)/client/@(token.ClientId)">@token.ClientId</a>
</td>
<td>
<button type="button" class="btn btn-secondary" @onclick="() => ShowData(token)">
<span class="oi oi-code"></span>
</button>
</td>
<td>
@token.Expiration
</td>
<td>
<AuthorizeButton CssSubClass="btn-primary" Type="button" Clicked="() => OnDeleteEntityClicked(token)">
<span class="oi oi-trash"></span>
</AuthorizeButton>
</td>
</RowTemplate>
</EntitiesGrid>
<div class="modal fade" id="token-data" tabindex="-1" role="dialog" aria-labelledby="delete-modal-header" aria-hidden="true" data-backdrop="false">
<div class="modal-dialog modal-dialog-centered modal-lg" role="document">
<div class="modal-content">
<div class="modal-header bg-secondary text-white">
<h5 class="modal-title" id="delete-modal-header">Token</h5>
<button type="button" class="close text-white" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">&times;</span>
</button>
</div>
<div class="modal-body">
<div class="row">
<div class="col text-monospace text-body">
<small><code><pre>@_selectedData</pre></code></small>
</div>
</div>
</div>
</div>
</div>
</div>

Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
using Aguacongas.IdentityServer.Store.Entity;
using Microsoft.JSInterop;
using Newtonsoft.Json.Linq;
using Newtonsoft.Json;
using System.Threading.Tasks;

namespace Aguacongas.TheIdServer.BlazorApp.Components
{
public partial class TokensGrid<T> where T: IGrant
{
private readonly JsonSerializerSettings _serializerOptions = new JsonSerializerSettings
{
Formatting = Formatting.Indented,
};
private string _selectedData;

private async Task ShowData(T row)
{
var token = JsonConvert.DeserializeObject<JObject>(row.Data);
_selectedData = JsonConvert.SerializeObject(token, _serializerOptions);
await _jsRuntime.InvokeVoidAsync("bootstrapInteropt.showModal", "token-data");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
</th>
<th>
<SortableHeader Property="Value"
Text="create at"
Text="value"
GridState="GridState" />
</th>
<th></th>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
using Microsoft.AspNetCore.Components;
using System.Collections.Generic;

namespace Aguacongas.TheIdServer.BlazorApp.Components.UserComponents
namespace Aguacongas.TheIdServer.BlazorApp.Components
{
public class UserEntitesGridModel<T> : ComponentBase
{
Expand Down
7 changes: 7 additions & 0 deletions src/Aguacongas.TheIdServer.BlazorApp/Models/TokenData.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace Aguacongas.TheIdServer.BlazorApp.Models
{
public class TokenData
{
public string Data { get; set; }
}
}
2 changes: 1 addition & 1 deletion src/Aguacongas.TheIdServer.BlazorApp/Pages/Client.razor
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ else
</div>
</div>
<div class="card-body">
<ClientTokens Model="@Model" />
<ClientTokens Model="@Model" TokenTypeChanged="OnTokenTypeChanged" />
</div>
</div>
<div id="secrets" class="card mb-3">
Expand Down
6 changes: 6 additions & 0 deletions src/Aguacongas.TheIdServer.BlazorApp/Pages/Client.razor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.NetworkInformation;
using System.Threading.Tasks;
using Entity = Aguacongas.IdentityServer.Store.Entity;

Expand Down Expand Up @@ -154,6 +155,11 @@ private void OnFilterChanged(string term)
StateHasChanged();
}

private void OnTokenTypeChanged(AccessTokenType accessTokenType)
{
Model.AccessTokenType = (int)accessTokenType;
base.OnEntityUpdated(Model.GetType(), Model);
}
private void OnAddUrlClicked()
{
var url = new Entity.ClientUri();
Expand Down
26 changes: 26 additions & 0 deletions src/Aguacongas.TheIdServer.BlazorApp/Pages/User.razor
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,12 @@ else
<li class="list-inline-item">
<a href="@(NavigationManager.Uri)#external-logins-tokens" @onclick='() => ScrollTo("external-logins-tokens")' @onclick:preventDefault>external logins tokens</a>
</li>
<li class="list-inline-item">
<a href="@(NavigationManager.Uri)#reference-tokens" @onclick='() => ScrollTo("reference-tokens")' @onclick:preventDefault>reference tokens</a>
</li>
<li class="list-inline-item">
<a href="@(NavigationManager.Uri)#refresh-tokens" @onclick='() => ScrollTo("refresh-tokens")' @onclick:preventDefault>refresh tokens</a>
</li>
</ul>
</div>

Expand Down Expand Up @@ -200,6 +206,26 @@ else
<UserTokens Model="@Model.Tokens" DeleteEntityClicked="OnDeleteUserTokenClicked" />
</div>
</div>
<div id="reference-tokens" class="card mb-3">
<div class="card-header">
<div class="row">
<h5 class="col">Reference tokens</h5>
</div>
</div>
<div class="card-body">
<ReferenceToken Model="@Model.ReferenceTokens" DeleteEntityClicked="OnDeleteReferenceTokenClicked" />
</div>
</div>
<div id="refresh-tokens" class="card mb-3">
<div class="card-header">
<div class="row">
<h5 class="col">Refresh tokens</h5>
</div>
</div>
<div class="card-body">
<RefreshToken Model="@Model.RefreshTokens" DeleteEntityClicked="OnDeleteRefreshTokenClicked" />
</div>
</div>
</div>
</EditForm>
}
47 changes: 30 additions & 17 deletions src/Aguacongas.TheIdServer.BlazorApp/Pages/User.razor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -81,23 +81,15 @@ protected override void SanetizeEntityToSaved<TEntity>(TEntity entity)
var refreshTokenStore = GetStore<entity.RefreshToken>();
var getRefreshTokenTask = refreshTokenStore.GetAsync(pageRequest);

await Task.WhenAll(getModelTask,
getClaimsTask,
getLoginsTask,
getUserRolesTask,
getUserConsentsTask,
getUserTokensTask,
getReferenceTokenTask).ConfigureAwait(false);

var model = getModelTask.Result;
model.Claims = getClaimsTask.Result.Items.ToList();
model.Logins = getLoginsTask.Result.Items.ToList();
model.Consents = getUserConsentsTask.Result.Items.ToList();
model.Tokens = getUserTokensTask.Result.Items.ToList();
model.ReferenceTokens = getReferenceTokenTask.Result.Items.ToList();
model.RefreshTokens = getRefreshTokenTask.Result.Items.ToList();

var userRoles = getUserRolesTask.Result.Items;
var model = await getModelTask.ConfigureAwait(false);
model.Claims = (await getClaimsTask.ConfigureAwait(false)).Items.ToList();
model.Logins = (await getLoginsTask.ConfigureAwait(false)).Items.ToList();
model.Consents = (await getUserConsentsTask.ConfigureAwait(false)).Items.ToList();
model.Tokens = (await getUserTokensTask.ConfigureAwait(false)).Items.ToList();
model.ReferenceTokens = (await getReferenceTokenTask.ConfigureAwait(false)).Items.ToList();
model.RefreshTokens = (await getRefreshTokenTask.ConfigureAwait(false)).Items.ToList();

var userRoles = (await getUserRolesTask.ConfigureAwait(false)).Items;
if (userRoles.Any())
{
var roleStore = GetStore<entity.Role>();
Expand Down Expand Up @@ -174,6 +166,12 @@ private void OnFilterChanged(string term)
Model.Tokens = State.Tokens.Where(t => t.LoginProvider.Contains(term) || t.Name.Contains(term) || t.Value.Contains(term))
.ToList();

Model.RefreshTokens = State.RefreshTokens.Where(t => t.ClientId.Contains(term) || t.Data.Contains(term))
.ToList();

Model.ReferenceTokens = State.ReferenceTokens.Where(t => t.ClientId.Contains(term) || t.Data.Contains(term))
.ToList();

AddEmptyRole();
}

Expand Down Expand Up @@ -213,6 +211,21 @@ private void OnDeleteUserTokenClicked(entity.UserToken token)
StateHasChanged();
}


private void OnDeleteRefreshTokenClicked(entity.RefreshToken token)
{
Model.RefreshTokens.Remove(token);
EntityDeleted(token);
StateHasChanged();
}

private void OnDeleteReferenceTokenClicked(entity.ReferenceToken token)
{
Model.ReferenceTokens.Remove(token);
EntityDeleted(token);
StateHasChanged();
}

private void OnDeleteUserLoginClicked(entity.UserLogin login)
{
Model.Logins.Remove(login);
Expand Down
9 changes: 5 additions & 4 deletions src/Aguacongas.TheIdServer.BlazorApp/wwwroot/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@
<meta name="viewport" content="width=device-width" />
<title>TheIdServer Admin</title>
<base href="admin" />
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">
<script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js" integrity="sha384-UO2eT0CpHqdSJQ6hJty5KVphtPhzWj9WO1clHTMGa3JDZwrnQq4sF86dIHNDz0W1" crossorigin="anonymous"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js" integrity="sha384-JjSmVgyd0p3pXB1rRibZUAYoIIy6OrQ6VrjIEaFf/nJGzIxFDsf4x0xIM+B07jRM" crossorigin="anonymous"></script>
<link rel="stylesheet" href="//stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">
<script src="//cdnjs.cloudflare.com/ajax/libs/pagedown/1.0/Markdown.Converter.js" ></script>
<script src="//code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js" integrity="sha384-UO2eT0CpHqdSJQ6hJty5KVphtPhzWj9WO1clHTMGa3JDZwrnQq4sF86dIHNDz0W1" crossorigin="anonymous"></script>
<script src="//stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js" integrity="sha384-JjSmVgyd0p3pXB1rRibZUAYoIIy6OrQ6VrjIEaFf/nJGzIxFDsf4x0xIM+B07jRM" crossorigin="anonymous"></script>
<link href="css/site.css" rel="stylesheet" />
</head>
<body>
Expand Down

This file was deleted.

12 changes: 10 additions & 2 deletions src/Aguacongas.TheIdServer.BlazorApp/wwwroot/scripts/interop.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,11 @@
});
$(query).toast('show');
},
showModal: id => {
$(`#${id}`).modal('show');
},
dismissModal: id => {
const query = `#${id}`;
$(query).modal('hide');
$(`#${id}`).modal('hide');
},
showDropDownMenu: id => {
$(`#${id}`).dropdown('show');
Expand All @@ -36,4 +38,10 @@ window.browserInteropt = {
}
};
}
};
window.showDownInteropt = {
convert: (id, data) => {
const converter = new Markdown.Converter();
document.getElementById(id).innerHtml = converter.makeHtml(data);
}
};

0 comments on commit 7d0884d

Please sign in to comment.