Fix some refactiring error. And Add a way to insert admin credentials

This commit is contained in:
amaury.joly 2021-07-30 12:40:24 +02:00
parent 6407c1b072
commit 2c6df73d02
2 changed files with 70 additions and 23 deletions

View File

@ -27,21 +27,21 @@ class PeerTubeRequester {
this.password = config.password;
}
private async requestAuthToken(): Promise<any> {
async requestAuthToken(): Promise<any> {
let response = await fetch(
new URL(`/api/v1/oauth-clients/local`, this.domainName)
);
if (!response.ok) {
throw new Error(response.statusText); // CRASH
throw new Error("Cannot get client credentials : " + response.statusText); // CRASH
}
const { client_id: clientId, client_secret: clientSecret } =
await response.json();
const clientInfo: { [key: string]: string } = {
clientId,
clientSecret,
grantType: "password",
responseType: "code",
client_id: clientId,
client_secret: clientSecret,
grant_type: "password",
response_type: "code",
username: this.username,
password: this.password,
};
@ -54,7 +54,7 @@ class PeerTubeRequester {
body: myParams,
});
if (!response.ok) {
throw new Error(response.statusText); // CRASH
throw new Error("Cannot get access Token : " + response.statusText); // CRASH
}
const { access_token: accessToken } = await response.json();
return accessToken;

View File

@ -10,7 +10,8 @@ type ListenerData = ListenerRss.Config & {
let myManager: ListenerRssAggregator;
let listenersDataBinding = new Map<string, ListenerData>();
let logger: any;
let peertube: PeerTubeRequester;
let peertube: PeerTubeRequester | undefined = undefined;
let goodPeertubeCredential: boolean = false;
import * as path from "path";
import fs from "fs";
@ -29,6 +30,18 @@ async function register({
type: "input-textarea",
});
registerSetting({
name: "admin-name",
label: "Admin Username",
type: "input",
});
registerSetting({
name: "admin-password",
label: "Admin Password",
type: "input-password",
});
logger.debug("setting register");
fs.appendFileSync(path.join(basePath, "/storage.bd"), "");
@ -37,20 +50,36 @@ async function register({
);
myManager = new ListenerRssAggregator(configAggregator);
peertube = new PeerTubeRequester({
domainName: "http://localhost:9000",
username: "root",
password: "test",
});
logger.debug("Aggregator created");
const inputs = await settingsManager.getSetting("ytb-urls");
if (inputs) await addListeners(inputs);
const settingYtbUrls = await settingsManager.getSetting("ytb-urls");
if (settingYtbUrls) await addListeners(settingYtbUrls);
logger.debug("Config loaded");
const settingCredentials: any = await settingsManager.getSettings([
"admin-name",
"admin-password",
]);
if (settingCredentials["admin-name"] && settingCredentials["admin-password"])
apiRequestInitializer({
domainName: peertubeHelpers.config.getWebserverUrl(),
username: settingCredentials["admin-name"],
password: settingCredentials["admin-password"],
});
logger.debug("Actual config loaded");
settingsManager.onSettingsChange(async (settings: any) => {
if (
!peertube ||
peertube.username != settings["admin-name"] ||
peertube.password != settings["admin-password"]
)
apiRequestInitializer({
domainName: peertubeHelpers.config.getWebserverUrl(),
username: settings["admin-name"],
password: settings["admin-password"],
});
await addListeners(settings["ytb-urls"]);
});
@ -64,13 +93,31 @@ async function register({
JSON.stringify(entries)
);
for (const item of entries.items)
if (peertube)
await peertube.uploadFromUrl({
channelId: datas.channelId,
targetUrl: item.link,
});
else {
logger.warn("Bad credential provides. New entries Skipped.");
}
});
}
async function apiRequestInitializer(data: PeerTubeRequester.Config) {
peertube = new PeerTubeRequester(data);
try {
await peertube.requestAuthToken();
goodPeertubeCredential = true;
logger.debug("credential ok");
} catch (error) {
logger.warn("Error during the credential validation : " + error);
peertube = undefined;
goodPeertubeCredential = false;
}
}
async function addListeners(listenerInput: string) {
let listeners: ListenerData[];
try {
@ -96,9 +143,9 @@ async function addListeners(listenerInput: string) {
myManager.stopAll();
await myManager.saveOverride(listeners);
if (logger) logger.warn("Configuration changed: " + listenerInput);
if (logger) logger.debug("Configuration changed: " + listenerInput);
myManager.startAll();
if (goodPeertubeCredential) myManager.startAll();
}
async function unregister() {