@rdfc/http-utils-processor-ts
Version:
HTTP util functions to be used within RDF-Connect
29 lines (28 loc) • 904 B
JavaScript
import { HttpUtilsError } from "../error.js";
export class HttpBasicAuth {
username;
password;
constructor(username, password) {
this.username = username;
this.password = password;
}
encode() {
return ("Basic " +
Buffer.from(`${this.username}:${this.password}`).toString("base64"));
}
async authorize(req) {
req.headers.set("Authorization", this.encode());
}
check(req) {
return req.headers.get("Authorization") == this.encode();
}
static from(config) {
if (!config.username) {
throw HttpUtilsError.illegalParameters("Username is required for HTTP Basic Auth.");
}
if (!config.password) {
throw HttpUtilsError.illegalParameters("Password is required for HTTP Basic Auth.");
}
return new HttpBasicAuth(config.username, config.password);
}
}