/// <param name="next">The next middleware to run</param>
 /// <param name="updateExpiration">If the middleware should update the JWT expiration, default <c>true</c></param>
 /// <param name="jwtExpiration">The Expiration for the JWT, default 1 hour</param>
 /// <param name="jwtService">The service to use in the middleware, default <see cref="JwtService" /> with no params</param>
 /// <param name="jwtIdentifier">The key that identifies the JWT, default <c>"StandardDotJwt"</c></param>
 /// <param name="beforeSubscribers">Subscribers to the after jwt work, before next is called event</param>
 /// <param name="afterSubscribers">Subscribers to the after next is called, before jwt work event</param>
 public JwtServiceMiddleware(RequestDelegate next, bool updateExpiration = true, TimeSpan?jwtExpiration = null,
                             string jwtIdentifier = null, JwtService jwtService = null,
                             IEnumerable <Func <JwtServiceMiddleware <T>, JwtEventArgs <T>, Task> > beforeSubscribers         = null,
                             IEnumerable <Func <JwtServiceMiddleware <T>, AfterWorkJwtEventArgs <T>, Task> > afterSubscribers = null)
 {
     _next             = next;
     _updateExpiration = updateExpiration;
     _jwtIdentifier    = jwtIdentifier ?? "StandardDotJwt";
     _jwtService       = jwtService ?? new JwtService();
     _jwtExpiration    = jwtExpiration ?? new TimeSpan(1, 0, 0);
     if (beforeSubscribers != null)
     {
         foreach (Func <JwtServiceMiddleware <T>, JwtEventArgs <T>, Task> beforeSubscriber in beforeSubscribers)
         {
             AfterJwtWorkBeforeNext += beforeSubscriber;
         }
     }
     if (afterSubscribers != null)
     {
         foreach (Func <JwtServiceMiddleware <T>, AfterWorkJwtEventArgs <T>, Task> afterSubscriber in afterSubscribers)
         {
             AfterNextBeforeJwtWork += afterSubscriber;
         }
     }
 }
        /// <summary>
        /// Adds a new <see cref="JwtService" /> with the passed args as a Singleton to the passed <see cref="IServiceCollection" />
        /// </summary>
        /// <param name="serviceCollection">The current <see cref="IServiceCollection" /></param>
        /// <param name="args">The arguments to create the <see cref="JwtService" /> with</param>
        /// <returns>The current <see cref="IServiceCollection" /></returns>
        public static IServiceCollection AddJwtService(this IServiceCollection serviceCollection, JwtServiceArgs args)
        {
            JwtService service = new JwtService(args);

            serviceCollection.AddSingleton(service);

            return(serviceCollection);
        }
 /// <param name="next">The next middleware to run</param>
 /// <param name="beforeSubscriber">A Subscriber to the after jwt work, before next is called event</param>
 /// <param name="afterSubscriber">A Subscriber to the after next is called, before jwt work event</param>
 /// <param name="updateExpiration">If the middleware should update the JWT expiration, default <c>true</c></param>
 /// <param name="jwtExpiration">The Expiration for the JWT, default 1 hour</param>
 /// <param name="jwtService">The service to use in the middleware, default <see cref="JwtService" /> with no params</param>
 /// <param name="jwtIdentifier">The key that identifies the JWT, default <c>"StandardDotJwt"</c></param>
 public JwtServiceMiddleware(RequestDelegate next,
                             Func <JwtServiceMiddleware <T>, JwtEventArgs <T>, Task> beforeSubscriber,
                             Func <JwtServiceMiddleware <T>, AfterWorkJwtEventArgs <T>, Task> afterSubscriber,
                             bool updateExpiration = true, TimeSpan?jwtExpiration = null,
                             string jwtIdentifier  = null, JwtService jwtService  = null)
     : this(next, updateExpiration, jwtExpiration, jwtIdentifier, jwtService, new [] { beforeSubscriber }, new [] { afterSubscriber })
 {
 }
 /// <summary>
 /// Registers a JwtService to the IOC
 /// </summary>
 /// <typeparam name="T">The type for the JWT</typeparam>
 /// <param name="beforeSubscriber">A Subscriber to the after jwt work, before next is called event</param>
 /// <param name="afterSubscriber">A Subscriber to the after next is called, before jwt work event</param>
 /// <param name="updateExpiration">If the middleware should update the JWT expiration, default <c>true</c></param>
 /// <param name="jwtExpiration">The Expiration for the JWT, default 1 hour</param>
 /// <param name="jwtService">The service to use in the middleware, default <see cref="JwtService" /> with no params</param>
 /// <param name="jwtIdentifier">The key that identifies the JWT, default <c>"StandardDotJwt"</c></param>
 public static IApplicationBuilder UseJwtService <T>(this IApplicationBuilder builder,
                                                     Func <JwtServiceMiddleware <T>, JwtEventArgs <T>, Task> beforeSubscriber,
                                                     Func <JwtServiceMiddleware <T>, AfterWorkJwtEventArgs <T>, Task> afterSubscriber,
                                                     bool updateExpiration = true, TimeSpan?jwtExpiration = null,
                                                     string jwtIdentifier  = null, JwtService jwtService  = null)
     where T : IJwtToken
 {
     return(UseJwtService(builder, updateExpiration, jwtExpiration, jwtService, jwtIdentifier, new [] { beforeSubscriber }, new [] { afterSubscriber }));
 }
 /// <summary>
 /// Registers a JwtService to the IOC
 /// </summary>
 /// <typeparam name="T">The type for the JWT</typeparam>
 /// <param name="builder">The application builder to add the middleware to</param>
 /// <param name="updateExpiration">If the middleware should update the JWT expiration, default <c>true</c></param>
 /// <param name="jwtExpiration">The Expiration for the JWT, default 1 hour</param>
 /// <param name="jwtService">The service to use in the middleware, default <see cref="JwtService" /> with no params</param>
 /// <param name="jwtIdentifier">The key that identifies the JWT, default <c>"StandardDotJwt"</c></param>
 /// <param name="beforeSubscribers">Subscribers to the after jwt work, before next is called event</param>
 /// <param name="afterSubscribers">Subscribers to the after next is called, before jwt work event</param>
 /// <returns>The application builder with the JWT service added to it</returns>
 public static IApplicationBuilder UseJwtService <T>(this IApplicationBuilder builder, bool updateExpiration = true,
                                                     TimeSpan?jwtExpiration = null, JwtService jwtService = null, string jwtIdentifier = null,
                                                     IEnumerable <Func <JwtServiceMiddleware <T>, JwtEventArgs <T>, Task> > beforeSubscribers         = null,
                                                     IEnumerable <Func <JwtServiceMiddleware <T>, AfterWorkJwtEventArgs <T>, Task> > afterSubscribers = null)
     where T : IJwtToken
 {
     builder.UseMiddleware <JwtServiceMiddleware <T> >(updateExpiration, jwtExpiration, jwtIdentifier, jwtService,
                                                       beforeSubscribers, afterSubscribers);
     return(builder);
 }