kaven-utils
Version:
Utils for Node.js.
83 lines (82 loc) • 3.46 kB
JavaScript
/********************************************************************
* @author: Kaven
* @email: kaven@wuwenkai.com
* @website: http://blog.kaven.xyz
* @file: [Kaven-Utils] /src/net/authentication/KavenAuthentication.ts
* @create: 2019-03-26 14:16:54.029
* @modify: 2023-12-07 10:59:05.572
* @version: 5.4.0
* @times: 35
* @lines: 121
* @copyright: Copyright © 2019-2023 Kaven. All Rights Reserved.
* @description: [description]
* @license: [license]
********************************************************************/
import { HttpStandardRequestHeader_Authorization, HttpStandardRequestHeader_ProxyAuthorization, HttpStandardResponseHeader_Connection, HttpStandardResponseHeader_ContentLength, HttpStandardResponseHeader_ContentType, HttpStandardResponseHeader_ProxyAuthenticate, HttpStandardResponseHeader_WwwAuthenticate, HttpStatusCode, } from "kaven-basic";
import { HttpResponseBody } from "../http/HttpResponseBody.js";
import { HttpResponseMessage } from "../http/HttpResponseMessage.js";
import { HttpResponseStatusLine } from "../http/HttpResponseStatusLine.js";
import { InternalLogger } from "../../KavenUtility.Internal.js";
export class KavenAuthentication {
UserName;
Password;
/**
* A string to be displayed to users so they know which username and
* password to use.
*
* This string should contain at least the name of
* the host performing the authentication and might additionally
* indicate the collection of users who might have access.
*/
Realm = "Kaven Authentication";
/**
* WWW-Authenticate or Proxy-Authenticate
*/
ResponseHeaderName = HttpStandardResponseHeader_WwwAuthenticate;
/**
* @since 4.3.6
* @version 2022-09-20
*/
Records;
constructor(userName, password) {
this.UserName = userName;
this.Password = password;
}
get StatusCode() {
if (this.ResponseHeaderName === HttpStandardResponseHeader_ProxyAuthenticate) {
return HttpStatusCode.ProxyAuthenticationRequired;
}
return HttpStatusCode.Unauthorized;
}
get RequestHeaderName() {
if (this.ResponseHeaderName === HttpStandardResponseHeader_ProxyAuthenticate) {
return HttpStandardRequestHeader_ProxyAuthorization;
}
return HttpStandardRequestHeader_Authorization;
}
CanAuthenticate(key) {
if (this.Records === undefined) {
return true;
}
if (this.Records.CanAuthenticate(key)) {
return true;
}
InternalLogger()?.Warn(`Max Authorization Reached: ${key}`);
return false;
}
AddRecord(key, success) {
this.Records?.AddRecord(key, success);
}
GetResponse(message = "Authentication is required.", statusCode) {
const statusLine = new HttpResponseStatusLine(statusCode ?? this.StatusCode);
let response = new HttpResponseMessage(statusLine);
response = this.Update(response);
if (message) {
response.Body = new HttpResponseBody(Buffer.from(message));
response.AddHeader(HttpStandardResponseHeader_ContentLength, response.Body.Data.byteLength.toString());
}
response.AddHeader(HttpStandardResponseHeader_ContentType, "text/html");
response.AddHeader(HttpStandardResponseHeader_Connection, "close");
return response;
}
}