public static NPMResponse GetLoginToken(string url, string user, string password)
        {
            using (var client = new WebClient())
            {
                string loginUri = UrlCombine(url, "/-/user/org.couchdb.user:"******"application/json");
                client.Headers.Add(HttpRequestHeader.ContentType, "application/json");
                client.Headers.Add(HttpRequestHeader.Authorization, "Basic " + Convert.ToBase64String(Encoding.ASCII.GetBytes(user + ":" + password)));

                NPMLoginRequest request = new NPMLoginRequest();
                request.name     = user;
                request.password = password;

                string requestString = JsonUtility.ToJson(request);

                try
                {
                    string      responseString = client.UploadString(loginUri, WebRequestMethods.Http.Put, requestString);
                    NPMResponse response       = JsonUtility.FromJson <NPMResponse>(responseString);
                    return(response);
                }
                catch (WebException e)
                {
                    NPMResponse response = new NPMResponse();
                    response.error = WebExceptionParser.ParseWebException(e);
                    return(response);
                }
            }
        }
예제 #2
0
        public static void Publish(string packageFolder, string registry)
        {
            CredentialManager manager = new CredentialManager();

            if (!manager.HasRegistry(registry))
            {
                throw new System.IO.IOException("Credentials not set for registry " + registry);
            }

            string token = manager.GetCredential(registry).token;

            PublicationManifest manifest = new PublicationManifest(packageFolder, registry);;



            using (var client = new ExpectContinueAware())
            {
                string upload = NPMLogin.UrlCombine(registry, manifest.name);


                client.Encoding = Encoding.UTF8;
                client.Headers.Add(HttpRequestHeader.Accept, "application/json");
                client.Headers.Add(HttpRequestHeader.ContentType, "application/json");
                client.Headers.Add(HttpRequestHeader.Authorization, "Bearer " + token);


                // Headers set by the NPM client, but not by us. Option to try with compatibility issues.

                // client.Headers.Add("npm-in-ci", "false");
                // client.Headers.Add("npm-scope", "");
                // client.Headers.Add(HttpRequestHeader.UserAgent, "npm/6.14.4 node/v12.16.2 linux x64");
                // var random = new Random();
                // string a = String.Format("{0:X8}", random.Next(0x10000000, int.MaxValue)).ToLower();
                // string b = String.Format("{0:X8}", random.Next(0x10000000, int.MaxValue)).ToLower();

                // client.Headers.Add("npm-session", a + b);
                // client.Headers.Add("referer", "publish");


                try
                {
                    string responseString = client.UploadString(upload, WebRequestMethods.Http.Put, manifest.Request);

                    try
                    {
                        NPMResponse response = JsonUtility.FromJson <NPMResponse>(responseString);
                        if (!response.success && string.IsNullOrEmpty(response.ok))
                        {
                            throw new System.IO.IOException(responseString);
                        }
                    }
                    catch (Exception)
                    {
                        throw new System.IO.IOException(responseString);
                    }
                }
                catch (WebException e)
                {
                    throw new System.IO.IOException(WebExceptionParser.ParseWebException(e));
                }
            }
        }