bodicsek 6 rokov pred
rodič
commit
61064117bf
4 zmenil súbory, kde vykonal 307 pridanie a 1 odobranie
  1. 1 1
      .gitignore
  2. 155 0
      app.js
  3. 136 0
      package-lock.json
  4. 15 0
      package.json

+ 1 - 1
.gitignore

@@ -31,4 +31,4 @@ node_modules
 # ---> VisualStudioCode
 .settings
 
-
+.DS_Store

+ 155 - 0
app.js

@@ -0,0 +1,155 @@
+var fs = require('fs');
+var https = require('https');
+var os = require('os');
+var httpSignature = require('http-signature');
+var jsSHA = require("jssha");
+
+
+// TODO: update these values to your own
+var tenancyId = "ocid1.tenancy.oc1..aaaaaaaa56ugo5lfjdiyb3n5dmk2t3kzs4kuwea3heu73bmmqycg7hmqpinq";
+var authUserId = "ocid1.user.oc1..aaaaaaaatffa6hunpdesdzxs5wnspkoqbualx4gbnajomrfsm3h7fp6vr5jq";
+var keyFingerprint = "c2:ba:18:4a:61:93:4f:7e:81:1d:4a:4d:66:39:4f:ef";
+var privateKeyPath = "~/.oci/oci_api_key.pem";
+
+
+var identityDomain = "identity.us-ashburn-1.oraclecloud.com";
+var coreServicesDomain = "iaas.us-ashburn-1.oraclecloud.com";
+
+
+if (privateKeyPath.indexOf("~/") === 0) {
+    privateKeyPath = privateKeyPath.replace("~", os.homedir())
+}
+var privateKey = fs.readFileSync(privateKeyPath, 'ascii');
+
+
+// signing function as described at https://docs.cloud.oracle.com/Content/API/Concepts/signingrequests.htm
+function sign(request, options) {
+
+    var apiKeyId = options.tenancyId + "/" + options.userId + "/" + options.keyFingerprint;
+
+    var headersToSign = [
+        "host",
+        "date",
+        "(request-target)"
+    ];
+
+    var methodsThatRequireExtraHeaders = ["POST", "PUT"];
+
+    if (methodsThatRequireExtraHeaders.indexOf(request.method.toUpperCase()) !== -1) {
+        options.body = options.body || "";
+
+        var shaObj = new jsSHA("SHA-256", "TEXT");
+        shaObj.update(options.body);
+
+        request.setHeader("Content-Length", options.body.length);
+        request.setHeader("x-content-sha256", shaObj.getHash('B64'));
+
+        headersToSign = headersToSign.concat([
+            "content-type",
+            "content-length",
+            "x-content-sha256"
+        ]);
+    }
+
+    httpSignature.sign(request, {
+        key: options.privateKey,
+        keyId: apiKeyId,
+        headers: headersToSign
+    });
+
+    var newAuthHeaderValue = request.getHeader("Authorization").replace("Signature ", "Signature version=\"1\",");
+    request.setHeader("Authorization", newAuthHeaderValue);
+}
+
+// generates a function to handle the https.request response object
+function handleRequest(callback) {
+
+    return function (response) {
+        var responseBody = "";
+
+        response.on('data', function (chunk) {
+            responseBody += chunk;
+        });
+
+        response.on('end', function () {
+            callback(JSON.parse(responseBody));
+        });
+    }
+}
+
+// gets the user with the specified id
+function getUser(userId, callback) {
+
+    var options = {
+        host: identityDomain,
+        path: "/20160918/users/" + encodeURIComponent(userId),
+    };
+
+    var request = https.request(options, handleRequest(callback));
+
+    sign(request, {
+        privateKey: privateKey,
+        keyFingerprint: keyFingerprint,
+        tenancyId: tenancyId,
+        userId: authUserId
+    });
+
+    request.end();
+};
+
+// creates a Oracle Cloud Infrastructure VCN in the specified compartment
+function createVCN(compartmentId, displayName, cidrBlock, callback) {
+
+    var body = JSON.stringify({
+        compartmentId: compartmentId,
+        displayName: displayName,
+        cidrBlock: cidrBlock
+    });
+
+    var options = {
+        host: coreServicesDomain,
+        path: '/20160918/vcns',
+        method: 'POST',
+        headers: {
+            "Content-Type": "application/json",
+        }
+    };
+
+    var request = https.request(options, handleRequest(callback));
+
+    sign(request, {
+        body: body,
+        privateKey: privateKey,
+        keyFingerprint: keyFingerprint,
+        tenancyId: tenancyId,
+        userId: authUserId
+    });
+
+    request.end(body);
+};
+
+/************************************************************************/
+
+function getStates(callback) {
+    const request = https.request("https://130.35.55.238:7002/wl/oc_gateway/20190701/states", handleRequest(callback));
+    sign(request, {
+        privateKey: privateKey,
+        keyFingerprint: keyFingerprint,
+        tenancyId: tenancyId,
+        userId: authUserId
+    });
+    request.end();
+}
+
+/************************************************************************/
+
+// test the above functions
+// console.log("GET USER:");
+
+// getUser(authUserId, function (data) {
+//     console.log(data);
+// });
+
+getStates(function (data) {
+    console.log(data);
+});

+ 136 - 0
package-lock.json

@@ -0,0 +1,136 @@
+{
+  "name": "oci-node-client",
+  "version": "1.0.0",
+  "lockfileVersion": 1,
+  "requires": true,
+  "dependencies": {
+    "asn1": {
+      "version": "0.2.4",
+      "resolved": "https://registry.npmjs.org/asn1/-/asn1-0.2.4.tgz",
+      "integrity": "sha512-jxwzQpLQjSmWXgwaCZE9Nz+glAG01yF1QnWgbhGwHI5A6FRIEY6IVqtHhIepHqI7/kyEyQEagBC5mBEFlIYvdg==",
+      "requires": {
+        "safer-buffer": "~2.1.0"
+      }
+    },
+    "assert-plus": {
+      "version": "1.0.0",
+      "resolved": "https://registry.npmjs.org/assert-plus/-/assert-plus-1.0.0.tgz",
+      "integrity": "sha1-8S4PPF13sLHN2RRpQuTpbB5N1SU="
+    },
+    "bcrypt-pbkdf": {
+      "version": "1.0.2",
+      "resolved": "https://registry.npmjs.org/bcrypt-pbkdf/-/bcrypt-pbkdf-1.0.2.tgz",
+      "integrity": "sha1-pDAdOJtqQ/m2f/PKEaP2Y342Dp4=",
+      "requires": {
+        "tweetnacl": "^0.14.3"
+      }
+    },
+    "core-util-is": {
+      "version": "1.0.2",
+      "resolved": "https://registry.npmjs.org/core-util-is/-/core-util-is-1.0.2.tgz",
+      "integrity": "sha1-tf1UIgqivFq1eqtxQMlAdUUDwac="
+    },
+    "dashdash": {
+      "version": "1.14.1",
+      "resolved": "https://registry.npmjs.org/dashdash/-/dashdash-1.14.1.tgz",
+      "integrity": "sha1-hTz6D3y+L+1d4gMmuN1YEDX24vA=",
+      "requires": {
+        "assert-plus": "^1.0.0"
+      }
+    },
+    "ecc-jsbn": {
+      "version": "0.1.2",
+      "resolved": "https://registry.npmjs.org/ecc-jsbn/-/ecc-jsbn-0.1.2.tgz",
+      "integrity": "sha1-OoOpBOVDUyh4dMVkt1SThoSamMk=",
+      "requires": {
+        "jsbn": "~0.1.0",
+        "safer-buffer": "^2.1.0"
+      }
+    },
+    "extsprintf": {
+      "version": "1.3.0",
+      "resolved": "https://registry.npmjs.org/extsprintf/-/extsprintf-1.3.0.tgz",
+      "integrity": "sha1-lpGEQOMEGnpBT4xS48V06zw+HgU="
+    },
+    "getpass": {
+      "version": "0.1.7",
+      "resolved": "https://registry.npmjs.org/getpass/-/getpass-0.1.7.tgz",
+      "integrity": "sha1-Xv+OPmhNVprkyysSgmBOi6YhSfo=",
+      "requires": {
+        "assert-plus": "^1.0.0"
+      }
+    },
+    "http-signature": {
+      "version": "1.2.0",
+      "resolved": "https://registry.npmjs.org/http-signature/-/http-signature-1.2.0.tgz",
+      "integrity": "sha1-muzZJRFHcvPZW2WmCruPfBj7rOE=",
+      "requires": {
+        "assert-plus": "^1.0.0",
+        "jsprim": "^1.2.2",
+        "sshpk": "^1.7.0"
+      }
+    },
+    "jsbn": {
+      "version": "0.1.1",
+      "resolved": "https://registry.npmjs.org/jsbn/-/jsbn-0.1.1.tgz",
+      "integrity": "sha1-peZUwuWi3rXyAdls77yoDA7y9RM="
+    },
+    "json-schema": {
+      "version": "0.2.3",
+      "resolved": "https://registry.npmjs.org/json-schema/-/json-schema-0.2.3.tgz",
+      "integrity": "sha1-tIDIkuWaLwWVTOcnvT8qTogvnhM="
+    },
+    "jsprim": {
+      "version": "1.4.1",
+      "resolved": "https://registry.npmjs.org/jsprim/-/jsprim-1.4.1.tgz",
+      "integrity": "sha1-MT5mvB5cwG5Di8G3SZwuXFastqI=",
+      "requires": {
+        "assert-plus": "1.0.0",
+        "extsprintf": "1.3.0",
+        "json-schema": "0.2.3",
+        "verror": "1.10.0"
+      }
+    },
+    "jssha": {
+      "version": "2.3.1",
+      "resolved": "https://registry.npmjs.org/jssha/-/jssha-2.3.1.tgz",
+      "integrity": "sha1-FHshJTaQNcpLL30hDcU58Amz3po="
+    },
+    "safer-buffer": {
+      "version": "2.1.2",
+      "resolved": "https://registry.npmjs.org/safer-buffer/-/safer-buffer-2.1.2.tgz",
+      "integrity": "sha512-YZo3K82SD7Riyi0E1EQPojLz7kpepnSQI9IyPbHHg1XXXevb5dJI7tpyN2ADxGcQbHG7vcyRHk0cbwqcQriUtg=="
+    },
+    "sshpk": {
+      "version": "1.16.1",
+      "resolved": "https://registry.npmjs.org/sshpk/-/sshpk-1.16.1.tgz",
+      "integrity": "sha512-HXXqVUq7+pcKeLqqZj6mHFUMvXtOJt1uoUx09pFW6011inTMxqI8BA8PM95myrIyyKwdnzjdFjLiE6KBPVtJIg==",
+      "requires": {
+        "asn1": "~0.2.3",
+        "assert-plus": "^1.0.0",
+        "bcrypt-pbkdf": "^1.0.0",
+        "dashdash": "^1.12.0",
+        "ecc-jsbn": "~0.1.1",
+        "getpass": "^0.1.1",
+        "jsbn": "~0.1.0",
+        "safer-buffer": "^2.0.2",
+        "tweetnacl": "~0.14.0"
+      }
+    },
+    "tweetnacl": {
+      "version": "0.14.5",
+      "resolved": "https://registry.npmjs.org/tweetnacl/-/tweetnacl-0.14.5.tgz",
+      "integrity": "sha1-WuaBd/GS1EViadEIr6k/+HQ/T2Q="
+    },
+    "verror": {
+      "version": "1.10.0",
+      "resolved": "https://registry.npmjs.org/verror/-/verror-1.10.0.tgz",
+      "integrity": "sha1-OhBcoXBTr1XW4nDB+CiGguGNpAA=",
+      "requires": {
+        "assert-plus": "^1.0.0",
+        "core-util-is": "1.0.2",
+        "extsprintf": "^1.2.0"
+      }
+    }
+  }
+}

+ 15 - 0
package.json

@@ -0,0 +1,15 @@
+{
+  "name": "oci-node-client",
+  "version": "1.0.0",
+  "description": "",
+  "main": "app.js",
+  "scripts": {
+    "test": "echo \"Error: no test specified\" && exit 1"
+  },
+  "author": "",
+  "license": "MIT",
+  "dependencies": {
+    "http-signature": "^1.2.0",
+    "jssha": "^2.3.1"
+  }
+}