Exemplo n.º 1
0
        /// <summary>
        /// A convenience function that tries to ensure that a given URL is a valid Bizweb domain. It does this by making a HEAD request to the given domain, and returns true if the response contains an X-StoreId header.
        ///
        /// **Warning**: a domain could fake the response header, which would cause this method to return true.
        ///
        /// **Warning**: this method of validation is not officially supported by Bizweb and could break at any time.
        /// </summary>
        /// <param name="url">The URL of the shop to check.</param>
        /// <returns>A boolean indicating whether the URL is valid.</returns>
        public static async Task <bool> IsValidShopDomainAsync(string url)
        {
            var uri = RequestEngine.BuildUri(url, false);

            using (var msg = new HttpRequestMessage(HttpMethod.Head, uri))
            {
                var version = (typeof(AuthorizationService)).GetTypeInfo().Assembly.GetName().Version;
                msg.Headers.Add("User-Agent",
                                $"BizwebSharp v{version} (https://github.com/vinhch/BizwebSharp)");
                try
                {
                    using (var response = await HttpUtils.SendHttpRequestNoRedirectAsync(msg))
                    {
                        return(response.Headers
                               .Any(h => h.Key.Equals("X-StoreId", StringComparison.OrdinalIgnoreCase)));
                    }
                }
                catch (HttpRequestException)
                {
                    return(false);
                }
            }
        }
Exemplo n.º 2
0
        /// <summary>
        /// Builds an authorization URL for OAuth integration.
        /// </summary>
        /// <param name="scopes">A permission strings, separated by comma, e.g. 'read_orders,write_script_tags'.
        /// These are the permissions that your app needs to run.</param>
        /// <param name="myApiUrl">The shop's *.bizwebvietnam.net URL.</param>
        /// <param name="apiKey">Your app's public API key.</param>
        /// <param name="redirectUri">URL to redirect the user to after integration.</param>
        /// <param name="state">
        /// An optional, random string value provided by your application which is unique for each authorization request.
        /// During the OAuth callback phase, your application should check that this value matches the one you provided to this method.
        /// </param>
        /// <returns>The authorization url.</returns>
        public static Uri BuildAuthorizationUrl(string scopes, string myApiUrl,
                                                string apiKey, string redirectUri, string state = null)
        {
            //Prepare a uri builder for the tenant URL
            var builder = new UriBuilder(RequestEngine.BuildUri(myApiUrl));

            //Build the querystring
            var qs = new List <KeyValuePair <string, string> >
            {
                new KeyValuePair <string, string>("client_id", apiKey),
                new KeyValuePair <string, string>("scope", scopes),
                new KeyValuePair <string, string>("redirect_uri", redirectUri)
            };

            if (string.IsNullOrEmpty(state) == false)
            {
                qs.Add(new KeyValuePair <string, string>("state", state));
            }

            builder.Path  = "admin/oauth/authorize";
            builder.Query = string.Join("&", qs.Select(s => $"{s.Key}={s.Value}"));

            return(builder.Uri);
        }