Esempio n. 1
0
 private void ProcessStuntmanServerResponse(
     StuntmanServerResponse stuntmanServerResponse,
     string source)
 {
     foreach (var user in stuntmanServerResponse.Users)
     {
         AddUser(user, source);
     }
 }
Esempio n. 2
0
        /// <summary>
        /// Enable Stuntman on this application.
        /// </summary>
        public static void UseStuntman(this IAppBuilder app, StuntmanOptions options)
        {
            if (options.AllowBearerTokenAuthentication)
            {
                app.UseOAuthBearerAuthentication(new OAuthBearerAuthenticationOptions()
                {
                    AuthenticationType = Constants.StuntmanAuthenticationType,
                    Provider           = new StuntmanOAuthBearerProvider(options),
                    AccessTokenFormat  = new StuntmanOAuthAccessTokenFormat()
                });
            }

            if (options.AllowCookieAuthentication)
            {
                app.UseCookieAuthentication(new CookieAuthenticationOptions
                {
                    AuthenticationType = Constants.StuntmanAuthenticationType,
                    LoginPath          = new PathString(options.SignInUri),
                    LogoutPath         = new PathString(options.SignOutUri),
                    ReturnUrlParameter = Constants.StuntmanOptions.ReturnUrlQueryStringKey,
                });

                app.Map(options.SignInUri, signin =>
                {
                    signin.Use(async(context, next) =>
                    {
                        var claims = new List <Claim>();

                        var overrideUserId = context.Request.Query[Constants.StuntmanOptions.OverrideQueryStringKey];

                        if (string.IsNullOrWhiteSpace(overrideUserId))
                        {
                            await next.Invoke();

                            IAppBuilderShared.ShowLoginUI(context, options);
                        }
                        else
                        {
                            var user = options.Users
                                       .Where(x => x.Id == overrideUserId)
                                       .FirstOrDefault();

                            if (user == null)
                            {
                                context.Response.StatusCode = 404;
                                await context.Response.WriteAsync(
                                    $"options provided does not include the requested '{overrideUserId}' user.");

                                return;
                            }

                            claims.Add(new Claim(user.NameClaimType, user.Name));
                            claims.AddRange(user.Claims);

                            var identity = new ClaimsIdentity(claims, Constants.StuntmanAuthenticationType, user.NameClaimType, user.RoleClaimType);

                            var authManager = context.Authentication;

                            authManager.SignIn(identity);

                            await next.Invoke();
                        }
                    });

                    IAppBuilderShared.RedirectToReturnUrl(signin);
                });

                app.Map(options.SignOutUri, signout =>
                {
                    signout.Use((context, next) =>
                    {
                        var authManager = context.Authentication;
                        authManager.SignOut(Constants.StuntmanAuthenticationType);

                        return(next.Invoke());
                    });

                    IAppBuilderShared.RedirectToReturnUrl(signout);
                });
            }

            if (options.ServerEnabled)
            {
                app.Map(options.ServerUri, server =>
                {
                    server.Use(async(context, next) =>
                    {
                        var response = new StuntmanServerResponse {
                            Users = options.Users
                        };
                        var json = JsonConvert.SerializeObject(response);

                        context.Response.ContentType = "application/json";
                        await context.Response.WriteAsync(json);
                    });
                });
            }
        }