Update the credential management

This commit is contained in:
Namhyeon Go 2025-01-13 14:46:48 +09:00
parent 3120a5ea93
commit dd3ebc16df
7 changed files with 86 additions and 60 deletions

View File

@ -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;

View File

@ -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;

View File

@ -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;

View File

@ -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")

69
lib/credentials.js Normal file
View File

@ -0,0 +1,69 @@
// credentials.js
// Namhyeon Go <abuse@catswords.net>
// 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;

View File

@ -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;

View File

@ -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,