Skip to content

Commit d792ba5

Browse files
authored
feat: Additional auth fixes (#160)
1 parent ecd8847 commit d792ba5

15 files changed

Lines changed: 341 additions & 248 deletions

File tree

Examples/BookingSystem.AspNetCore.IdentityServer/Custom/Grants/PersistedGrantStore.cs

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using IdentityServer4.Extensions;
22
using IdentityServer4.Models;
33
using IdentityServer4.Stores;
4+
using Microsoft.Extensions.Logging;
45
using OpenActive.FakeDatabase.NET;
56
using System.Collections.Generic;
67
using System.Linq;
@@ -10,11 +11,21 @@ namespace IdentityServer
1011
{
1112
public class AcmePersistedGrantStore : IPersistedGrantStore
1213
{
14+
protected readonly ILogger _logger;
15+
16+
public AcmePersistedGrantStore(ILogger<AcmePersistedGrantStore> logger)
17+
{
18+
_logger = logger;
19+
}
20+
1321
public async Task<IEnumerable<PersistedGrant>> GetAllAsync(PersistedGrantFilter filter)
1422
{
1523
filter.Validate();
1624

1725
var grants = await FakeBookingSystem.Database.GetAllGrants(filter.SubjectId, filter.SessionId, filter.ClientId, filter.Type);
26+
27+
_logger.LogDebug("{persistedGrantCount} persisted grants found for {@filter}", grants.Count, filter);
28+
1829
var persistedGrants = grants.Select(grant => new PersistedGrant
1930
{
2031
Key = grant.Key,
@@ -23,6 +34,7 @@ public async Task<IEnumerable<PersistedGrant>> GetAllAsync(PersistedGrantFilter
2334
SessionId = grant.SessionId,
2435
ClientId = grant.ClientId,
2536
CreationTime = grant.CreationTime,
37+
ConsumedTime = grant.ConsumedTime,
2638
Expiration = grant.Expiration,
2739
Data = grant.Data
2840
}).ToList();
@@ -34,6 +46,8 @@ public async Task<PersistedGrant> GetAsync(string key)
3446
{
3547
var grant = await FakeBookingSystem.Database.GetGrant(key);
3648

49+
_logger.LogDebug("{persistedGrantKey} found in database: {persistedGrantKeyFound}", key, grant != null);
50+
3751
return grant != null ? new PersistedGrant
3852
{
3953
Key = grant.Key,
@@ -42,6 +56,7 @@ public async Task<PersistedGrant> GetAsync(string key)
4256
SessionId = grant.SessionId,
4357
ClientId = grant.ClientId,
4458
CreationTime = grant.CreationTime,
59+
ConsumedTime = grant.ConsumedTime,
4560
Expiration = grant.Expiration,
4661
Data = grant.Data
4762
} : null;
@@ -51,17 +66,28 @@ public async Task RemoveAllAsync(PersistedGrantFilter filter)
5166
{
5267
filter.Validate();
5368

69+
_logger.LogDebug("removing all persisted grants from database for {@filter}", filter);
70+
5471
await FakeBookingSystem.Database.RemoveAllGrants(filter.SubjectId, filter.SessionId, filter.ClientId, filter.Type);
5572
}
5673

5774
public async Task RemoveAsync(string key)
5875
{
76+
_logger.LogDebug("removing {persistedGrantKey} persisted grant from database", key);
77+
5978
await FakeBookingSystem.Database.RemoveGrant(key);
6079
}
6180

6281
public async Task StoreAsync(PersistedGrant grant)
6382
{
64-
await FakeBookingSystem.Database.AddGrant(grant.Key, grant.Type, grant.SubjectId, grant.SessionId, grant.ClientId, grant.CreationTime, grant.Expiration, grant.Data);
83+
if (await FakeBookingSystem.Database.AddGrant(grant.Key, grant.Type, grant.SubjectId, grant.SessionId, grant.ClientId, grant.CreationTime, grant.ConsumedTime, grant.Expiration, grant.Data))
84+
{
85+
_logger.LogDebug("{persistedGrantKey} not found in database, and so was inserted", grant.Key);
86+
}
87+
else
88+
{
89+
_logger.LogDebug("{persistedGrantKey} found in database, and updated", grant.Key);
90+
}
6591
}
6692
}
6793
}

Examples/BookingSystem.AspNetCore.IdentityServer/Startup.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,7 @@ public void ConfigureServices(IServiceCollection services)
3636
.AddInMemoryApiResources(Config.ApiResources)
3737
.AddClientStore<ClientStore>()
3838
.AddFakeUserStore(AppSettings.JsonLdIdBaseUrl)
39-
.AddPersistedGrantStore<AcmePersistedGrantStore>()
40-
.AddProfileService<ProfileService>(); //adding a custom profile service
39+
.AddPersistedGrantStore<AcmePersistedGrantStore>();
4140

4241
services.AddControllersWithViews();
4342

Examples/BookingSystem.AspNetCore.IdentityServer/Views/Account/Login.cshtml

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
@using OpenActive.FakeDatabase.NET
12
@model LoginViewModel
23

34
<div class="login-page">
@@ -15,7 +16,7 @@
1516
<div class="col-sm-6">
1617
<div class="card">
1718
<div class="card-header">
18-
<h2>Local Account</h2>
19+
<h2>Seller Account</h2>
1920
</div>
2021

2122
<div class="card-body">
@@ -84,4 +85,21 @@
8485
</div>
8586
}
8687
</div>
88+
<div class="row mt-5">
89+
<div class="col-sm-6">
90+
<div class="panel panel-default">
91+
<div class="panel-heading">
92+
<h3 class="panel-title">Test credentials</h3>
93+
</div>
94+
<div class="panel-body">
95+
<p>Please use the test Seller credentials below to log in:</p>
96+
<div><b>Username</b> &#8594; <b>Password</b></div>
97+
@foreach (var seller in FakeDatabase.DefaultSellerUsers)
98+
{
99+
<div>@seller.Username &#8594; @seller.PasswordRaw</div>
100+
}
101+
</div>
102+
</div>
103+
</div>
104+
</div>
87105
</div>

Examples/BookingSystem.AspNetCore/BookingSystem.AspNetCore.csproj

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,7 @@
1010
<PrivateAssets Condition="'%(PackageReference.Version)' == ''">all</PrivateAssets>
1111
<Publish Condition="'%(PackageReference.Version)' == ''">true</Publish>
1212
</PackageReference>
13-
<PackageReference Include="Microsoft.AspNetCore.Authentication.AzureADB2C.UI" Version="2.1.1" />
14-
<PackageReference Include="Microsoft.AspNetCore.Razor.Design" Version="2.1.2" PrivateAssets="All" />
13+
<PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="2.1.2" />
1514
<PackageReference Include="OpenActive.NET" Version="15.2.5" />
1615
</ItemGroup>
1716

Examples/BookingSystem.AspNetCore/Stores/OrderStore.cs

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@ namespace BookingSystem
1111
{
1212
public class OrderStateContext : IStateContext
1313
{
14+
// OrderStateContext will be disposed at the end of the flow
15+
public void Dispose()
16+
{
17+
}
1418
}
1519

1620
public class AcmeOrderStore : OrderStore<OrderTransaction, OrderStateContext>
@@ -181,13 +185,19 @@ public override async Task TriggerTestAction(OpenBookingSimulateAction simulateA
181185
}
182186
}
183187

184-
public override ValueTask<OrderStateContext> Initialise(StoreBookingFlowContext flowContext)
188+
public override ValueTask<OrderStateContext> CreateOrderStateContext(StoreBookingFlowContext flowContext)
185189
{
186-
// Runs before the flow starts, for both leasing and booking
187190
// Useful for transferring state between stages of the flow
188191
return new ValueTask<OrderStateContext>(new OrderStateContext());
189192
}
190193

194+
public override ValueTask Initialise(StoreBookingFlowContext flowContext, OrderStateContext stateContext)
195+
{
196+
// Runs before the flow starts, for both leasing and booking
197+
// Simply remove this method if it is not required
198+
return new ValueTask();
199+
}
200+
191201
private static BrokerRole BrokerTypeToBrokerRole(BrokerType brokerType)
192202
{
193203
return brokerType == BrokerType.AgentBroker

Examples/BookingSystem.AspNetFramework/BookingSystem.AspNetFramework.csproj

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -202,19 +202,19 @@
202202
<HintPath>..\..\packages\Schema.NET.7.0.1\lib\net461\Schema.NET.dll</HintPath>
203203
</Reference>
204204
<Reference Include="ServiceStack.Common, Version=5.0.0.0, Culture=neutral, processorArchitecture=MSIL">
205-
<HintPath>..\..\packages\ServiceStack.Common.Core.5.10.4\lib\netstandard2.0\ServiceStack.Common.dll</HintPath>
205+
<HintPath>..\..\packages\ServiceStack.Common.Core.5.11.0\lib\netstandard2.0\ServiceStack.Common.dll</HintPath>
206206
</Reference>
207207
<Reference Include="ServiceStack.Interfaces, Version=5.0.0.0, Culture=neutral, processorArchitecture=MSIL">
208-
<HintPath>..\..\packages\ServiceStack.Interfaces.Core.5.10.4\lib\netstandard2.0\ServiceStack.Interfaces.dll</HintPath>
208+
<HintPath>..\..\packages\ServiceStack.Interfaces.Core.5.11.0\lib\netstandard2.0\ServiceStack.Interfaces.dll</HintPath>
209209
</Reference>
210210
<Reference Include="ServiceStack.OrmLite, Version=5.0.0.0, Culture=neutral, processorArchitecture=MSIL">
211-
<HintPath>..\..\packages\ServiceStack.OrmLite.Core.5.10.4\lib\netstandard2.0\ServiceStack.OrmLite.dll</HintPath>
211+
<HintPath>..\..\packages\ServiceStack.OrmLite.Core.5.11.0\lib\netstandard2.0\ServiceStack.OrmLite.dll</HintPath>
212212
</Reference>
213213
<Reference Include="ServiceStack.OrmLite.Sqlite, Version=5.0.0.0, Culture=neutral, processorArchitecture=MSIL">
214-
<HintPath>..\..\packages\ServiceStack.OrmLite.Sqlite.Core.5.10.4\lib\netstandard2.0\ServiceStack.OrmLite.Sqlite.dll</HintPath>
214+
<HintPath>..\..\packages\ServiceStack.OrmLite.Sqlite.Core.5.11.0\lib\netstandard2.0\ServiceStack.OrmLite.Sqlite.dll</HintPath>
215215
</Reference>
216216
<Reference Include="ServiceStack.Text, Version=5.0.0.0, Culture=neutral, processorArchitecture=MSIL">
217-
<HintPath>..\..\packages\ServiceStack.Text.Core.5.10.4\lib\netstandard2.0\ServiceStack.Text.dll</HintPath>
217+
<HintPath>..\..\packages\ServiceStack.Text.Core.5.11.0\lib\netstandard2.0\ServiceStack.Text.dll</HintPath>
218218
</Reference>
219219
<Reference Include="Stubble.Core, Version=1.7.0.0, Culture=neutral, processorArchitecture=MSIL">
220220
<HintPath>..\..\packages\Stubble.Core.1.7.2\lib\net45\Stubble.Core.dll</HintPath>

Examples/BookingSystem.AspNetFramework/Stores/OrderStore.cs

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@ namespace BookingSystem
1111
{
1212
public class OrderStateContext : IStateContext
1313
{
14+
// OrderStateContext will be disposed at the end of the flow
15+
public void Dispose()
16+
{
17+
}
1418
}
1519

1620
public class AcmeOrderStore : OrderStore<OrderTransaction, OrderStateContext>
@@ -181,13 +185,19 @@ public override async Task TriggerTestAction(OpenBookingSimulateAction simulateA
181185
}
182186
}
183187

184-
public override ValueTask<OrderStateContext> Initialise(StoreBookingFlowContext flowContext)
188+
public override ValueTask<OrderStateContext> CreateOrderStateContext(StoreBookingFlowContext flowContext)
185189
{
186-
// Runs before the flow starts, for both leasing and booking
187190
// Useful for transferring state between stages of the flow
188191
return new ValueTask<OrderStateContext>(new OrderStateContext());
189192
}
190193

194+
public override ValueTask Initialise(StoreBookingFlowContext flowContext, OrderStateContext stateContext)
195+
{
196+
// Runs before the flow starts, for both leasing and booking
197+
// Simply remove this method if it is not required
198+
return new ValueTask();
199+
}
200+
191201
private static BrokerRole BrokerTypeToBrokerRole(BrokerType brokerType)
192202
{
193203
return brokerType == BrokerType.AgentBroker

Examples/BookingSystem.AspNetFramework/packages.config

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -85,14 +85,14 @@
8585
<package id="Owin" version="1.0" targetFramework="net461" />
8686
<package id="Schema.NET" version="7.0.1" targetFramework="net461" />
8787
<package id="ServiceStack.Common" version="5.10.4" targetFramework="net461" />
88-
<package id="ServiceStack.Common.Core" version="5.10.4" targetFramework="net461" />
88+
<package id="ServiceStack.Common.Core" version="5.11.0" targetFramework="net461" />
8989
<package id="ServiceStack.Interfaces" version="5.10.4" targetFramework="net461" />
90-
<package id="ServiceStack.Interfaces.Core" version="5.10.4" targetFramework="net461" />
90+
<package id="ServiceStack.Interfaces.Core" version="5.11.0" targetFramework="net461" />
9191
<package id="ServiceStack.OrmLite" version="5.10.4" targetFramework="net461" />
92-
<package id="ServiceStack.OrmLite.Core" version="5.10.4" targetFramework="net461" />
93-
<package id="ServiceStack.OrmLite.Sqlite.Core" version="5.10.4" targetFramework="net461" />
92+
<package id="ServiceStack.OrmLite.Core" version="5.11.0" targetFramework="net461" />
93+
<package id="ServiceStack.OrmLite.Sqlite.Core" version="5.11.0" targetFramework="net461" />
9494
<package id="ServiceStack.Text" version="5.10.4" targetFramework="net461" />
95-
<package id="ServiceStack.Text.Core" version="5.10.4" targetFramework="net461" />
95+
<package id="ServiceStack.Text.Core" version="5.11.0" targetFramework="net461" />
9696
<package id="Stub.System.Data.SQLite.Core.NetFramework" version="1.0.113.3" targetFramework="net461" />
9797
<package id="Stubble.Core" version="1.7.2" targetFramework="net461" />
9898
<package id="Stubble.Extensions.JsonNet.Net45" version="1.3.3" targetFramework="net461" />

Fakes/OpenActive.FakeDatabase.NET.Tests/OpenActive.FakeDatabase.NET.Tests.csproj

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.9.0" />
1111
<PackageReference Include="xunit" Version="2.4.0" />
1212
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.0" />
13-
<PackageReference Include="NPoco" Version="4.0.2" />
1413
<PackageReference Include="OpenActive.NET" Version="15.2.5" />
1514
<PackageReference Include="Newtonsoft.Json" Version="12.0.3" />
1615
</ItemGroup>

Fakes/OpenActive.FakeDatabase.NET/FakeBookingSystem.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1661,7 +1661,7 @@ public async Task<GrantTable> GetGrant(string key)
16611661
return await db.SingleAsync<GrantTable>(x => x.Key == key);
16621662
}
16631663
}
1664-
public async Task<IEnumerable<GrantTable>> GetAllGrants(string subjectId, string sessionId, string clientId, string type)
1664+
public async Task<List<GrantTable>> GetAllGrants(string subjectId, string sessionId, string clientId, string type)
16651665
{
16661666
using (var db = await Mem.Database.OpenAsync())
16671667
{
@@ -1687,7 +1687,7 @@ public async Task<IEnumerable<GrantTable>> GetAllGrants(string subjectId, string
16871687
}
16881688
}
16891689

1690-
public async Task AddGrant(string key, string type, string subjectId, string sessionId, string clientId, DateTime creationTime, DateTime? expiration, string data)
1690+
public async Task<bool> AddGrant(string key, string type, string subjectId, string sessionId, string clientId, DateTime creationTime, DateTime? consumedTime, DateTime? expiration, string data)
16911691
{
16921692
using (var db = await Mem.Database.OpenAsync())
16931693
{
@@ -1699,10 +1699,11 @@ public async Task AddGrant(string key, string type, string subjectId, string ses
16991699
SessionId = sessionId,
17001700
ClientId = clientId,
17011701
CreationTime = creationTime,
1702+
ConsumedTime = consumedTime,
17021703
Expiration = expiration,
17031704
Data = data
17041705
};
1705-
await db.SaveAsync(grant);
1706+
return await db.SaveAsync(grant);
17061707
}
17071708
}
17081709

0 commit comments

Comments
 (0)