Example #1
0
        public static async Task <TResult> InitializeAsync <TResult>(IConfigureIdentityServer configurationManager,
                                                                     HttpConfiguration config,
                                                                     Func <
                                                                         Func <IProvideAuthorization, IProvideAuthorization[]>, // onProvideAuthorization
                                                                         Func <IProvideAuthorization[]>,                        // onProvideNothing
                                                                         Func <string, IProvideAuthorization[]>,                // onFailure
                                                                         Task <IProvideAuthorization[]> > [] initializers,
                                                                     Expression <IntegrationActivityDelegate>[] activities,
                                                                     Expression <ConnectionActivityDelegate>[] connections,
                                                                     Func <TResult> onSuccess,
                                                                     Func <string, TResult> onFailure)
        {
            Library.configurationManager = configurationManager;



            AddExternalControllers <OpenIdResponseController>(config);
            //config.Routes.MapHttpRoute(name: "apple-app-links",
            //    routeTemplate: "apple-app-site-association",
            //    defaults: new { controller = "AppleAppSiteAssociation", id = RouteParameter.Optional });

            var credentialProvidersWithoutMethods = await initializers.Aggregate(
                (new IProvideAuthorization[] { }).ToTask(),
                async (providersTask, initializer) =>
            {
                var providers = await providersTask;
                return(await initializer(
                           (service) => providers.Append(service).ToArray(),
                           () => providers,
                           (why) => providers));
            });

            credentialProviders = credentialProvidersWithoutMethods
                                  .ToDictionary(
                credentialProvider =>
            {
                var methodName = credentialProvider.GetType().GetCustomAttribute <IntegrationNameAttribute, string>(
                    attr => attr.Name,
                    () => throw new Exception($"{credentialProvider.GetType().FullName} does not have IntegrationName attribute set"));
                return(methodName);
            },
                credentialProvider => credentialProvider);
            loginProviders = credentialProvidersWithoutMethods
                             .Where(credentialProvider => typeof(IProvideLogin).IsAssignableFrom(credentialProvider.GetType()))
                             .ToDictionary(
                credentialProvider => credentialProvider.GetType().GetCustomAttribute <IntegrationNameAttribute, string>(
                    attr => attr.Name,
                    () => throw new Exception($"{credentialProvider.GetType().FullName} does not have IntegrationName attribute set")),
                credentialProvider => (IProvideLogin)credentialProvider);
            managementProviders = credentialProviders
                                  .Where(credentialProvider => typeof(IProvideLoginManagement).IsAssignableFrom(credentialProvider.Value.GetType()))
                                  .ToDictionary(
                credentialProvider => credentialProvider.Key,
                credentialProvider => (IProvideLoginManagement)credentialProvider.Value);

            //var spaZipPath = System.Web.Hosting.HostingEnvironment.MapPath("~/Spa.zip");
            //var zipArchive = ZipFile.OpenRead(spaZipPath);

            //var lookupSpaFile = zipArchive.Entries
            //    .Select(
            //        entity => entity.FullName.PairWithValue(entity.Open().ToBytes()))
            //    .ToDictionary();

            integrationActivites = activities
                                   .Where(activity => activity.Body is System.Linq.Expressions.MethodCallExpression)
                                   .SelectMany(
                (activity) =>
            {
                var method = (activity.Body as System.Linq.Expressions.MethodCallExpression).Method;
                return(method.DeclaringType.GetInterfaces()
                       .FlatMap(
                           (conformsTo, nextItem, skipItem) => method.GetCustomAttribute(
                               (IntegrationNameAttribute integrationNameAttr) => nextItem(
                                   conformsTo.PairWithValue(integrationNameAttr.Name).PairWithValue(activity.Compile())),
                               () => skipItem()),
                           (IEnumerable <KeyValuePair <KeyValuePair <Type, string>, IntegrationActivityDelegate> > activityKvps) => activityKvps));
            })
                                   .GroupBy(activityKvp => activityKvp.Key.Key)
                                   .Select(
                grp => grp.Key.PairWithValue(
                    (IDictionary <string, IntegrationActivityDelegate[]>)grp
                    .Select(item => item.Key.Value.PairWithValue(item.Value))
                    .GroupBy(kvp => kvp.Key)
                    .Select(grpInner => grpInner.Key.PairWithValue(grpInner.SelectValues().ToArray()))
                    .ToDictionary()))
                                   .ToDictionary();

            ServiceConfiguration.connections = connections
                                               .Where(activity => activity.Body is System.Linq.Expressions.MethodCallExpression)
                                               .FlatMap(
                (activity, next, skip) =>
            {
                var method    = (activity.Body as System.Linq.Expressions.MethodCallExpression).Method;
                var paramType = method.GetParameters().First().ParameterType;
                return(method.GetCustomAttribute(
                           (IntegrationNameAttribute integrationNameAttr) =>
                           next(integrationNameAttr.Name.PairWithValue(paramType).PairWithValue(activity.Compile())),
                           () => skip()));
            },
                (IEnumerable <KeyValuePair <KeyValuePair <string, Type>, ConnectionActivityDelegate> > operations) =>
            {
                try
                {
                    var conns = operations
                                .GroupBy(activityKvp => activityKvp.Key.Key)
                                .Select(
                        grp =>
                    {
                        var connActivitiesByType = (IDictionary <Type, ConnectionActivityDelegate[]>)grp
                                                   .Select(item => item.Key.Value.PairWithValue(item.Value))
                                                   .GroupBy(kvp => kvp.Key)
                                                   .Select(grpInner => grpInner.Key.PairWithValue(grpInner.SelectValues().ToArray()))
                                                   .ToDictionary();
                        return(grp.Key.PairWithValue(connActivitiesByType));
                    })
                                .ToDictionary();
                    return(conns);
                } catch (Exception ex)
                {
                    throw ex;
                }
            });

            return(onSuccess());
        }
        //internal static Dictionary<CredentialValidationMethodTypes, IProvideAccess> accessProviders =
        //    default(Dictionary<CredentialValidationMethodTypes, IProvideAccess>);

        public static async Task <TResult> InitializeAsync <TResult>(IConfigureIdentityServer configurationManager,
                                                                     HttpConfiguration config,
                                                                     Func <
                                                                         Func <IProvideAuthorization, IProvideAuthorization[]>, // onProvideAuthorization
                                                                         Func <IProvideAuthorization[]>,                        // onProvideNothing
                                                                         Func <string, IProvideAuthorization[]>,                // onFailure
                                                                         Task <IProvideAuthorization[]> > [] initializers,
                                                                     Func <TResult> onSuccess,
                                                                     Func <string, TResult> onFailure)
        {
            Library.configurationManager = configurationManager;
            //config.AddExternalControllers<SessionServer.Api.Controllers.OpenIdResponseController>();
            AddExternalControllers <Api.Controllers.OpenIdResponseController>(config);
            //return InitializeAsync(audience, configurationEndpoint, onSuccess, onFailed);
            config.Routes.MapHttpRoute(name: "apple-app-links",
                                       routeTemplate: "apple-app-site-association",
                                       defaults: new { controller = "AppleAppSiteAssociation", id = RouteParameter.Optional });

            var dataContext = new Persistence.DataContext(Configuration.AppSettings.Storage);
            var credentialProvidersWithoutMethods = await initializers.Aggregate(
                (new IProvideAuthorization[] { }).ToTask(),
                async (providersTask, initializer) =>
            {
                var providers = await providersTask;
                return(await initializer(
                           (service) => providers.Append(service).ToArray(),
                           () => providers,
                           (why) => providers));
            });

            credentialProviders = credentialProvidersWithoutMethods
                                  .ToDictionary(
                credentialProvider => credentialProvider.Method,
                credentialProvider => credentialProvider);
            loginProviders = credentialProvidersWithoutMethods
                             .Where(credentialProvider => typeof(IProvideLogin).IsAssignableFrom(credentialProvider.GetType()))
                             .ToDictionary(
                credentialProvider => credentialProvider.Method,
                credentialProvider => (IProvideLogin)credentialProvider);
            managementProviders = credentialProvidersWithoutMethods
                                  .Where(credentialProvider => typeof(IProvideLoginManagement).IsAssignableFrom(credentialProvider.GetType()))
                                  .ToDictionary(
                credentialProvider => credentialProvider.Method,
                credentialProvider => (IProvideLoginManagement)credentialProvider);
            //accessProviders = credentialProvidersWithoutMethods
            //    .Where(credentialProvider => typeof(IProvideAccess).IsAssignableFrom(credentialProvider.GetType()))
            //    .ToDictionary(
            //        credentialProvider => credentialProvider.Method,
            //        credentialProvider => (IProvideAccess)credentialProvider);
            tokenProviders = credentialProvidersWithoutMethods
                             .Where(credentialProvider => typeof(IProvideToken).IsAssignableFrom(credentialProvider.GetType()))
                             .ToDictionary(
                credentialProvider => credentialProvider.Method,
                credentialProvider => (IProvideToken)credentialProvider);

            //var spaZipPath = System.Web.Hosting.HostingEnvironment.MapPath("~/Spa.zip");
            //var zipArchive = ZipFile.OpenRead(spaZipPath);

            //var lookupSpaFile = zipArchive.Entries
            //    .Select(
            //        entity => entity.FullName.PairWithValue(entity.Open().ToBytes()))
            //    .ToDictionary();

            return(onSuccess());
        }