chore: bunch of improvements
Signed-off-by: Sebastian Krupinski <krupinski01@gmail.com>
This commit is contained in:
@@ -16,7 +16,12 @@ import type {
|
||||
*/
|
||||
export abstract class Identity {
|
||||
abstract toJson(): ServiceIdentity;
|
||||
|
||||
abstract clone(): Identity;
|
||||
|
||||
toJSON(): ServiceIdentity {
|
||||
return this.toJson();
|
||||
}
|
||||
|
||||
static fromJson(data: ServiceIdentity): Identity {
|
||||
switch (data.type) {
|
||||
case 'NA':
|
||||
@@ -39,16 +44,30 @@ export abstract class Identity {
|
||||
* No authentication
|
||||
*/
|
||||
export class IdentityNone extends Identity {
|
||||
readonly type = 'NA' as const;
|
||||
|
||||
private _data: ServiceIdentityNone;
|
||||
|
||||
constructor() {
|
||||
super();
|
||||
this._data = {
|
||||
type: 'NA'
|
||||
};
|
||||
}
|
||||
|
||||
get type(): 'NA' {
|
||||
return this._data.type;
|
||||
}
|
||||
|
||||
static fromJson(_data: ServiceIdentityNone): IdentityNone {
|
||||
return new IdentityNone();
|
||||
}
|
||||
|
||||
toJson(): ServiceIdentityNone {
|
||||
return {
|
||||
type: this.type
|
||||
};
|
||||
return { ...this._data };
|
||||
}
|
||||
|
||||
clone(): IdentityNone {
|
||||
return IdentityNone.fromJson(this.toJson());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -56,14 +75,36 @@ export class IdentityNone extends Identity {
|
||||
* Basic authentication (username/password)
|
||||
*/
|
||||
export class IdentityBasic extends Identity {
|
||||
readonly type = 'BA' as const;
|
||||
identity: string;
|
||||
secret: string;
|
||||
|
||||
private _data: ServiceIdentityBasic;
|
||||
|
||||
constructor(identity: string = '', secret: string = '') {
|
||||
super();
|
||||
this.identity = identity;
|
||||
this.secret = secret;
|
||||
this._data = {
|
||||
type: 'BA',
|
||||
identity,
|
||||
secret
|
||||
};
|
||||
}
|
||||
|
||||
get type(): 'BA' {
|
||||
return this._data.type;
|
||||
}
|
||||
|
||||
get identity(): string {
|
||||
return this._data.identity;
|
||||
}
|
||||
|
||||
set identity(value: string) {
|
||||
this._data.identity = value;
|
||||
}
|
||||
|
||||
get secret(): string {
|
||||
return this._data.secret;
|
||||
}
|
||||
|
||||
set secret(value: string) {
|
||||
this._data.secret = value;
|
||||
}
|
||||
|
||||
static fromJson(data: ServiceIdentityBasic): IdentityBasic {
|
||||
@@ -71,11 +112,11 @@ export class IdentityBasic extends Identity {
|
||||
}
|
||||
|
||||
toJson(): ServiceIdentityBasic {
|
||||
return {
|
||||
type: this.type,
|
||||
identity: this.identity,
|
||||
secret: this.secret
|
||||
};
|
||||
return { ...this._data };
|
||||
}
|
||||
|
||||
clone(): IdentityBasic {
|
||||
return IdentityBasic.fromJson(this.toJson());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -83,12 +124,27 @@ export class IdentityBasic extends Identity {
|
||||
* Token authentication (API key, static token)
|
||||
*/
|
||||
export class IdentityToken extends Identity {
|
||||
readonly type = 'TA' as const;
|
||||
token: string;
|
||||
|
||||
private _data: ServiceIdentityToken;
|
||||
|
||||
constructor(token: string = '') {
|
||||
super();
|
||||
this.token = token;
|
||||
this._data = {
|
||||
type: 'TA',
|
||||
token
|
||||
};
|
||||
}
|
||||
|
||||
get type(): 'TA' {
|
||||
return this._data.type;
|
||||
}
|
||||
|
||||
get token(): string {
|
||||
return this._data.token;
|
||||
}
|
||||
|
||||
set token(value: string) {
|
||||
this._data.token = value;
|
||||
}
|
||||
|
||||
static fromJson(data: ServiceIdentityToken): IdentityToken {
|
||||
@@ -96,10 +152,11 @@ export class IdentityToken extends Identity {
|
||||
}
|
||||
|
||||
toJson(): ServiceIdentityToken {
|
||||
return {
|
||||
type: this.type,
|
||||
token: this.token
|
||||
};
|
||||
return { ...this._data };
|
||||
}
|
||||
|
||||
clone(): IdentityToken {
|
||||
return IdentityToken.fromJson(this.toJson());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -107,12 +164,8 @@ export class IdentityToken extends Identity {
|
||||
* OAuth authentication
|
||||
*/
|
||||
export class IdentityOAuth extends Identity {
|
||||
readonly type = 'OA' as const;
|
||||
accessToken: string;
|
||||
accessScope?: string[];
|
||||
accessExpiry?: number;
|
||||
refreshToken?: string;
|
||||
refreshLocation?: string;
|
||||
|
||||
private _data: ServiceIdentityOAuth;
|
||||
|
||||
constructor(
|
||||
accessToken: string = '',
|
||||
@@ -122,11 +175,58 @@ export class IdentityOAuth extends Identity {
|
||||
refreshLocation?: string
|
||||
) {
|
||||
super();
|
||||
this.accessToken = accessToken;
|
||||
this.accessScope = accessScope;
|
||||
this.accessExpiry = accessExpiry;
|
||||
this.refreshToken = refreshToken;
|
||||
this.refreshLocation = refreshLocation;
|
||||
this._data = {
|
||||
type: 'OA',
|
||||
accessToken,
|
||||
accessScope,
|
||||
accessExpiry,
|
||||
refreshToken,
|
||||
refreshLocation
|
||||
};
|
||||
}
|
||||
|
||||
get type(): 'OA' {
|
||||
return this._data.type;
|
||||
}
|
||||
|
||||
get accessToken(): string {
|
||||
return this._data.accessToken;
|
||||
}
|
||||
|
||||
set accessToken(value: string) {
|
||||
this._data.accessToken = value;
|
||||
}
|
||||
|
||||
get accessScope(): string[] | undefined {
|
||||
return this._data.accessScope;
|
||||
}
|
||||
|
||||
set accessScope(value: string[] | undefined) {
|
||||
this._data.accessScope = value;
|
||||
}
|
||||
|
||||
get accessExpiry(): number | undefined {
|
||||
return this._data.accessExpiry;
|
||||
}
|
||||
|
||||
set accessExpiry(value: number | undefined) {
|
||||
this._data.accessExpiry = value;
|
||||
}
|
||||
|
||||
get refreshToken(): string | undefined {
|
||||
return this._data.refreshToken;
|
||||
}
|
||||
|
||||
set refreshToken(value: string | undefined) {
|
||||
this._data.refreshToken = value;
|
||||
}
|
||||
|
||||
get refreshLocation(): string | undefined {
|
||||
return this._data.refreshLocation;
|
||||
}
|
||||
|
||||
set refreshLocation(value: string | undefined) {
|
||||
this._data.refreshLocation = value;
|
||||
}
|
||||
|
||||
static fromJson(data: ServiceIdentityOAuth): IdentityOAuth {
|
||||
@@ -143,13 +243,17 @@ export class IdentityOAuth extends Identity {
|
||||
return {
|
||||
type: this.type,
|
||||
accessToken: this.accessToken,
|
||||
...(this.accessScope && { accessScope: this.accessScope }),
|
||||
...(this.accessExpiry && { accessExpiry: this.accessExpiry }),
|
||||
...(this.refreshToken && { refreshToken: this.refreshToken }),
|
||||
...(this.refreshLocation && { refreshLocation: this.refreshLocation })
|
||||
...(this.accessScope !== undefined && { accessScope: [...this.accessScope] }),
|
||||
...(this.accessExpiry !== undefined && { accessExpiry: this.accessExpiry }),
|
||||
...(this.refreshToken !== undefined && { refreshToken: this.refreshToken }),
|
||||
...(this.refreshLocation !== undefined && { refreshLocation: this.refreshLocation })
|
||||
};
|
||||
}
|
||||
|
||||
clone(): IdentityOAuth {
|
||||
return IdentityOAuth.fromJson(this.toJson());
|
||||
}
|
||||
|
||||
isExpired(): boolean {
|
||||
if (!this.accessExpiry) return false;
|
||||
return Date.now() / 1000 >= this.accessExpiry;
|
||||
@@ -165,16 +269,44 @@ export class IdentityOAuth extends Identity {
|
||||
* Client certificate authentication (mTLS)
|
||||
*/
|
||||
export class IdentityCertificate extends Identity {
|
||||
readonly type = 'CC' as const;
|
||||
certificate: string;
|
||||
privateKey: string;
|
||||
passphrase?: string;
|
||||
private _data: ServiceIdentityCertificate;
|
||||
|
||||
constructor(certificate: string = '', privateKey: string = '', passphrase?: string) {
|
||||
super();
|
||||
this.certificate = certificate;
|
||||
this.privateKey = privateKey;
|
||||
this.passphrase = passphrase;
|
||||
this._data = {
|
||||
type: 'CC',
|
||||
certificate,
|
||||
privateKey,
|
||||
passphrase
|
||||
};
|
||||
}
|
||||
|
||||
get type(): 'CC' {
|
||||
return this._data.type;
|
||||
}
|
||||
|
||||
get certificate(): string {
|
||||
return this._data.certificate;
|
||||
}
|
||||
|
||||
set certificate(value: string) {
|
||||
this._data.certificate = value;
|
||||
}
|
||||
|
||||
get privateKey(): string {
|
||||
return this._data.privateKey;
|
||||
}
|
||||
|
||||
set privateKey(value: string) {
|
||||
this._data.privateKey = value;
|
||||
}
|
||||
|
||||
get passphrase(): string | undefined {
|
||||
return this._data.passphrase;
|
||||
}
|
||||
|
||||
set passphrase(value: string | undefined) {
|
||||
this._data.passphrase = value;
|
||||
}
|
||||
|
||||
static fromJson(data: ServiceIdentityCertificate): IdentityCertificate {
|
||||
@@ -190,7 +322,11 @@ export class IdentityCertificate extends Identity {
|
||||
type: this.type,
|
||||
certificate: this.certificate,
|
||||
privateKey: this.privateKey,
|
||||
...(this.passphrase && { passphrase: this.passphrase })
|
||||
...(this.passphrase !== undefined && { passphrase: this.passphrase })
|
||||
};
|
||||
}
|
||||
|
||||
clone(): IdentityCertificate {
|
||||
return IdentityCertificate.fromJson(this.toJson());
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user