Skip to content

Commit

Permalink
Register the stores
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewgunn committed Apr 11, 2018
1 parent b339ed9 commit 0f56291
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 25 deletions.
4 changes: 2 additions & 2 deletions appveyor.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ for:
only:
- master

version: 0.1.5
version: 0.1.6

deploy:
- provider: Environment
Expand All @@ -38,4 +38,4 @@ for:
except:
- master

version: 0.1.5.{build}-{branch}
version: 0.1.6.{build}-{branch}
35 changes: 35 additions & 0 deletions src/Gunnsoft.IdentityServer.Stores.MongoDB/BsonClassMapper.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
using Gunnsoft.IdentityServer.Stores.MongoDB.Collections.Clients;
using Gunnsoft.IdentityServer.Stores.MongoDB.Collections.PersistedGrants;
using MongoDB.Bson.Serialization;

namespace Gunnsoft.IdentityServer.Stores.MongoDB
{
public static class BsonClassMapper
{
public static void MapClient()
{
if (!BsonClassMap.IsClassMapRegistered(typeof(Client)))
{
BsonClassMap.RegisterClassMap<Client>(cm =>
{
cm.AutoMap();
cm.MapIdMember(c => c.Id);
cm.SetIgnoreExtraElements(true);
});
}
}

public static void MapPeristedGrant()
{
if (!BsonClassMap.IsClassMapRegistered(typeof(PersistedGrant)))
{
BsonClassMap.RegisterClassMap<PersistedGrant>(cm =>
{
cm.AutoMap();
cm.MapIdMember(c => c.Id);
cm.SetIgnoreExtraElements(true);
});
}
}
}
}
9 changes: 3 additions & 6 deletions src/Gunnsoft.IdentityServer.Stores.MongoDB/CollectionNames.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
namespace Gunnsoft.IdentityServer.Stores.MongoDB
{
public static partial class IdentityServerBuilderExtensions
public static class CollectionNames
{
public static class CollectionNames
{
public const string Clients = "clients";
public const string PersistedGrants = "persistedGrants";
}
public const string Clients = "clients";
public const string PersistedGrants = "persistedGrants";
}
}
Original file line number Diff line number Diff line change
@@ -1,17 +1,20 @@
using System;
using Gunnsoft.IdentityServer.Stores.MongoDB.Collections.Clients;
using Gunnsoft.IdentityServer.Stores.MongoDB.Collections.PersistedGrants;
using IdentityServer4.Stores;
using Microsoft.Extensions.DependencyInjection;
using MongoDB.Driver;

namespace Gunnsoft.IdentityServer.Stores.MongoDB
{
public static partial class IdentityServerBuilderExtensions
public static class IdentityServerBuilderExtensions
{
public static IIdentityServerBuilder AddMongoClientStore(this IIdentityServerBuilder extended, MongoUrl mongoUrl)
{
MongoConfigurator.Configure();

BsonClassMapper.MapClient();

var client = new MongoClient(mongoUrl);

if (mongoUrl.DatabaseName == null)
Expand All @@ -23,7 +26,7 @@ public static IIdentityServerBuilder AddMongoClientStore(this IIdentityServerBui

var clientsCollection = database.GetCollection<Client>(CollectionNames.Clients);

extended.Services.AddTransient(cc => new MongoClientStore(clientsCollection));
extended.Services.AddTransient<IClientStore>(cc => new MongoClientStore(clientsCollection));

return extended;
}
Expand All @@ -32,6 +35,8 @@ public static IIdentityServerBuilder AddMongoPersistedGrantStore(this IIdentityS
{
MongoConfigurator.Configure();

BsonClassMapper.MapPeristedGrant();

var client = new MongoClient(mongoUrl);

if (mongoUrl.DatabaseName == null)
Expand All @@ -43,7 +48,7 @@ public static IIdentityServerBuilder AddMongoPersistedGrantStore(this IIdentityS

var persistedGrantsCollection = database.GetCollection<PersistedGrant>(CollectionNames.PersistedGrants);

extended.Services.AddTransient(cc => new MongoPersistedGrantStore(persistedGrantsCollection));
extended.Services.AddTransient<IPersistedGrantStore>(cc => new MongoPersistedGrantStore(persistedGrantsCollection));

return extended;
}
Expand Down
14 changes: 1 addition & 13 deletions src/Gunnsoft.IdentityServer.Stores.MongoDB/MongoConfigurator.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
using Gunnsoft.IdentityServer.Stores.MongoDB.Collections.PersistedGrants;
using MongoDB.Bson;
using MongoDB.Bson.Serialization;
using MongoDB.Bson;
using MongoDB.Bson.Serialization.Conventions;

namespace Gunnsoft.IdentityServer.Stores.MongoDB
Expand All @@ -18,16 +16,6 @@ public static void Configure()

ConventionRegistry.Remove(conventionName);
ConventionRegistry.Register(conventionName, conventionPack, t => true);

if (!BsonClassMap.IsClassMapRegistered(typeof(PersistedGrant)))
{
BsonClassMap.RegisterClassMap<PersistedGrant>(cm =>
{
cm.AutoMap();
cm.MapIdMember(c => c.Id);
cm.SetIgnoreExtraElements(true);
});
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public async Task<PersistedGrant> GetAsync(string key)
}

return await _persistedGrantsCollection.Find(pg => pg.Key == key)
.SortByDescending(pg => pg)
.SortByDescending(pg => pg.Id)
.Project(pg => new PersistedGrant
{
ClientId = pg.ClientId,
Expand Down

0 comments on commit 0f56291

Please sign in to comment.