Example #1
0
 /// <summary>
 /// Create an instance of the result object
 /// </summary>
 /// <param name="identity">Assigned to the Identity property. May be null.</param>
 /// <param name="extra">Assigned to the Extra property. An empty Extra instance is created if needed.</param>
 /// <param name="description">Assigned to the Description property. An empty AuthenticationDescription instance is created if needed.</param>
 public AuthenticateResult(IIdentity identity, IDictionary<string, string> extra, IDictionary<string, object> description)
 {
     if (identity != null)
     {
         Identity = identity as ClaimsIdentity ?? new ClaimsIdentity(identity);
     }
     Extra = new AuthenticationExtra(extra);
     Description = new AuthenticationDescription(description);
 }
Example #2
0
        public Task Invoke(IDictionary<string, object> env)
        {
            var request = new OwinRequest(env);
            var response = new OwinResponse(env);

            // The forms auth module has a bug where it null refs on a null Extra
            var headers = request.Get<IDictionary<string, string[]>>(Owin.Types.OwinConstants.RequestHeaders);

            var cookies = request.GetCookies();
            string cookieValue;
            if (cookies != null && cookies.TryGetValue("jabbr.id", out cookieValue))
            {
                AuthenticationTicket ticket = _ticketHandler.Unprotect(cookieValue);
                if (ticket != null && ticket.Extra == null)
                {
                    var extra = new AuthenticationExtra();
                    extra.IsPersistent = true;
                    extra.IssuedUtc = DateTime.UtcNow;
                    extra.ExpiresUtc = DateTime.UtcNow.AddDays(30);

                    var newTicket = new AuthenticationTicket(ticket.Identity, extra);

                    var cookieBuilder = new StringBuilder();
                    foreach (var cookie in cookies)
                    {
                        string value = cookie.Value;

                        if (cookie.Key == "jabbr.id")
                        {
                            // Create a new ticket preserving the identity of the user
                            // so they don't get logged out
                            value = _ticketHandler.Protect(newTicket);
                            response.AddCookie("jabbr.id", value, new CookieOptions
                            {
                                Expires = extra.ExpiresUtc.Value.UtcDateTime,
                                HttpOnly = true
                            });
                        }

                        if (cookieBuilder.Length > 0)
                        {
                            cookieBuilder.Append(";");
                        }

                        cookieBuilder.Append(cookie.Key)
                                     .Append("=")
                                     .Append(Uri.EscapeDataString(value));
                    }

                    headers["Cookie"] = new[] { cookieBuilder.ToString() };
                }
            }

            return _next(env);
        }
        public AuthenticationResponseGrant(ClaimsPrincipal principal, AuthenticationExtra extra)
        {
            if (principal == null)
            {
                throw new ArgumentNullException("principal");
            }

            Principal = principal;
            Identity = principal.Identities.FirstOrDefault();
            Extra = extra;
        }
Example #4
0
        public Task Invoke(IDictionary<string, object> env)
        {
            var request = new OwinRequest(env);
            var response = new OwinResponse(env);

            string cookieValue = request.Cookies["jabbr.id"];
            if (!String.IsNullOrEmpty(cookieValue))
            {
                AuthenticationTicket ticket = _ticketHandler.Unprotect(cookieValue);
                if (ticket != null && ticket.Extra == null)
                {
                    var extra = new AuthenticationExtra();
                    extra.IsPersistent = true;
                    extra.IssuedUtc = DateTime.UtcNow;
                    extra.ExpiresUtc = DateTime.UtcNow.AddDays(30);

                    var newTicket = new AuthenticationTicket(ticket.Identity, extra);

                    var cookieBuilder = new StringBuilder();
                    foreach (var cookie in request.Cookies)
                    {
                        string value = cookie.Value;

                        if (cookie.Key == "jabbr.id")
                        {
                            // Create a new ticket preserving the identity of the user
                            // so they don't get logged out
                            value = _ticketHandler.Protect(newTicket);
                            response.Cookies.Append("jabbr.id", value, new CookieOptions
                            {
                                Expires = extra.ExpiresUtc.Value.UtcDateTime,
                                HttpOnly = true
                            });
                        }

                        if (cookieBuilder.Length > 0)
                        {
                            cookieBuilder.Append(";");
                        }

                        cookieBuilder.Append(cookie.Key)
                                     .Append("=")
                                     .Append(Uri.EscapeDataString(value));
                    }

                    request.Headers["Cookie"] = cookieBuilder.ToString();
                }
            }

            return _next(env);
        }
        public void SendAsync_LeavesAuthenticationChallenges_WhenExistingAuthenticationTypesIsNonEmpty()
        {
            // Arrange
            IHostPrincipalService principalService = CreateStubPrincipalService();
            HttpMessageHandler inner = CreateStubHandler();
            HttpMessageHandler handler = CreateProductUnderTest(principalService, inner);
            IDictionary<string, object> environment = CreateOwinEnvironment();
            OwinResponse owinResponse = new OwinResponse(environment);
            AuthenticationExtra extraWrapper = new AuthenticationExtra();
            string[] expectedAuthenticationTypes = new string[] { "Existing" };
            IDictionary<string, string> expectedExtra = extraWrapper.Properties;
            owinResponse.AuthenticationResponseChallenge = new AuthenticationResponseChallenge(
                expectedAuthenticationTypes, extraWrapper);

            using (HttpRequestMessage request = CreateRequestWithOwinEnvironment(environment))
            {
                // Act
                handler.SendAsync(request, CancellationToken.None);
            }

            // Assert
            AuthenticationResponseChallenge challenge = owinResponse.AuthenticationResponseChallenge;
            Assert.NotNull(challenge);
            Assert.Same(expectedAuthenticationTypes, challenge.AuthenticationTypes);
            AuthenticationExtra actualExtraWrapper = challenge.Extra;
            Assert.NotNull(actualExtraWrapper);
            Assert.Same(expectedExtra, actualExtraWrapper.Properties);
        }
 public static void SignIn(this IAuthenticationManager authenticationManager, ClaimsIdentity identity)
 {
     var extra = new AuthenticationExtra();
     authenticationManager.SignIn(extra, identity);
 }
 public AuthenticationTicket(ClaimsIdentity identity, IDictionary<string, string> extra)
 {
     Identity = identity;
     Extra = new AuthenticationExtra(extra);
 }
 public AuthenticationTicket(ClaimsIdentity identity, AuthenticationExtra extra)
 {
     Identity = identity;
     Extra = extra;
 }
Example #9
0
        /// <summary></summary>
        /// <param name="context"></param>
        /// <param name="authenticationTypes"></param>
        /// <param name="extra"></param>
        public static void Challenge(this HttpContext context, string[] authenticationTypes, AuthenticationExtra extra)
        {
            if (context == null)
            {
                throw new ArgumentNullException("context");
            }
            if (extra == null)
            {
                throw new ArgumentNullException("extra");
            }

            OwinResponse response = GetOwinResponse(context);
            response.Challenge(authenticationTypes, extra);
        }
 public AuthenticationResponseGrant(ClaimsIdentity identity, AuthenticationExtra extra)
 {
     Principal = new ClaimsPrincipal(identity);
     Identity = identity;
     Extra = extra;
 }
Example #11
0
 public void SignIn(AuthenticationExtra extra, params ClaimsIdentity[] identities)
 {
     _response.Grant(new ClaimsPrincipal(identities), extra);
 }
Example #12
0
 public void Challenge(AuthenticationExtra extra, params string[] authenticationTypes)
 {
     _response.Challenge(authenticationTypes, extra);
 }
Example #13
0
 public void Challenge(string[] authenticationTypes, AuthenticationExtra extra)
 {
     StatusCode = 401;
     AuthenticationResponseChallenge = new AuthenticationResponseChallenge(authenticationTypes, extra);
 }
Example #14
0
 public void Grant(ClaimsPrincipal principal, AuthenticationExtra extra)
 {
     AuthenticationResponseGrant = new AuthenticationResponseGrant(principal, extra);
 }
Example #15
0
 public void Grant(ClaimsIdentity identity, AuthenticationExtra extra)
 {
     AuthenticationResponseGrant = new AuthenticationResponseGrant(identity, extra);
 }
        private static AuthenticationResponseChallenge AddChallengeAuthenticationType(
            AuthenticationResponseChallenge challenge, string authenticationType)
        {
            Contract.Assert(authenticationType != null);

            List<string> authenticationTypes = new List<string>();
            AuthenticationExtra extra;

            if (challenge != null)
            {
                string[] currentAuthenticationTypes = challenge.AuthenticationTypes;

                if (currentAuthenticationTypes != null)
                {
                    authenticationTypes.AddRange(currentAuthenticationTypes);
                }

                extra = challenge.Extra;
            }
            else
            {
                extra = new AuthenticationExtra();
            }

            authenticationTypes.Add(authenticationType);

            return new AuthenticationResponseChallenge(authenticationTypes.ToArray(), extra);
        }
 public AuthenticationResponseChallenge(string[] authenticationTypes, AuthenticationExtra extra)
 {
     AuthenticationTypes = authenticationTypes;
     Extra = extra;
 }
        /// <summary></summary>
        /// <param name="context"></param>
        /// <param name="authenticationTypes"></param>
        public static void Challenge(this HttpContextBase context, string authenticationType, AuthenticationExtra extra)
        {
            if (context == null)
            {
                throw new ArgumentNullException("context");
            }

            OwinResponse response = GetOwinResponse(context);
            response.Challenge(new[] { authenticationType }, extra);
        }
Example #19
0
        /// <summary></summary>
        /// <param name="context"></param>
        /// <param name="user"></param>
        /// <param name="extra"></param>
        public static void SignIn(this HttpContext context, ClaimsPrincipal user, AuthenticationExtra extra)
        {
            if (context == null)
            {
                throw new ArgumentNullException("context");
            }

            OwinResponse response = GetOwinResponse(context);
            response.Grant(user, extra);
        }
 /// <summary></summary>
 /// <param name="context"></param>
 /// <param name="authenticationType"> </param>
 /// <param name="claims"></param>
 /// <param name="nameClaimType"></param>
 /// <param name="roleClaimType"></param>
 /// <param name="isPersistent"></param>
 /// <exception cref="ArgumentNullException"></exception>
 public static void SignIn(this HttpContext context, string authenticationType, IEnumerable<Claim> claims, string nameClaimType, string roleClaimType, bool isPersistent)
 {
     if (authenticationType == null)
     {
         throw new ArgumentNullException("authenticationType");
     }
     if (claims == null)
     {
         throw new ArgumentNullException("claims");
     }
     if (nameClaimType == null)
     {
         throw new ArgumentNullException("nameClaimType");
     }
     if (roleClaimType == null)
     {
         throw new ArgumentNullException("roleClaimType");
     }
     var extra = new AuthenticationExtra { IsPersistent = isPersistent };
     context.SignIn(new ClaimsPrincipal(new ClaimsIdentity(claims, authenticationType, nameClaimType, roleClaimType)), extra);
 }