From dd3ebc16dfdc5ba7dfaf63431a129367856510aa Mon Sep 17 00:00:00 2001 From: "Namhyeon, Go" Date: Mon, 13 Jan 2025 14:46:48 +0900 Subject: [PATCH] Update the credential management --- lib/anthropic.js | 6 ++-- lib/apikey.js | 39 -------------------------- lib/aviation.js | 10 +++---- lib/chatgpt.js | 4 +-- lib/credentials.js | 69 ++++++++++++++++++++++++++++++++++++++++++++++ lib/groq.js | 10 ++----- testloader.js | 8 +++--- 7 files changed, 86 insertions(+), 60 deletions(-) delete mode 100644 lib/apikey.js create mode 100644 lib/credentials.js diff --git a/lib/anthropic.js b/lib/anthropic.js index 72aa315..d518100 100644 --- a/lib/anthropic.js +++ b/lib/anthropic.js @@ -8,12 +8,12 @@ // var FILE = require("lib/file"); var HTTP = require("lib/http"); -var APIKEY = require("lib/apikey"); +var CRED = require("lib/credentials"); function chat(content) { var answers = []; - var apikey = APIKEY.getApiKey("anthropic"); + var apikey = CRED.get("apikey", "anthropic"); console.log("Anthropic (Claude) API KEY:", apikey); var response = HTTP.create("MSXML") @@ -54,7 +54,7 @@ function chat(content) { exports.chat = chat; -exports.VERSIONINFO = "Anthropic (Claude) interface (anthropic.js) version 0.1.2"; +exports.VERSIONINFO = "Anthropic (Claude) interface (anthropic.js) version 0.1.4"; exports.AUTHOR = "abuse@catswords.net"; exports.global = global; exports.require = global.require; diff --git a/lib/apikey.js b/lib/apikey.js deleted file mode 100644 index 3ff033e..0000000 --- a/lib/apikey.js +++ /dev/null @@ -1,39 +0,0 @@ -// apikey.js -// https://github.com/gnh1201/welsonjs -var FILE = require("lib/file"); - -function loadTextFile(filename) { - if (FILE.fileExists(filename)) { - return FILE.readFile("data/apikey.json", FILE.CdoCharset.CdoUTF_8); - } - return ""; -} - -function loadKeyData() { - var s = loadTextFile("data/apikey.json"); - return JSON.parse(s); -} - -function getApiKey(serviceName) { - var apikey = ""; - if (serviceName in API_KEY_DATA) { - apikey = API_KEY_DATA[serviceName]; - } - - var prelude = "file:"; - if (apikey.indexOf(prelude) == 0) { - var filename = apikey.substring(prelude.length); - apikey = loadTextFile(filename); - } - - return apikey; -} - -var API_KEY_DATA = loadKeyData(); - -exports.getApiKey = getApiKey; - -exports.VERSIONINFO = "API key library (apikey.js) version 0.1"; -exports.AUTHOR = "abuse@catswords.net"; -exports.global = global; -exports.require = global.require; diff --git a/lib/aviation.js b/lib/aviation.js index 85be871..ada4ab8 100644 --- a/lib/aviation.js +++ b/lib/aviation.js @@ -8,7 +8,7 @@ // - SearchApi website: https://www.searchapi.io/?via=namhyeon // var HTTP = require("lib/http"); -var APIKEY = require("lib/apikey"); +var CRED = require("lib/credentials"); function getData(type, params, limit, offset) { var params = params || {}; @@ -21,7 +21,7 @@ function getData(type, params, limit, offset) { params["limit"] = limit; params["offset"] = offset; - params["access_key"] = APIKEY.getApiKey("aviationstack"); + params["access_key"] = CRED.get("apikey", "aviationstack"); var response = HTTP.create() .setParameters(params) @@ -70,7 +70,7 @@ function getFlightsFuture(params, limit, offset) { function getRoundTrip(arrival_id, departure_id, outbound_date, return_date) { var response = HTTP.create() .setParameters({ - "api_key": APIKEY.getApiKey("searchapi"), + "api_key": CRED.get("apikey", "searchapi"), "arrival_id": arrival_id, "departure_id": departure_id, "engine": "google_flights", @@ -87,7 +87,7 @@ function getRoundTrip(arrival_id, departure_id, outbound_date, return_date) { function getOneWay(arrival_id, departure_id, outbound_date) { var response = HTTP.create() .setParameters({ - "api_key": APIKEY.getApiKey("searchapi"), + "api_key": CRED.get("apikey", "searchapi"), "arrival_id": arrival_id, "departure_id": departure_id, "engine": "google_flights", @@ -113,7 +113,7 @@ exports.getFlightsFuture = getFlightsFuture; exports.getRoundTrip = getRoundTrip; exports.getOneWay = getOneWay; -exports.VERSIONINFO = "Aviation Data Integration (aviation.js) version 0.1.2"; +exports.VERSIONINFO = "Aviation Data Integration (aviation.js) version 0.1.3"; exports.AUTHOR = "abuse@catswords.net"; exports.global = global; exports.require = global.require; diff --git a/lib/chatgpt.js b/lib/chatgpt.js index 17937eb..d855dd6 100644 --- a/lib/chatgpt.js +++ b/lib/chatgpt.js @@ -8,12 +8,12 @@ // var FILE = require("lib/file"); var HTTP = require("lib/http"); -var APIKEY = require("lib/apikey"); +var CRED = require("lib/credentials"); function chat(content) { var answers = []; - var apikey = APIKEY.getApiKey("chatgpt"); + var apikey = CRED.get("apikey", "chatgpt"); console.log("ChatGPT API KEY:", apikey); var response = HTTP.create("MSXML") diff --git a/lib/credentials.js b/lib/credentials.js new file mode 100644 index 0000000..b653718 --- /dev/null +++ b/lib/credentials.js @@ -0,0 +1,69 @@ +// credentials.js +// Namhyeon Go +// https://github.com/gnh1201/welsonjs +// +var FILE = require("lib/file"); + +var CREDENTIALS_DATA = []; + +function getTextFromFile(filename) { + if (FILE.fileExists(filename)) { + return FILE.readFile(filename, FILE.CdoCharset.CdoUTF_8); + } + + return ""; +} + +function readFromFile(type, filename) { + var data = JSON.parse(getTextFromFile(filename)); + + for (var provider in data) { + var prelude = "file:"; + var value = (function(s) { + if (s.indexOf(prelude) == 0) { + var filename = s.substring(prelude.length); + return getTextFromFile(filename); + } else { + return s; + } + })(data[provider]); + + push(type, provider, value); + } +} + +function push(type, provider, value) { + CREDENTIALS_DATA.push({ + "type": type, + "provider": provider, + "value": value + }); +} + +function get(type, provider, index) { + var index = index || 0; + var matches = CREDENTIALS_DATA.reduce(function(a, x) { + if (x.type == type && x.provider == provider) { + a.push(x.value); + } + + return a; + }, []); + + if (matches.length - 1 < index) { + return null; + } + + return matches[index]; +} + +readFromFile("apikey", "data/apikey.json"); + +exports.readFromFile = readFromFile; +exports.push = push; +exports.get = get; + +exports.VERSIONINFO = "Credential store (credentials.js) version 0.1"; +exports.AUTHOR = "abuse@catswords.net"; +exports.global = global; +exports.require = global.require; diff --git a/lib/groq.js b/lib/groq.js index ca99729..e6f92bb 100644 --- a/lib/groq.js +++ b/lib/groq.js @@ -8,16 +8,12 @@ // var FILE = require("lib/file"); var HTTP = require("lib/http"); - -function loadApiKey() { - var s = FILE.readFile("data/groq-apikey.txt", FILE.CdoCharset.CdoUTF_8); - return s.trim(); -} +var CRED = require("lib/credentials"); function chat(content) { var answers = []; - var apikey = loadApiKey(); + var apikey = CRED.get("apikey", "groq"); console.log("Groq (GroqCloud) API KEY:", apikey); var response = HTTP.create("MSXML") @@ -45,7 +41,7 @@ function chat(content) { exports.chat = chat; -exports.VERSIONINFO = "Groq (GroqCloud) interface (groq.js) version 0.1"; +exports.VERSIONINFO = "Groq (GroqCloud) interface (groq.js) version 0.1.1"; exports.AUTHOR = "abuse@catswords.net"; exports.global = global; exports.require = global.require; diff --git a/testloader.js b/testloader.js index 54be39e..0d6fde8 100644 --- a/testloader.js +++ b/testloader.js @@ -1006,11 +1006,11 @@ var test_implements = { // https://catswords-oss.rdbl.io/5719744820/8278298336 "proxy_custom_provider": function() { var HTTP = require("lib/http"); - var APIKEY = require("lib/apikey"); + var CRED = require("lib/credentials"); var response = HTTP.create() .setVariables({ - "api_key": APIKEY.getApiKey("scrapeops"), + "api_key": CRED.get("apikey", "scrapeops"), "render_js": "false", "residential": "false", "country": "us", @@ -1030,11 +1030,11 @@ var test_implements = { // https://catswords-oss.rdbl.io/5719744820/8278298336 "proxy_serp": function() { var HTTP = require("lib/http"); - var APIKEY = require("lib/apikey"); + var CRED = require("lib/credentials"); var response = HTTP.create() .setVariables({ - "api_key": APIKEY.getApiKey("searchapi") + "api_key": CRED.get("apikey", "searchapi") }) .setProxy({ "enabled": true,