コード例 #1
0
ファイル: AuthConfig.cs プロジェクト: xamarin/ServiceStack
        public virtual object Authenticate(IServiceBase service, Auth request, IAuthSession session,
                                           IOAuthTokens tokens, OAuthAuthorizer oAuth)
        {
            //Default oAuth logic based on Twitter's oAuth workflow
            if (!tokens.RequestToken.IsNullOrEmpty() && !request.oauth_token.IsNullOrEmpty())
            {
                oAuth.RequestToken          = tokens.RequestToken;
                oAuth.RequestTokenSecret    = tokens.RequestTokenSecret;
                oAuth.AuthorizationToken    = request.oauth_token;
                oAuth.AuthorizationVerifier = request.oauth_verifier;

                if (oAuth.AcquireAccessToken())
                {
                    tokens.AccessToken       = oAuth.AccessToken;
                    tokens.AccessTokenSecret = oAuth.AccessTokenSecret;
                    session.OnAuthenticated(service, tokens, oAuth.AuthInfo);
                    service.SaveSession(session);

                    //Haz access!
                    return(service.Redirect(session.ReferrerUrl.AddHashParam("s", "1")));
                }

                //No Joy :(
                tokens.RequestToken       = null;
                tokens.RequestTokenSecret = null;
                service.SaveSession(session);
                return(service.Redirect(session.ReferrerUrl.AddHashParam("f", "AccessTokenFailed")));
            }
            if (oAuth.AcquireRequestToken())
            {
                tokens.RequestToken       = oAuth.RequestToken;
                tokens.RequestTokenSecret = oAuth.RequestTokenSecret;
                service.SaveSession(session);

                //Redirect to OAuth provider to approve access
                return(service.Redirect(this.AuthorizeUrl
                                        .AddQueryParam("oauth_token", tokens.RequestToken)
                                        .AddQueryParam("oauth_callback", session.ReferrerUrl)));
            }

            return(service.Redirect(session.ReferrerUrl.AddHashParam("f", "RequestTokenFailed")));
        }
コード例 #2
0
ファイル: OAuthConfig.cs プロジェクト: benaston/ServiceStack
        public override object Authenticate(IServiceBase service, OAuth request, IOAuthSession session, IOAuthTokens tokens, OAuthAuthorizer oAuth)
        {
            var code = service.RequestContext.Get<IHttpRequest>().QueryString["code"];
            var isPreAuthCallback = !code.IsNullOrEmpty();
            if (!isPreAuthCallback)
            {
                var preAuthUrl = PreAuthUrl + "?client_id={0}&redirect_uri={1}&scope={2}"
                    .Fmt(AppId, this.CallbackUrl.UrlEncode(), string.Join(",", Permissions));
                return service.Redirect(preAuthUrl);
            }

            var accessTokenUrl = this.AccessTokenUrl + "?client_id={0}&redirect_uri={1}&client_secret={2}&code={3}"
                .Fmt(AppId, this.CallbackUrl.UrlEncode(), AppSecret, code);

            try
            {
                var contents = accessTokenUrl.DownloadUrl();
                var authInfo = HttpUtility.ParseQueryString(contents);
                tokens.AccessTokenSecret = authInfo["access_token"];
                service.SaveSession(session);
                session.OnAuthenticated(service, tokens, authInfo.ToDictionary());

                //Haz access!
                return service.Redirect(session.ReferrerUrl.AddQueryParam("s", "1"));
            }
            catch (WebException we)
            {
                var statusCode = ((HttpWebResponse)we.Response).StatusCode;
                if (statusCode == HttpStatusCode.BadRequest)
                {
                    return service.Redirect(session.ReferrerUrl.AddQueryParam("f", "AccessTokenFailed"));
                }
            }

            //Shouldn't get here
            return service.Redirect(session.ReferrerUrl.AddQueryParam("f", "Unknown"));
        }
コード例 #3
0
        public virtual object Authenticate(IServiceBase service, Auth request, IAuthSession session,
            IOAuthTokens tokens, OAuthAuthorizer oAuth)
        {
            //Default oAuth logic based on Twitter's oAuth workflow
            if (!tokens.RequestToken.IsNullOrEmpty() && !request.oauth_token.IsNullOrEmpty())
            {
                oAuth.RequestToken = tokens.RequestToken;
                oAuth.RequestTokenSecret = tokens.RequestTokenSecret;
                oAuth.AuthorizationToken = request.oauth_token;
                oAuth.AuthorizationVerifier = request.oauth_verifier;

                if (oAuth.AcquireAccessToken())
                {
                    tokens.AccessToken = oAuth.AccessToken;
                    tokens.AccessTokenSecret = oAuth.AccessTokenSecret;
                    session.OnAuthenticated(service, tokens, oAuth.AuthInfo);
                    service.SaveSession(session);

                    //Haz access!
                    return service.Redirect(session.ReferrerUrl.AddHashParam("s", "1"));
                }

                //No Joy :(
                tokens.RequestToken = null;
                tokens.RequestTokenSecret = null;
                service.SaveSession(session);
                return service.Redirect(session.ReferrerUrl.AddHashParam("f", "AccessTokenFailed"));
            }
            if (oAuth.AcquireRequestToken())
            {
                tokens.RequestToken = oAuth.RequestToken;
                tokens.RequestTokenSecret = oAuth.RequestTokenSecret;
                service.SaveSession(session);

                //Redirect to OAuth provider to approve access
                return service.Redirect(this.AuthorizeUrl
                    .AddQueryParam("oauth_token", tokens.RequestToken)
                    .AddQueryParam("oauth_callback", session.ReferrerUrl));
            }

            return service.Redirect(session.ReferrerUrl.AddHashParam("f", "RequestTokenFailed"));
        }
コード例 #4
0
        public override object OnGet(Auth request)
        {
            if (ValidateFn != null)
            {
                var response = ValidateFn(this, HttpMethods.Get, request);
                if (response != null)
                {
                    return(response);
                }
            }

            AssertAuthProviders();

            if (request.provider == LogoutAction)
            {
                this.RemoveSession();
                return(new AuthResponse());
            }

            var provider = request.provider ?? AuthConfigs[0].Provider;

            if (provider == BasicProvider || provider == CredentialsProvider)
            {
                return(CredentialsAuth(request));
            }

            var oAuthConfig = AuthConfigs.FirstOrDefault(x => x.Provider == provider);

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

            var session = this.GetSession();

            if (oAuthConfig.CallbackUrl.IsNullOrEmpty())
            {
                oAuthConfig.CallbackUrl = base.RequestContext.AbsoluteUri;
            }

            if (session.ReferrerUrl.IsNullOrEmpty())
            {
                session.ReferrerUrl = base.RequestContext.GetHeader("Referer") ?? oAuthConfig.CallbackUrl;
            }

            var oAuth = new OAuthAuthorizer(oAuthConfig);

            if (!session.IsAuthorized(provider))
            {
                var tokens = session.ProviderOAuthAccess.FirstOrDefault(x => x.Provider == provider);
                if (tokens == null)
                {
                    session.ProviderOAuthAccess.Add(tokens = new OAuthTokens {
                        Provider = provider
                    });
                }

                return(oAuthConfig.Authenticate(this, request, session, tokens, oAuth));
            }

            //Already Authenticated
            return(this.Redirect(session.ReferrerUrl.AddHashParam("s", "0")));
        }
コード例 #5
0
ファイル: AuthConfig.cs プロジェクト: xamarin/ServiceStack
        public override object Authenticate(IServiceBase service, Auth request, IAuthSession session, IOAuthTokens tokens, OAuthAuthorizer oAuth)
        {
            var code = service.RequestContext.Get <IHttpRequest>().QueryString["code"];
            var isPreAuthCallback = !code.IsNullOrEmpty();

            if (!isPreAuthCallback)
            {
                var preAuthUrl = PreAuthUrl + "?client_id={0}&redirect_uri={1}&scope={2}"
                                 .Fmt(AppId, this.CallbackUrl.UrlEncode(), string.Join(",", Permissions));
                return(service.Redirect(preAuthUrl));
            }

            var accessTokenUrl = this.AccessTokenUrl + "?client_id={0}&redirect_uri={1}&client_secret={2}&code={3}"
                                 .Fmt(AppId, this.CallbackUrl.UrlEncode(), AppSecret, code);

            try
            {
                var contents = accessTokenUrl.DownloadUrl();
                var authInfo = HttpUtility.ParseQueryString(contents);
                tokens.AccessTokenSecret = authInfo["access_token"];
                service.SaveSession(session);
                session.OnAuthenticated(service, tokens, authInfo.ToDictionary());

                //Haz access!
                return(service.Redirect(session.ReferrerUrl.AddHashParam("s", "1")));
            }
            catch (WebException we)
            {
                var statusCode = ((HttpWebResponse)we.Response).StatusCode;
                if (statusCode == HttpStatusCode.BadRequest)
                {
                    return(service.Redirect(session.ReferrerUrl.AddHashParam("f", "AccessTokenFailed")));
                }
            }

            //Shouldn't get here
            return(service.Redirect(session.ReferrerUrl.AddHashParam("f", "Unknown")));
        }