public static TinyIoCContainer UseHttpClientFactory(this TinyIoCContainer self, NancyContext context)
        {
            var correlationToken =
                context.GetOwinEnvironment()?["correlationToken"] as string;
            object key = null;

            context.GetOwinEnvironment()?.TryGetValue(OwinConstants.RequestUser, out key);
            var principal = key as ClaimsPrincipal;
            var idToken   = principal?.FindFirst("id_token");

            self.Register <IHttpClientFactory>(new HttpClientFactory(TokenUrl, ClientName, ClientSecret, correlationToken ?? "", idToken?.Value ?? ""));
            return(self);
        }
Exemplo n.º 2
0
        public static AuthenticationManager GetAuthenticationManager(this NancyContext context, bool throwOnNull = false)
        {
            var environment = context.GetOwinEnvironment();

            if (environment == null && throwOnNull)
            {
                throw new InvalidOperationException("OWIN environment not found. Is this an owin application?");
            }

            try
            {
                var httpcontext = (Microsoft.AspNetCore.Http.HttpContext)environment["Microsoft.AspNetCore.Http.HttpContext"];

                return(httpcontext.Authentication);
            }
            catch (KeyNotFoundException)
            {
                try
                {
                    var defaultcontext = (Microsoft.AspNetCore.Http.DefaultHttpContext)environment["Microsoft.AspNetCore.Http.DefaultHttpContext"];

                    return(defaultcontext.Authentication);
                }
                catch (KeyNotFoundException)
                {
                    return(null);
                }
            }
        }
Exemplo n.º 3
0
        public static TenantInstance GetTenantInstance(this NancyContext context)
        {
            var    owinEnvironment = context.GetOwinEnvironment();
            object tenant;

            return(owinEnvironment.TryGetValue(Constants.OwinCurrentTenant, out tenant) ? (TenantInstance)tenant : null);
        }
Exemplo n.º 4
0
        => NancyInternalConfiguration.WithOverrides(builder => builder.StatusCodeHandlers.Clear()); // when 500 or 404, no default HTML message will be generated!

        protected override void RequestStartup(TinyIoCContainer container, IPipelines pipelines, NancyContext context)
        {
            base.RequestStartup(container, pipelines, context);
            var correlationToken = context.GetOwinEnvironment()["correlationToken"] as string;

            container.Register <IHttpClientFactory>(new HttpClientFactory(correlationToken));
        }
Exemplo n.º 5
0
        /// <summary>
        /// Pipeline for a web api request
        /// </summary>
        /// <param name="container"></param>
        /// <param name="pipelines"></param>
        /// <param name="context"></param>
        protected override void RequestStartup(TinyIoCContainer container, IPipelines pipelines, NancyContext context)
        {
            base.RequestStartup(container, pipelines, context);
            //CORS
            pipelines.AfterRequest.AddItemToEndOfPipeline((ctx) =>
            {
                ctx.Response.WithHeader("Access-Control-Allow-Origin", "*")
                .WithHeader("Access-Control-Allow-Methods", "POST,GET,DELETE,PUT,OPTIONS")
                .WithHeader("Access-Control-Allow-Headers", "Accept, Origin, Content-type");
            });

            //Custom error handling
            pipelines.OnError.AddItemToEndOfPipeline((ctx, exc) =>
            {
                return(StatusCodeHandler.GetStatusCode(ctx, exc));
            });

            //Authenticathion
            var owinEnvironment = context.GetOwinEnvironment();
            var principal       = owinEnvironment?["server.User"] as ClaimsPrincipal;

            if (principal == null)
            {
                return;
            }
            //context.CurrentUser = principal;
        }
        protected override void RequestStartup(TinyIoCContainer container, IPipelines pipelines, NancyContext context)
        {
            base.RequestStartup(container, pipelines, context);
            var owinEnvironment = context.GetOwinEnvironment();
            var principal       = owinEnvironment[OwinConstants.RequestUser] as ClaimsPrincipal;

            context.CurrentUser = principal;
        }
        public static TinyIoCContainer UseHttpClientFactory(this TinyIoCContainer self, NancyContext context, IdentityServerConfidentialClientSettings settings)
        {
            var correlationToken = context.GetOwinEnvironment()?[Constants.FabricLogContextProperties.CorrelationTokenContextName] as string;

            self.Register <IHttpClientFactory>(new HttpClientFactory(settings.Authority, settings.ClientId, settings.ClientSecret,
                                                                     correlationToken ?? "", ""));
            return(self);
        }
Exemplo n.º 8
0
        public static TinyIoCContainer UseHttpClient(this TinyIoCContainer self, NancyContext context)
        {
            // reads the end user from the OWIN environment
            object key = null;

            context.GetOwinEnvironment()?.TryGetValue(OwinConstants.RequestUser, out key);
            // get the end user's identity token from the user object
            var principal = key as ClaimsPrincipal;
            var idToken   = principal?.FindFirst("id_token");

            // reads the correlation token from OWIN environment
            var correlationToken = context.GetOwinEnvironment()?["correlationToken"] as string;

            // Registers the UserviceHttpClient as a per-request dependency in Nancy’s container
            self.Register <IHttpClient>(new UserviceHttpClient(TokenUrl, ClientId, ClientSecret, correlationToken ?? "", idToken?.Value ?? ""));

            return(self);
        }
Exemplo n.º 9
0
        private static void SignInOwinUser(NancyContext context)
        {
            var principal = new OwinContext(context.GetOwinEnvironment() ?? new Dictionary <string, object>()).Authentication.User;

            if (principal != null && principal.Identity.IsAuthenticated)
            {
                context.CurrentUser = new DemoUser(principal.FindFirst(c => c.Type == ClaimTypes.Name).Value, principal.Claims.Select(c => string.Join(":", c.Type, c.Value)));
            }
        }
Exemplo n.º 10
0
        /// <summary>
        /// Called on each request
        /// </summary>
        public void Initialize(IPipelines pipelines, NancyContext context)
        {
            const string userIdKey = "pos-end-user";
            var          owinEnv   = context.GetOwinEnvironment();

            if (owinEnv.ContainsKey(userIdKey))
            {
                context.CurrentUser = owinEnv[userIdKey] as ClaimsPrincipal;
            }
        }
Exemplo n.º 11
0
        protected override void RequestStartup(TinyIoCContainer container, IPipelines pipelines, NancyContext context)
        {
            base.RequestStartup(container, pipelines, context);
            var principal = context.GetOwinEnvironment()[OwinConstants.RequestUser] as ClaimsPrincipal;

            context.CurrentUser = principal;
            var appConfig = container.Resolve <IAppConfiguration>();

            container.UseHttpClientFactory(context, appConfig.IdentityServerConfidentialClientSettings);
        }
Exemplo n.º 12
0
        protected override void RequestStartup(TinyIoCContainer container, IPipelines pipelines, NancyContext context)
        {
            base.RequestStartup(container, pipelines, context);
            var owinEnvironment = context.GetOwinEnvironment();
            var user            = owinEnvironment["server.User"] as ClaimsPrincipal;

            context.CurrentUser = new DemoUserIdentity()
            {
                UserName = user.Identity.Name,
                Claims   = user.Claims.Select(x => x.Value)
            };
        }
        public static HttpContext GetHttpContext(this NancyContext context)
        {
            var key = typeof(HttpContext).FullName;

            var environment = context.GetOwinEnvironment();

            if (environment.TryGetValue(key, out var httpContext))
            {
                return(httpContext as HttpContext);
            }

            return(null);
        }
Exemplo n.º 14
0
        protected override void RequestStartup(TinyIoCContainer container, IPipelines pipelines, NancyContext context)
        {
            base.RequestStartup(container, pipelines, context);
            var owinEnvironment = context.GetOwinEnvironment();
            var user            = owinEnvironment["server.User"] as ClaimsPrincipal;

            if (user != null)
            {
                context.CurrentUser = new DemoUserIdentity()
                {
                    UserName = user.Identity.Name,
                    Claims   = user.Claims.Where(x => x.Type == "http://schemas.microsoft.com/ws/2008/06/identity/claims/role").Select(x => x.Value)
                };
            }
        }
Exemplo n.º 15
0
        public static IOwinContext GetOwinContext(this NancyContext context)
        {
            if (context == null)
            {
                throw new ArgumentNullException("context");
            }

            var environment = context.GetOwinEnvironment();

            if (environment == null)
            {
                throw new InvalidOperationException("The OWIN environment cannot be extracted from NancyContext");
            }

            return(new OwinContext(environment));
        }
        protected override void RequestStartup(TinyIoCContainer container, IPipelines pipelines, NancyContext context)
        {
            base.RequestStartup(container, pipelines, context);
            var owinEnvironment = context.GetOwinEnvironment();

            if (owinEnvironment != null && owinEnvironment.ContainsKey(OwinConstants.RequestUser))
            {
                var principal = owinEnvironment[OwinConstants.RequestUser] as ClaimsPrincipal;
                context.CurrentUser = principal;
            }

            pipelines.AfterRequest.AddItemToEndOfPipeline(
                c => {
                c.Response.Headers["Pragma"]            = "no-cache";
                c.Response.Headers["Expires"]           = "Sat, 01 Jan 2000 00:00:00 GMT";
                c.Response.Headers["If-Modified-Since"] = "0";
            });
        }
Exemplo n.º 17
0
        protected override void RequestStartup(TinyIoCContainer container, IPipelines pipelines, NancyContext context)
        {
            base.RequestStartup(container, pipelines, context);


            var owinEnvironment = context.GetOwinEnvironment();
            var user            = owinEnvironment["server.User"] as ClaimsPrincipal;

            if (user != null && user.Claims.Any(x => x.Type == "http://schemas.xmlsoap.org/ws/2005/05/identity/claims/sid"))
            {
                context.CurrentUser = new UserIdentity()
                {
                    Id       = int.Parse(user.Claims.FirstOrDefault(x => x.Type == "http://schemas.xmlsoap.org/ws/2005/05/identity/claims/sid").Value),
                    UserName = user.Claims.FirstOrDefault(x => x.Type == "http://schemas.xmlsoap.org/ws/2005/05/identity/claims/emailaddress").Value,
                    Claims   = user.Claims.Where(x => x.Type == "http://schemas.microsoft.com/ws/2008/06/identity/claims/role").Select(x => x.Value),
                };
            }
        }
Exemplo n.º 18
0
        private static Response FlowPrincipal(NancyContext context)
        {
            var env = context.GetOwinEnvironment();

            if (env == null)
            {
                return(null);
            }

            var principal = env.Get <IPrincipal>("server.User") as ClaimsPrincipal;

            if (principal != null)
            {
                context.CurrentUser = new TheBenchUser(new TheBenchPrincipal(principal));
            }

            return(null);
        }
Exemplo n.º 19
0
        protected override void RequestStartup(IContainer container, IPipelines pipelines, NancyContext context)
        {
            base.RequestStartup(container, pipelines, context);
            var owinEnvironment = context.GetOwinEnvironment();

            if (!owinEnvironment.ContainsKey("server.User"))
            {
                return;
            }

            var user = owinEnvironment["server.User"] as ClaimsPrincipal;

            if (user != null && user.Identity.IsAuthenticated)
            {
                context.CurrentUser =
                    new CustomUserIdentity(Guid.Parse(user.Claims.Single(x => x.Type == ClaimTypes.NameIdentifier).Value),
                                           user.Identity.Name, user.Claims.Select(x => x.Value));
            }
        }
Exemplo n.º 20
0
        protected override void RequestStartup(TinyIoCContainer container, IPipelines pipelines, NancyContext context)
        {
            base.RequestStartup(container, pipelines, context);

            var owinEnvironment = context.GetOwinEnvironment();

            var principal = owinEnvironment?["server.User"] as ClaimsPrincipal;

            if (principal == null)
            {
                return;
            }

            var userName = principal.Identity.Name;
            var claims   = principal.Claims.Where(
                o => o.Type == ClaimTypes.Role)
                           .Select(o => o.Value);

            context.CurrentUser = new ValidatedUser(userName, claims);
        }
 public static AuthenticationService <TAccount> GetAuthenticationService <TAccount>(this NancyContext ctx)
     where TAccount : UserAccount
 {
     return(ctx.GetOwinEnvironment().GetAuthenticationService <TAccount>());
 }
Exemplo n.º 22
0
 public static T GetFromOwinContext <T>(this NancyContext context, string typeName)
 {
     return((T)context.GetOwinEnvironment()["AspNet.Identity.Owin:" + typeName]); // get by recompiling Microsoft.Aspnet.Identity.Owin
 }
Exemplo n.º 23
0
 public static T GetFromOwinContext <T>(this NancyContext context)
 {
     return((T)context.GetOwinEnvironment()["AspNet.Identity.Owin:" +
                                            typeof(T).AssemblyQualifiedName]); // get by recompiling Microsoft.Aspnet.Identity.Owin
 }
Exemplo n.º 24
0
 public void Initialize(IPipelines pipelines, NancyContext context) =>
 context.CurrentUser = context.GetOwinEnvironment()["pos-end-user"] as ClaimsPrincipal;