示例#1
0
        /// <summary>Post this message.</summary>
        ///
        /// <exception cref="NotFound"> Thrown when a not found error condition occurs.</exception>
        /// <exception cref="HttpError">Thrown when a HTTP error error condition occurs.</exception>
        ///
        /// <param name="request">.</param>
        ///
        /// <returns>An object.</returns>
        public virtual object Post(Auth request)
        {
            AssertAuthProviders();

            if (ValidateFn != null)
            {
                var validationResponse = ValidateFn(this, HttpMethods.Get, request);
                if (validationResponse != null)
                {
                    return(validationResponse);
                }
            }

            if (request.RememberMe.HasValue)
            {
                var opt = request.RememberMe.GetValueOrDefault(false)
                    ? SessionOptions.Permanent
                    : SessionOptions.Temporary;

                base.RequestContext.Get <IHttpResponse>()
                .AddSessionOptions(base.RequestContext.Get <IHttpRequest>(), opt);
            }

            var provider    = request.provider ?? AuthProviders[0].Provider;
            var oAuthConfig = GetAuthProvider(provider);

            if (oAuthConfig == null)
            {
                throw HttpError.NotFound("No configuration was added for OAuth provider '{0}'".Fmt(provider));
            }

            if (request.provider == LogoutAction)
            {
                return(oAuthConfig.Logout(this, request));
            }

            var session = this.GetSession();

            var isHtml = base.RequestContext.ResponseContentType.MatchesContentType(ContentType.Html);

            try
            {
                var response = Authenticate(request, provider, session, oAuthConfig);

                // The above Authenticate call may end an existing session and create a new one so we need
                // to refresh the current session reference.
                session = this.GetSession();

                var referrerUrl = request.Continue
                                  ?? session.ReferrerUrl
                                  ?? this.RequestContext.GetHeader("Referer")
                                  ?? oAuthConfig.CallbackUrl;

                var alreadyAuthenticated = response == null;
                response = response ?? new AuthResponse {
                    UserName    = session.UserAuthName,
                    SessionId   = session.Id,
                    ReferrerUrl = referrerUrl,
                };

                if (isHtml)
                {
                    if (alreadyAuthenticated)
                    {
                        return(this.Redirect(referrerUrl.AddHashParam("s", "0")));
                    }

                    if (!(response is IHttpResult) && !String.IsNullOrEmpty(referrerUrl))
                    {
                        return(new HttpResult(response)
                        {
                            Location = referrerUrl
                        });
                    }
                }

                return(response);
            }
            catch (HttpError ex)
            {
                var errorReferrerUrl = this.RequestContext.GetHeader("Referer");
                if (isHtml && errorReferrerUrl != null)
                {
                    errorReferrerUrl = errorReferrerUrl.SetQueryParam("error", ex.Message);
                    return(HttpResult.Redirect(errorReferrerUrl));
                }

                throw;
            }
        }
示例#2
0
 /// <summary>Options the given request.</summary>
 ///
 /// <param name="request">.</param>
 public virtual void Options(Auth request)
 {
 }
示例#3
0
 /// <summary>Gets the given request.</summary>
 ///
 /// <param name="request">.</param>
 ///
 /// <returns>An object.</returns>
 public virtual object Get(Auth request)
 {
     return(Post(request));
 }
 /// <summary>The entry point for all AuthProvider providers. Runs inside the AuthService so exceptions are treated normally. Overridable so you can provide your own Auth implementation.</summary>
 ///
 /// <param name="authService">The authentication service.</param>
 /// <param name="session">    The session.</param>
 /// <param name="request">    The request.</param>
 ///
 /// <returns>An object.</returns>
 public override object Authenticate(IServiceBase authService, IAuthSession session, Auth request)
 {
     //new CredentialsAuthValidator().ValidateAndThrow(request);
     return(Authenticate(authService, session, request.UserName, request.Password));
 }