Ejemplo n.º 1
0
 public CzarIntrospectionRequestValidator(ITokenValidator tokenValidator, DapperStoreOptions config, ICache <CzarToken> cache, ILogger <CzarIntrospectionRequestValidator> logger)
 {
     _tokenValidator = tokenValidator;
     _config         = config;
     _cache          = cache;
     _logger         = logger;
 }
 /// <summary>
 /// Add a custom implementation for <see cref="UsersTable{TUser, TKey, TUserClaim, TUserRole, TUserLogin, TUserToken}"/>.
 /// </summary>
 /// <typeparam name="TUsersTable">The type of the table to register.</typeparam>
 /// <typeparam name="TUser">The type representing a user.</typeparam>
 /// <typeparam name="TKey">The type of the primary key for a role and user.</typeparam>
 /// <param name="options">Options for configuring Dapper stores.</param>
 public static void AddUsersTable <TUsersTable, TUser, TKey>(this DapperStoreOptions options)
     where TUsersTable : UsersTable <TUser, TKey, IdentityUserClaim <TKey>, IdentityUserRole <TKey>, IdentityUserLogin <TKey>, IdentityUserToken <TKey> >
     where TUser : IdentityUser <TKey>
     where TKey : IEquatable <TKey>
 {
     options.AddUsersTable <TUsersTable, TUser, TKey, IdentityUserClaim <TKey>, IdentityUserRole <TKey>, IdentityUserLogin <TKey>, IdentityUserToken <TKey> >();
 }
 /// <summary>
 /// Add a custom implementation for <see cref="UserClaimsTable{TKey, TUserClaim}"/>.
 /// </summary>
 /// <typeparam name="TUserClaimsTable">The type of the table to register.</typeparam>
 /// <typeparam name="TKey">The type of the primary key for a user.</typeparam>
 /// <typeparam name="TUserClaim">The type representing a claim.</typeparam>
 /// <param name="options">Options for configuring Dapper stores.</param>
 public static void AddUserClaimsTable <TUserClaimsTable, TKey, TUserClaim>(this DapperStoreOptions options)
     where TUserClaimsTable : UserClaimsTable <TKey, TUserClaim>
     where TKey : IEquatable <TKey>
     where TUserClaim : IdentityUserClaim <TKey>, new()
 {
     options.Services.AddScoped(typeof(IUserClaimsTable <,>).MakeGenericType(typeof(TKey), typeof(TUserClaim)), typeof(TUserClaimsTable));
 }
 /// <summary>
 ///     Add a custom implementation for
 ///     <see cref="UsersRecord{TUser, TKey, TUserClaim, TUserRole, TUserLogin, TUserToken}" />.
 /// </summary>
 /// <typeparam name="TUsersRecord">The type of the table to register.</typeparam>
 /// <typeparam name="TUser">The type representing a user.</typeparam>
 /// <param name="options">Options for configuring Dapper stores.</param>
 public static void AddUsersRecord <TUsersRecord, TUser>(this DapperStoreOptions options)
     where TUsersRecord : UsersRecord <TUser, string, IdentityUserClaim <string>, IdentityUserRole <string>,
                                       IdentityUserLogin <string>, IdentityUserToken <string> >
     where TUser : IdentityUser <string>
 {
     options.AddUsersRecord <TUsersRecord, TUser, string>();
 }
 /// <summary>
 ///     Add a custom implementation for <see cref="UserTokensRecord{TKey, TUserToken}" />.
 /// </summary>
 /// <typeparam name="TUserTokensRecord">The type of the table to register.</typeparam>
 /// <typeparam name="TKey">The type of the primary key for a role and user.</typeparam>
 /// <typeparam name="TUserToken">The type representing a user token.</typeparam>
 /// <param name="options">Options for configuring Dapper stores.</param>
 public static void AddUserTokensRecord <TUserTokensRecord, TKey, TUserToken>(this DapperStoreOptions options)
     where TUserTokensRecord : UserTokensRecord <TKey, TUserToken>
     where TKey : IEquatable <TKey>
     where TUserToken : IdentityUserToken <TKey>, new()
 {
     options.Services.AddScoped(typeof(IUserTokensRecord <,>).MakeGenericType(typeof(TKey), typeof(TUserToken)),
                                typeof(TUserTokensRecord));
 }
 /// <summary>
 ///     Add a custom implementation for <see cref="RoleClaimsRecord{TKey, TRoleClaim}" />.
 /// </summary>
 /// <typeparam name="TRoleClaimsRecord">The type of the table to register.</typeparam>
 /// <typeparam name="TKey">The type of the primary key for a role.</typeparam>
 /// <typeparam name="TRoleClaim">The type of the class representing a role claim.</typeparam>
 /// <param name="options">Options for configuring Dapper stores.</param>
 public static void AddRoleClaimsRecord <TRoleClaimsRecord, TKey, TRoleClaim>(this DapperStoreOptions options)
     where TRoleClaimsRecord : RoleClaimsRecord <TKey, TRoleClaim>
     where TKey : IEquatable <TKey>
     where TRoleClaim : IdentityRoleClaim <TKey>, new()
 {
     options.Services.AddScoped(typeof(IRoleClaimsRecord <,>).MakeGenericType(typeof(TKey), typeof(TRoleClaim)),
                                typeof(TRoleClaimsRecord));
 }
Ejemplo n.º 7
0
        public TokenCleanup(IPersistedGrants persistedGrants, ILogger <TokenCleanup> logger, DapperStoreOptions options)
        {
            _options = options ?? throw new ArgumentNullException(nameof(options));
            if (_options.TokenCleanupInterval < 1)
            {
                throw new ArgumentException("Token cleanup interval must be at least 1 second");
            }

            _logger          = logger ?? throw new ArgumentNullException(nameof(logger));
            _persistedGrants = persistedGrants;
        }
 /// <summary>
 ///     Add a custom implementation for
 ///     <see cref="UsersRecord{TUser, TKey, TUserClaim, TUserRole, TUserLogin, TUserToken}" />.
 /// </summary>
 /// <typeparam name="TUsersRecord">The type of the table to register.</typeparam>
 /// <typeparam name="TUser">The type representing a user.</typeparam>
 /// <typeparam name="TKey">The type of the primary key for a role and user.</typeparam>
 /// <typeparam name="TUserClaim">The type representing a claim.</typeparam>
 /// <typeparam name="TUserRole">The type representing a user role.</typeparam>
 /// <typeparam name="TUserLogin">The type representing a user external login.</typeparam>
 /// <typeparam name="TUserToken">The type representing a user token.</typeparam>
 /// <param name="options">Options for configuring Dapper stores.</param>
 public static void AddUsersRecord <TUsersRecord, TUser, TKey, TUserClaim, TUserRole, TUserLogin, TUserToken>(
     this DapperStoreOptions options)
     where TUsersRecord : UsersRecord <TUser, TKey, TUserClaim, TUserRole, TUserLogin, TUserToken>
     where TUser : IdentityUser <TKey>
     where TKey : IEquatable <TKey>
     where TUserClaim : IdentityUserClaim <TKey>, new()
     where TUserRole : IdentityUserRole <TKey>, new()
     where TUserLogin : IdentityUserLogin <TKey>, new()
     where TUserToken : IdentityUserToken <TKey>, new()
 {
     options.Services.AddScoped(
         typeof(IUsersRecord <, , , , ,>).MakeGenericType(typeof(TUser), typeof(TKey), typeof(TUserClaim),
                                                          typeof(TUserRole), typeof(TUserLogin), typeof(TUserToken)), typeof(TUsersRecord));
 }
        /// <summary>
        /// 配置Dapper接口和实现(默认使用SqlServer)
        /// </summary>
        /// <param name="builder">The builder.</param>
        /// <param name="storeOptionsAction">存储配置信息</param>
        /// <returns></returns>
        public static IIdentityServerBuilder AddDapperStore(
            this IIdentityServerBuilder builder,
            Action <DapperStoreOptions> storeOptionsAction = null)
        {
            var options = new DapperStoreOptions();

            builder.Services.AddSingleton(options);
            storeOptionsAction?.Invoke(options);
            builder.Services.AddSingleton <TokenCleanup>();
            builder.Services.AddSingleton <IHostedService, TokenCleanupHost>();
            builder.Services.AddSingleton <ITokenResponseGenerator, CusTokenResponseGenerator>();
            builder.Services.AddTransient(typeof(ICache <>), typeof(MemoryCache <>));
            builder.Services.AddTransient <IIntrospectionRequestValidator, IntrospectionRequestValidator>();
            return(builder);
        }
        public static IIdentityServerBuilder AddDapperStore(
            this IIdentityServerBuilder builder,
            Action <DapperStoreOptions> storeOptionsAction)
        {
            var options = new DapperStoreOptions();

            builder.Services.AddSingleton(options);
            storeOptionsAction?.Invoke(options);
            builder.Services.AddTransient <IClientStore, SqlServerClientStore>();
            builder.Services.AddTransient <IResourceStore, SqlServerResourceStore>();
            builder.Services.AddTransient <IPersistedGrantStore, SqlServerPersistedGrantStore>();
            builder.Services.AddTransient <IPersistedGrants, SqlServerPersistedGrants>();
            builder.Services.AddSingleton <TokenCleanup>();
            builder.Services.AddSingleton <IHostedService, TokenCleanupHost>();
            return(builder);
        }
Ejemplo n.º 11
0
 public RedisCache(DapperStoreOptions configurationStoreOptions)
 {
     CSRedis.CSRedisClient csredis;
     if (configurationStoreOptions.RedisConnectionStrings.Count == 1)
     {
         //普通模式
         csredis = new CSRedis.CSRedisClient(configurationStoreOptions.RedisConnectionStrings[0]);
     }
     else
     {
         //集群模式
         //实现思路:根据key.GetHashCode() % 节点总数量,确定连向的节点
         //也可以自定义规则(第一个参数设置)
         csredis = new CSRedis.CSRedisClient(item => item, configurationStoreOptions.RedisConnectionStrings.ToArray());
     }
     //初始化 RedisHelper
     RedisHelper.Initialization(csredis);
 }
Ejemplo n.º 12
0
        /// <summary>
        /// 配置Dapper接口和实现(默认使用SqlServer)
        /// </summary>
        /// <param name="builder">The builder.</param>
        /// <param name="storeOptionsAction">存储配置信息</param>
        /// <returns></returns>
        public static IIdentityServerBuilder AddDapperStore(
            this IIdentityServerBuilder builder,
            Action <DapperStoreOptions> storeOptionsAction = null)
        {
            var options = new DapperStoreOptions();

            builder.Services.AddSingleton(options);
            storeOptionsAction?.Invoke(options);
            builder.Services.AddTransient <IClientStore, SqlServerClientStore>();
            builder.Services.AddTransient <IResourceStore, SqlServerResourceStore>();
            builder.Services.AddTransient <IPersistedGrantStore, SqlServerPersistedGrantStore>();
            builder.Services.AddTransient <IPersistedGrants, SqlServerPersistedGrants>();
            builder.Services.AddSingleton <TokenCleanup>();
            builder.Services.AddSingleton <IHostedService, TokenCleanupHost>();
            builder.Services.AddSingleton <ITokenResponseGenerator, CzarTokenResponseGenerator>();
            builder.Services.AddTransient(typeof(ICache <>), typeof(CzarRedisCache <>));
            builder.Services.AddTransient <IIntrospectionRequestValidator, CzarIntrospectionRequestValidator>();
            return(builder);
        }
Ejemplo n.º 13
0
        /// <summary>
        /// 配置Dapper接口和实现(默认使用SqlServer)
        /// </summary>
        /// <param name="builder">The builder.</param>
        /// <param name="storeOptionsAction">存储配置信息</param>
        /// <returns></returns>
        public static IIdentityServerBuilder AddDapperStore(
            this IIdentityServerBuilder builder,
            Action <DapperStoreOptions> storeOptionsAction = null)
        {
            var options = new DapperStoreOptions();

            builder.Services.AddSingleton(options);
            storeOptionsAction?.Invoke(options);
            builder.Services.AddTransient <IClientStore, SqlServerClientStore>();
            builder.Services.AddTransient <IResourceStore, SqlServerResourceStore>();
            builder.Services.AddTransient <IPersistedGrantStore, SqlServerPersistedGrantStore>();
            builder.Services.AddTransient <IPersistedGrants, SqlServerPersistedGrants>();
            //builder.Services.AddTransient<IHandleGenerationService, DefaultHandleGenerationService>();
            //builder.Services.AddTransient<IPersistentGrantSerializer, PersistentGrantSerializer>();
            //builder.Services.AddTransient<IReferenceTokenStore, DefaultReferenceTokenStore>();
            builder.Services.AddSingleton <TokenCleanup>();
            builder.Services.AddSingleton <IHostedService, TokenCleanupHost>();
            return(builder);
        }
Ejemplo n.º 14
0
        public static IIdentityServerBuilder AddDapperStore(this IIdentityServerBuilder builder, Action <DapperStoreOptions> options = null)
        {
            var opts = new DapperStoreOptions();

            builder.Services.AddSingleton(opts);
            options?.Invoke(opts);

            builder.AddClientStore <ClientStore>();
            builder.AddResourceStore <ResourceStore>();
            builder.AddCorsPolicyService <CorsPolicyService>();

            builder.AddInMemoryCaching();
            builder.AddClientStoreCache <ClientStore>();
            builder.AddResourceStoreCache <ResourceStore>();
            builder.AddCorsPolicyCache <CorsPolicyService>();

            builder.Services.AddTransient <IPersistedGrantStore, PersistedGrantStore>();
            builder.Services.AddTransient <IDeviceFlowStore, DeviceFlowStore>();

            builder.Services.AddSingleton <TokenCleanup>();
            builder.Services.AddSingleton <IHostedService, TokenCleanupHostedService>();

            return(builder);
        }
Ejemplo n.º 15
0
 public ClientStore(DapperStoreOptions dapperStoreOptions)
 {
     _dapperStoreOptions = dapperStoreOptions;
 }
 public ResourceStore(DapperStoreOptions dapperStoreOptions)
 {
     _dapperStoreOptions = dapperStoreOptions;
 }
 public PersistedGrantStore(DapperStoreOptions dapperStoreOptions)
 {
     _dapperStoreOptions = dapperStoreOptions;
 }
Ejemplo n.º 18
0
 public TokenCleanupHost(TokenCleanup tokenCleanup, DapperStoreOptions options)
 {
     _tokenCleanup = tokenCleanup;
     _options      = options;
 }
Ejemplo n.º 19
0
 public CzarTokenResponseGenerator(ISystemClock clock, ITokenService tokenService, IRefreshTokenService refreshTokenService, IResourceStore resources, IClientStore clients, ILogger <TokenResponseGenerator> logger, DapperStoreOptions config, ICache <CzarToken> cache) : base(clock, tokenService, refreshTokenService, resources, clients, logger)
 {
     _config = config;
     _cache  = cache;
 }
 public MySqlPersistedGrantStore(ILogger <MySqlPersistedGrantStore> logger, DapperStoreOptions configurationStoreOptions)
 {
     _logger = logger;
     _configurationStoreOptions = configurationStoreOptions;
 }
 /// <summary>
 ///     Add a custom implementation for <see cref="UserClaimsRecord{TKey, TUserClaim}" />.
 /// </summary>
 /// <typeparam name="TUserClaimsRecord">The type of the table to register.</typeparam>
 /// <typeparam name="TUserClaim">The type representing a claim.</typeparam>
 /// <param name="options">Options for configuring Dapper stores.</param>
 public static void AddUserClaimsRecord <TUserClaimsRecord, TUserClaim>(this DapperStoreOptions options)
     where TUserClaimsRecord : UserClaimsRecord <string, TUserClaim>
     where TUserClaim : IdentityUserClaim <string>, new()
 {
     options.AddUserClaimsRecord <TUserClaimsRecord, string, TUserClaim>();
 }
 /// <summary>
 ///     Add a custom implementation for <see cref="RolesRecord{TRole, TKey, TRoleClaim}" />.
 /// </summary>
 /// <typeparam name="TRolesRecord">The type of the table to register.</typeparam>
 /// <typeparam name="TRole">The type of the class representing a role.</typeparam>
 /// <param name="options">Options for configuring Dapper stores.</param>
 public static void AddRolesRecord <TRolesRecord, TRole>(this DapperStoreOptions options)
     where TRolesRecord : RolesRecord <TRole, string, IdentityRoleClaim <string> >
     where TRole : IdentityRole <string>
 {
     options.AddRolesRecord <TRolesRecord, TRole, string, IdentityRoleClaim <string> >();
 }
Ejemplo n.º 23
0
 public MySqlResourceStore(ILogger <MySqlResourceStore> logger, DapperStoreOptions configurationStoreOptions)
 {
     _logger = logger;
     _configurationStoreOptions = configurationStoreOptions;
 }
Ejemplo n.º 24
0
        private static void AddStores(IServiceCollection services, Type userType, Type roleType, IDbConnectionFactory dbConnectionFactory,
                                      Action <DapperStoreOptions> configureAction = null)
        {
            var identityUserType = FindGenericBaseType(userType, typeof(IdentityUser <>));

            if (identityUserType == null)
            {
                throw new InvalidOperationException(
                          $"Method {nameof(AddDapperStores)} can only be called with a user that derives from IdentityUser<TKey>.");
            }

            var serviceProvider            = services.BuildServiceProvider();
            var configuration              = serviceProvider.GetRequiredService <IConfiguration>();
            var dbConnectionContextOptions = new DapperStoreOptions
            {
                ConnectionString    = configuration.GetConnectionString("DefaultConnection"),
                DbConnectionFactory = dbConnectionFactory,
                Services            = services
            };

            configureAction?.Invoke(dbConnectionContextOptions);
            dbConnectionContextOptions.Services = null;
            var keyType = identityUserType.GenericTypeArguments[0];

            services.TryAddScoped(typeof(IDbConnectionFactory), x =>
            {
                var dbConnectionFactoryInstance =
                    (IDbConnectionFactory)Activator.CreateInstance(dbConnectionContextOptions.DbConnectionFactory
                                                                   .GetType());
                dbConnectionFactoryInstance.ConnectionString = dbConnectionContextOptions.ConnectionString;
                return(dbConnectionFactoryInstance);
            });
            Type userStoreType;
            var  userClaimType = typeof(IdentityUserClaim <>).MakeGenericType(keyType);
            var  userRoleType  = typeof(IdentityUserRole <>).MakeGenericType(keyType);
            var  userLoginType = typeof(IdentityUserLogin <>).MakeGenericType(keyType);
            var  roleClaimType = typeof(IdentityRoleClaim <>).MakeGenericType(keyType);
            var  userTokenType = typeof(IdentityUserToken <>).MakeGenericType(keyType);

            if (roleType != null)
            {
                var identityRoleType = FindGenericBaseType(roleType, typeof(IdentityRole <>));
                if (identityRoleType == null)
                {
                    throw new InvalidOperationException(
                              $"Method {nameof(AddDapperStores)} can only be called with a role that derives from IdentityRole<TKey>.");
                }

                services.TryAddScoped(
                    typeof(IUsersRecord <, , , , ,>).MakeGenericType(userType, keyType, userClaimType, userRoleType,
                                                                     userLoginType, userTokenType),
                    typeof(UsersRecord <, , , , ,>).MakeGenericType(userType, keyType, userClaimType, userRoleType,
                                                                    userLoginType, userTokenType)
                    );
                services.TryAddScoped(typeof(IRolesRecord <, ,>).MakeGenericType(roleType, keyType, roleClaimType),
                                      typeof(RolesRecord <, ,>).MakeGenericType(roleType, keyType, roleClaimType));
                services.TryAddScoped(typeof(IUserRolesRecord <, ,>).MakeGenericType(roleType, keyType, userRoleType),
                                      typeof(UserRolesRecord <, ,>).MakeGenericType(roleType, keyType, userRoleType));
                services.TryAddScoped(typeof(IRoleClaimsRecord <,>).MakeGenericType(keyType, roleClaimType),
                                      typeof(RoleClaimsRecord <,>).MakeGenericType(keyType, roleClaimType));
                services.TryAddScoped(typeof(IRoleStore <>).MakeGenericType(roleType),
                                      typeof(RoleStore <, , ,>).MakeGenericType(roleType, keyType, userRoleType, roleClaimType));
                userStoreType = typeof(UserStore <, , , , , , ,>).MakeGenericType(userType, roleType, keyType,
                                                                                  userClaimType, userRoleType, userLoginType, userTokenType, roleClaimType);
            }
            else
            {
                services.TryAddScoped(
                    typeof(IUsersOnlyRecord <, , , ,>).MakeGenericType(userType, keyType, userClaimType, userLoginType,
                                                                       userTokenType),
                    typeof(UsersRecord <, , , , ,>).MakeGenericType(userType, keyType, userClaimType, roleType,
                                                                    userLoginType, userTokenType)
                    );
                userStoreType = typeof(UserOnlyStore <, , , ,>).MakeGenericType(userType, keyType, userClaimType,
                                                                                userLoginType, userTokenType);
            }

            services.TryAddScoped(typeof(IUserClaimsRecord <,>).MakeGenericType(keyType, userClaimType),
                                  typeof(UserClaimsRecord <,>).MakeGenericType(keyType, userClaimType));
            services.TryAddScoped(typeof(IUserLoginsRecord <, ,>).MakeGenericType(userType, keyType, userLoginType),
                                  typeof(UserLoginsRecord <, ,>).MakeGenericType(userType, keyType, userLoginType));
            services.TryAddScoped(typeof(IUserTokensRecord <,>).MakeGenericType(keyType, userTokenType),
                                  typeof(UserTokensRecord <,>).MakeGenericType(keyType, userTokenType));
            services.TryAddScoped(typeof(IUserStore <>).MakeGenericType(userType), userStoreType);
        }
 /// <summary>
 ///     Add a custom implementation for <see cref="UserTokensRecord{TKey, TUserToken}" />.
 /// </summary>
 /// <typeparam name="TUserTokensRecord">The type of the table to register.</typeparam>
 /// <typeparam name="TUserToken">The type representing a user token.</typeparam>
 /// <param name="options">Options for configuring Dapper stores.</param>
 public static void AddUserTokensRecord <TUserTokensRecord, TUserToken>(this DapperStoreOptions options)
     where TUserTokensRecord : UserTokensRecord <string, TUserToken>
     where TUserToken : IdentityUserToken <string>, new()
 {
     options.AddUserTokensRecord <TUserTokensRecord, string, TUserToken>();
 }
Ejemplo n.º 26
0
        private readonly IPersistentGrantSerializer _persistentGrantSerializer; // PersistentGrantSerializer

        public DeviceFlowStore(DapperStoreOptions dapperStoreOptions, IPersistentGrantSerializer persistentGrantSerializer)
        {
            _dapperStoreOptions        = dapperStoreOptions;
            _persistentGrantSerializer = persistentGrantSerializer;
        }
 /// <summary>
 ///     Add a custom implementation for <see cref="RoleClaimsRecord{TKey,TRoleClaim}" />.
 /// </summary>
 /// <typeparam name="TRoleClaimsRecord">The type of the table to register.</typeparam>
 /// <typeparam name="TRoleClaim">The type of the class representing a role claim.</typeparam>
 /// <param name="options">Options for configuring Dapper stores.</param>
 public static void AddRoleClaimsRecord <TRoleClaimsRecord, TRoleClaim>(this DapperStoreOptions options)
     where TRoleClaimsRecord : RoleClaimsRecord <string, TRoleClaim>
     where TRoleClaim : IdentityRoleClaim <string>, new()
 {
     options.AddRoleClaimsRecord <TRoleClaimsRecord, string, TRoleClaim>();
 }
Ejemplo n.º 28
0
 public ClientStore(ILogger <ClientStore> logger, DapperStoreOptions configurationStoreOptions)
 {
     _logger = logger;
     _configurationStoreOptions = configurationStoreOptions;
 }
 public SqlServerPersistedGrants(ILogger <SqlServerPersistedGrants> logger, DapperStoreOptions configurationStoreOptions)
 {
     _logger = logger;
     _configurationStoreOptions = configurationStoreOptions;
 }
 /// <summary>
 ///     Add a custom implementation for <see cref="UserRolesRecord{TRole, TKey, TUserRole}" />.
 /// </summary>
 /// <typeparam name="TUserRolesRecord">The type of the table to register.</typeparam>
 /// <typeparam name="TUserRole">The type representing a user role.</typeparam>
 /// <param name="options">Options for configuring Dapper stores.</param>
 public static void AddUserRolesRecord <TUserRolesRecord, TUserRole>(this DapperStoreOptions options)
     where TUserRolesRecord : UserRolesRecord <IdentityRole, string, TUserRole>
     where TUserRole : IdentityUserRole <string>, new()
 {
     options.AddUserRolesRecord <TUserRolesRecord, IdentityRole, string, TUserRole>();
 }