82 lines
2.3 KiB
JavaScript
82 lines
2.3 KiB
JavaScript
const DEFAULT_URL = "";
|
|
const API_PATH = "/api/v3/core/applications/";
|
|
|
|
function isValidUrl(string) {
|
|
try {
|
|
new URL(string);
|
|
return true;
|
|
} catch (err) {
|
|
return false;
|
|
}
|
|
}
|
|
|
|
export default class AuthentikEngine {
|
|
name = "Authentik";
|
|
type = "web"; // web | images | videos | news | custom
|
|
bangShortcut = "authentik";
|
|
|
|
authentikSearchUrl = "";
|
|
authentikBase = "";
|
|
authentikToken = "";
|
|
|
|
settingsSchema = [
|
|
{ key: "authentikUrl", label: "Authentik Instance URL", description: "The base URL of your custom authentik instance.", type: "url", required: true },
|
|
{ key: "accessToken", label: "Token", description: "Token to the authentik instance.", type: "password", required: true },
|
|
]
|
|
|
|
configure(settings) {
|
|
// called after settings save and on server restart
|
|
this.authentikBase = (settings.authentikUrl || DEFAULT_URL );
|
|
this.authentikSearchUrl = this.authentikBase + API_PATH;
|
|
|
|
if(!isValidUrl(this.authentikSearchUrl)){
|
|
this.authentikSearchUrl = "";
|
|
}
|
|
|
|
this.authentikToken = settings.accessToken || "";
|
|
}
|
|
|
|
async isConfigured(){
|
|
if(this.authentikSearchUrl == ""){
|
|
return false;
|
|
}
|
|
|
|
if(this.authentikToken == ""){
|
|
return false;
|
|
}
|
|
|
|
return true;
|
|
}
|
|
|
|
async executeSearch (
|
|
query,
|
|
page = 1,
|
|
timeFilter,
|
|
context
|
|
) {
|
|
try {
|
|
const configured = await this.isConfigured();
|
|
if(!configured){
|
|
return [];
|
|
}
|
|
|
|
const doFetch = context?.fetch ?? fetch;
|
|
|
|
const response = await doFetch(`${this.authentikSearchUrl}?search=${encodeURIComponent(query)}&page_size=20&page=${encodeURIComponent(page)}`, {headers: {"Accept": "application/json", "Authorization": `Bearer ${this.authentikToken}` }})
|
|
context?.sentinel?.(response, this.name)
|
|
const response_json = await response.json()
|
|
|
|
return response_json.results.map((r) => ({
|
|
title: r.name || r.slug || r?.provider_obj.name || r.launch_url || r.meta_launch_url || "Unknown",
|
|
url: r.launch_url || r.meta_launch_url || this.authentikBase,
|
|
snippet: r.meta_description || r.meta_publisher || "",
|
|
source: this.name,
|
|
...(r.meta_icon_url ? { thumbnail: r.meta_icon_url } : {}),
|
|
}));
|
|
} catch (e) {
|
|
if (e?.name === "SentinelBreach") throw e
|
|
return []
|
|
}
|
|
}
|
|
|
|
} |