Skip to content

Commit

Permalink
Ticket #704 : In the website, convert datetime UTC to local datetime
Browse files Browse the repository at this point in the history
  • Loading branch information
thabart committed Feb 27, 2024
1 parent 93a61d2 commit 0466613
Show file tree
Hide file tree
Showing 27 changed files with 144 additions and 75 deletions.
48 changes: 48 additions & 0 deletions src/IdServer/SimpleIdServer.IdServer.Website/SidJsonSerializer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
// Copyright (c) SimpleIdServer. All rights reserved.
// Licensed under the Apache License, Version 2.0. See LICENSE in the project root for license information.

using SimpleIdServer.IdServer.Domains;
using SimpleIdServer.IdServer.DTOs;
using System.Text.Json;

namespace SimpleIdServer.IdServer.Website;

public class SidJsonSerializer
{
public static T Deserialize<T>(string json) where T : class
{
var serializeOptions = new JsonSerializerOptions
{
Converters =
{
new UtcDateTimeConverter()
}
};
var searchResult = JsonSerializer.Deserialize<T>(json, serializeOptions);
return searchResult;
}

public static SearchResult<Client> DeserializeSearchClients(string json)
{
var result = Deserialize<SearchResult<Client>>(json);
foreach(var client in result.Content)
{
client.CreateDateTime = client.CreateDateTime.ToLocalTime();
client.UpdateDateTime = client.UpdateDateTime.ToLocalTime();
}

return result;
}

public static IEnumerable<Client> DeserializeClients(string json)
{
var result = Deserialize<IEnumerable<Client>>(json);
foreach (var client in result)
{
client.CreateDateTime = client.CreateDateTime.ToLocalTime();
client.UpdateDateTime = client.UpdateDateTime.ToLocalTime();
}

return result;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,10 @@ public class AcrEffects
private readonly IdServerWebsiteOptions _options;
private readonly ProtectedSessionStorage _sessionStorage;

public AcrEffects(IWebsiteHttpClientFactory websiteHttpClientFactory, IOptions<IdServerWebsiteOptions> options, ProtectedSessionStorage sessionStorage)
public AcrEffects(
IWebsiteHttpClientFactory websiteHttpClientFactory,
IOptions<IdServerWebsiteOptions> options,
ProtectedSessionStorage sessionStorage)
{
_websiteHttpClientFactory = websiteHttpClientFactory;
_options = options.Value;
Expand All @@ -36,7 +39,7 @@ public async Task Handle(GetAllAcrsAction action, IDispatcher dispatcher)
};
var httpResult = await httpClient.SendAsync(requestMessage);
var json = await httpResult.Content.ReadAsStringAsync();
var acrs = JsonSerializer.Deserialize<IEnumerable<AuthenticationContextClassReference>>(json);
var acrs = SidJsonSerializer.Deserialize<IEnumerable<AuthenticationContextClassReference>>(json);
dispatcher.Dispatch(new GetAllAcrsSuccessAction { Acrs = acrs });
}

Expand Down Expand Up @@ -64,7 +67,7 @@ public async Task Handle(AddAcrAction action, IDispatcher dispatcher)
try
{
httpResult.EnsureSuccessStatusCode();
var newAcr = JsonSerializer.Deserialize<AuthenticationContextClassReference>(json);
var newAcr = SidJsonSerializer.Deserialize<AuthenticationContextClassReference>(json);
dispatcher.Dispatch(new AddAcrSuccessAction { Acr = newAcr });
}
catch
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public async Task Handle(SearchApiResourcesAction action, IDispatcher dispatcher
};
var httpResult = await httpClient.SendAsync(requestMessage);
var json = await httpResult.Content.ReadAsStringAsync();
var searchResult = JsonSerializer.Deserialize<SearchResult<ApiResource>>(json);
var searchResult = SidJsonSerializer.Deserialize<SearchResult<ApiResource>>(json);
var selectedResources = new List<string>();
if(!string.IsNullOrWhiteSpace(action.ScopeName))
selectedResources = searchResult.Content.Where(c => c.Scopes.Any(s => s.Name == action.ScopeName)).Select(r => r.Name).ToList();
Expand Down Expand Up @@ -79,7 +79,7 @@ public async Task Handle(AddApiResourceAction action, IDispatcher dispatcher)
try
{
httpResult.EnsureSuccessStatusCode();
var newApiResource = JsonSerializer.Deserialize<Domains.ApiResource>(json);
var newApiResource = SidJsonSerializer.Deserialize<Domains.ApiResource>(json);
dispatcher.Dispatch(new AddApiResourceSuccessAction { Id = newApiResource.Id, Name = action.Name, Description = action.Description, Audience = action.Audience });
}
catch
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public static SearchApiResourcesState ReduceSearchApiResourcesSuccessAction(Sear
public static SearchApiResourcesState ReduceAddApiResourceSuccessAction(SearchApiResourcesState state, AddApiResourceSuccessAction act)
{
var apiResources = state.ApiResources.ToList();
var newApiResource = new ApiResource { Id = act.Id, CreateDateTime = DateTime.UtcNow, UpdateDateTime = DateTime.UtcNow, Name = act.Name, Description = act.Description, Audience = act.Audience };
var newApiResource = new ApiResource { Id = act.Id, CreateDateTime = DateTime.Now, UpdateDateTime = DateTime.Now, Name = act.Name, Description = act.Description, Audience = act.Audience };
apiResources.Add(new SelectableApiResource(newApiResource) { IsNew = true });
return state with
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public async Task Handle(SearchAuditingRecordsAction action, IDispatcher dispatc
};
var httpResult = await httpClient.SendAsync(requestMessage);
var json = await httpResult.Content.ReadAsStringAsync();
var searchResult = JsonSerializer.Deserialize<SearchResult<AuditEvent>>(json);
var searchResult = SidJsonSerializer.Deserialize<SearchResult<AuditEvent>>(json);
dispatcher.Dispatch(new SearchAuditingRecordsSuccessAction { AuditEvents = searchResult.Content, Count = searchResult.Count });

string SanitizeExpression(string expression) => expression.Replace("Value.", "");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public async Task Handle(GetAllAuthMethodAction action, IDispatcher dispatcher)
};
var httpResult = await httpClient.SendAsync(requestMessage);
var json = await httpResult.Content.ReadAsStringAsync();
var result = JsonSerializer.Deserialize<IEnumerable<AuthenticationMethodResult>>(json);
var result = SidJsonSerializer.Deserialize<IEnumerable<AuthenticationMethodResult>>(json);
dispatcher.Dispatch(new GetAllAuthMethodSuccessAction { AuthMethods = result });
}

Expand All @@ -54,7 +54,7 @@ public async Task Handle(GetAuthMethodAction action, IDispatcher dispatcher)
try
{
httpResult.EnsureSuccessStatusCode();
var result = JsonSerializer.Deserialize<AuthenticationMethodResult>(json);
var result = SidJsonSerializer.Deserialize<AuthenticationMethodResult>(json);
dispatcher.Dispatch(new GetAuthMethodSuccessAction { AuthMethod = result });
}
catch
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public async Task Handle(SearchCertificateAuthoritiesAction action, IDispatcher
};
var httpResult = await httpClient.SendAsync(requestMessage);
var json = await httpResult.Content.ReadAsStringAsync();
var searchResult = JsonSerializer.Deserialize<SearchResult<CertificateAuthority>>(json);
var searchResult = SidJsonSerializer.Deserialize<SearchResult<CertificateAuthority>>(json);
dispatcher.Dispatch(new SearchCertificateAuthoritiesSuccessAction { CertificateAuthorities = searchResult.Content, Count = searchResult.Count });

string SanitizeExpression(string expression) => expression.Replace("Value.", "");
Expand All @@ -72,7 +72,7 @@ public async Task Handle(GenerateCertificateAuthorityAction action, IDispatcher
try
{
httpResult.EnsureSuccessStatusCode();
var certificateAuthority = JsonSerializer.Deserialize<CertificateAuthority>(json);
var certificateAuthority = SidJsonSerializer.Deserialize<CertificateAuthority>(json);
dispatcher.Dispatch(new GenerateCertificateAuthoritySuccessAction { CertificateAuthority = certificateAuthority });
}
catch
Expand Down Expand Up @@ -104,7 +104,7 @@ public async Task Handle(ImportCertificateAuthorityAction action, IDispatcher di
try
{
httpResult.EnsureSuccessStatusCode();
var certificateAuthority = JsonSerializer.Deserialize<CertificateAuthority>(json);
var certificateAuthority = SidJsonSerializer.Deserialize<CertificateAuthority>(json);
dispatcher.Dispatch(new GenerateCertificateAuthoritySuccessAction { CertificateAuthority = certificateAuthority });
}
catch
Expand All @@ -130,7 +130,7 @@ public async Task Handle(SaveCertificateAuthorityAction action, IDispatcher disp
try
{
httpResult.EnsureSuccessStatusCode();
var certificateAuthority = JsonSerializer.Deserialize<CertificateAuthority>(json);
var certificateAuthority = SidJsonSerializer.Deserialize<CertificateAuthority>(json);
dispatcher.Dispatch(new SaveCertificateAuthoritySuccessAction { CertificateAuthority = certificateAuthority });
}
catch
Expand Down Expand Up @@ -170,7 +170,7 @@ public async Task Handle(GetCertificateAuthorityAction action, IDispatcher dispa
};
var httpResult = await httpClient.SendAsync(requestMessage);
var json = await httpResult.Content.ReadAsStringAsync();
var certificateAuthority = JsonSerializer.Deserialize<CertificateAuthority>(json);
var certificateAuthority = SidJsonSerializer.Deserialize<CertificateAuthority>(json);
var store = new IdServer.Stores.CertificateAuthorityStore(null);
var certificate = store.Get(certificateAuthority);
dispatcher.Dispatch(new GetCertificateAuthoritySuccessAction { CertificateAuthority = certificateAuthority, Certificate = certificate });
Expand Down Expand Up @@ -214,7 +214,7 @@ public async Task Handle(AddClientCertificateAction action, IDispatcher dispatch
try
{
httpResult.EnsureSuccessStatusCode();
var clientCertificate = JsonSerializer.Deserialize<ClientCertificate>(json);
var clientCertificate = SidJsonSerializer.Deserialize<ClientCertificate>(json);
dispatcher.Dispatch(new AddClientCertificateSuccessAction { CertificateAuthorityId = action.CertificateAuthorityId, ClientCertificate = clientCertificate });
}
catch
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
using SimpleIdServer.IdServer.Api.Token.Handlers;
using SimpleIdServer.IdServer.Builders;
using SimpleIdServer.IdServer.Domains;
using SimpleIdServer.IdServer.DTOs;
using SimpleIdServer.IdServer.Saml.Idp.Extensions;
using SimpleIdServer.IdServer.Store;
using SimpleIdServer.IdServer.WsFederation;
Expand Down Expand Up @@ -52,10 +51,9 @@ public async Task Handle(SearchClientsAction action, IDispatcher dispatcher)
}), Encoding.UTF8, "application/json")
};
var httpResult = await httpClient.SendAsync(requestMessage);
var json = await httpResult.Content.ReadAsStringAsync();
var searchResult = JsonSerializer.Deserialize<SearchResult<Domains.Client>>(json);
var json = await httpResult.Content.ReadAsStringAsync();
var searchResult = SidJsonSerializer.DeserializeSearchClients(json);
dispatcher.Dispatch(new SearchClientsSuccessAction { Clients = searchResult.Content, Count = searchResult.Count });

string SanitizeExpression(string expression) => expression.Replace("Value.", "");
}

Expand All @@ -71,7 +69,7 @@ public async Task Handle(GetAllClientsAction action, IDispatcher dispatcher)
};
var httpResult = await httpClient.SendAsync(requestMessage);
var json = await httpResult.Content.ReadAsStringAsync();
var clients = JsonSerializer.Deserialize<IEnumerable<Domains.Client>>(json);
var clients = SidJsonSerializer.DeserializeClients(json);
dispatcher.Dispatch(new SearchClientsSuccessAction { Clients = clients, Count = clients.Count() });
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public static SearchClientsState ReduceAddClientSuccessAction(SearchClientsState
{
var clients = state.Clients?.ToList();
if (clients == null) return state;
var newClient = new Domains.Client { ClientId = act.ClientId, CreateDateTime = DateTime.UtcNow, UpdateDateTime = DateTime.UtcNow, ClientType = act.ClientType };
var newClient = new Domains.Client { ClientId = act.ClientId, CreateDateTime = DateTime.Now, UpdateDateTime = DateTime.Now, ClientType = act.ClientType };
if(!string.IsNullOrWhiteSpace(act.ClientName))
newClient.Translations.Add(new Translation
{
Expand Down Expand Up @@ -279,7 +279,7 @@ public static ClientState ReduceUpdateClientCredentialsSuccessAction(ClientState
client.TlsClientAuthSanIP = act.TlsClientAuthSanIP;
}

client.UpdateDateTime = DateTime.UtcNow;
client.UpdateDateTime = DateTime.Now;
return state with
{
Client = client
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public async Task Handle(GetAllConfigurationDefinitionsAction action, IDispatche
};
var httpResult = await httpClient.SendAsync(requestMessage);
var json = await httpResult.Content.ReadAsStringAsync();
var content = JsonSerializer.Deserialize<IEnumerable<ConfigurationDefResult>>(json);
var content = SidJsonSerializer.Deserialize<IEnumerable<ConfigurationDefResult>>(json);
dispatcher.Dispatch(new GetAllConfigurationDefinitionsSuccessAction { Content = content });
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public async Task<GetGroupResult> Get(string id)
};
var httpResult = await httpClient.SendAsync(requestMessage);
var json = await httpResult.Content.ReadAsStringAsync();
var getResult = JsonSerializer.Deserialize<GetGroupResult>(json);
var getResult = SidJsonSerializer.Deserialize<GetGroupResult>(json);
return getResult;
}

Expand All @@ -67,7 +67,7 @@ public async Task Handle(SearchGroupsAction action, IDispatcher dispatcher)
};
var httpResult = await httpClient.SendAsync(requestMessage);
var json = await httpResult.Content.ReadAsStringAsync();
var searchResult = JsonSerializer.Deserialize<SearchResult<Domains.Group>>(json);
var searchResult = SidJsonSerializer.Deserialize<SearchResult<Domains.Group>>(json);
dispatcher.Dispatch(new SearchGroupsSuccessAction { Groups = searchResult.Content, Count = searchResult.Count });

string SanitizeExpression(string expression) => expression.Replace("Group.", "").Replace("Value", "");
Expand Down Expand Up @@ -159,7 +159,7 @@ public async Task Handle(AddGroupRolesAction action, IDispatcher dispatcher)
};
var httpResult = await httpClient.SendAsync(requestMessage);
var json = await httpResult.Content.ReadAsStringAsync();
roles.Add(JsonSerializer.Deserialize<Domains.Scope>(json));
roles.Add(SidJsonSerializer.Deserialize<Domains.Scope>(json));
}

dispatcher.Dispatch(new AddGroupRolesSuccessAction { Roles = roles });
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public static SearchGroupsState ReduceAddGroupSuccessAction(SearchGroupsState st
{
if (!string.IsNullOrWhiteSpace(act.ParentGroupId)) return state;
var groups = state.Groups.ToList();
groups.Add(new SelectableGroup(new Group { CreateDateTime = DateTime.UtcNow, UpdateDateTime = DateTime.UtcNow, Id = act.Id, Name = act.Name, Description = act.Description })
groups.Add(new SelectableGroup(new Group { CreateDateTime = DateTime.Now, UpdateDateTime = DateTime.Now, Id = act.Id, Name = act.Name, Description = act.Description })
{
IsNew = true
});
Expand Down Expand Up @@ -242,7 +242,7 @@ public static GroupMembersState ReduceAddGroupSuccessAction(GroupMembersState st
{
if (string.IsNullOrWhiteSpace(action.ParentGroupId)) return state;
var members = state.Members.ToList();
members.Add(new SelectableGroupMember(new Group { Id = action.Id, Name = action.Name, ParentGroupId = action.ParentGroupId, Description = action.Description, FullPath = action.FullPath, CreateDateTime = DateTime.UtcNow, UpdateDateTime = DateTime.UtcNow })
members.Add(new SelectableGroupMember(new Group { Id = action.Id, Name = action.Name, ParentGroupId = action.ParentGroupId, Description = action.Description, FullPath = action.FullPath, CreateDateTime = DateTime.Now, UpdateDateTime = DateTime.Now })
{
IsNew = true
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public async Task Handle(SearchIdProvidersAction action, IDispatcher dispatcher)
};
var httpResult = await httpClient.SendAsync(requestMessage);
var json = await httpResult.Content.ReadAsStringAsync();
var result = JsonSerializer.Deserialize<DTOs.SearchResult<AuthenticationSchemeProviderResult>>(json);
var result = SidJsonSerializer.Deserialize<DTOs.SearchResult<AuthenticationSchemeProviderResult>>(json);
dispatcher.Dispatch(new SearchIdProvidersSuccessAction { IdProviders = result.Content, Count = result.Count });

string SanitizeExpression(string expression) => expression?.Replace("Value.", "");
Expand Down Expand Up @@ -81,7 +81,7 @@ public async Task Handle(GetIdProviderAction action, IDispatcher dispatcher)
};
var httpResult = await httpClient.SendAsync(requestMessage);
var json = await httpResult.Content.ReadAsStringAsync();
var result = JsonSerializer.Deserialize<AuthenticationSchemeProviderResult>(json);
var result = SidJsonSerializer.Deserialize<AuthenticationSchemeProviderResult>(json);
dispatcher.Dispatch(new GetIdProviderSuccessAction { IdProvider = result });
}

Expand Down Expand Up @@ -179,7 +179,7 @@ public async Task Handle(AddAuthenticationSchemeProviderMapperAction action, IDi
};
var httpResult = await httpClient.SendAsync(requestMessage);
var json = await httpResult.Content.ReadAsStringAsync();
var newMapper = JsonSerializer.Deserialize<AuthenticationSchemeProviderMapperResult>(json);
var newMapper = SidJsonSerializer.Deserialize<AuthenticationSchemeProviderMapperResult>(json);
dispatcher.Dispatch(new AddAuthenticationSchemeProviderMapperSuccessAction
{
Id = newMapper.Id,
Expand Down Expand Up @@ -256,7 +256,7 @@ public async Task Handle(GetIdProviderDefsAction action, IDispatcher dispatcher)
};
var httpResult = await httpClient.SendAsync(requestMessage);
var json = await httpResult.Content.ReadAsStringAsync();
var idProviderDefs = JsonSerializer.Deserialize<IEnumerable<AuthenticationSchemeProviderDefinition>>(json);
var idProviderDefs = SidJsonSerializer.Deserialize<IEnumerable<AuthenticationSchemeProviderDefinition>>(json);
dispatcher.Dispatch(new GetIdProviderDefsSuccessAction { AuthProviderDefinitions = idProviderDefs });
}

Expand Down
Loading

0 comments on commit 0466613

Please sign in to comment.