From ca8bdafccdbbd4e8f2f3c7fc815353243520e528 Mon Sep 17 00:00:00 2001 From: Luis Claro Date: Tue, 18 Apr 2023 04:17:04 +0100 Subject: [PATCH] Support for ephemeral credentials --- src/api/user-agent-options.ts | 4 ++-- src/core/messages/digest-authentication.ts | 28 +++++++++++++++++----- 2 files changed, 24 insertions(+), 8 deletions(-) diff --git a/src/api/user-agent-options.ts b/src/api/user-agent-options.ts index 5eb037d72..e848c62e4 100644 --- a/src/api/user-agent-options.ts +++ b/src/api/user-agent-options.ts @@ -49,13 +49,13 @@ export interface UserAgentOptions { * Authorization password. * @defaultValue `""` */ - authorizationPassword?: string; + authorizationPassword?: string | (() => string); /** * Authorization username. * @defaultValue `""` */ - authorizationUsername?: string; + authorizationUsername?: string | (() => string); /** * The user portion of user agent's contact URI. diff --git a/src/core/messages/digest-authentication.ts b/src/core/messages/digest-authentication.ts index d5ea2663e..c2ea5c975 100644 --- a/src/core/messages/digest-authentication.ts +++ b/src/core/messages/digest-authentication.ts @@ -19,8 +19,8 @@ export class DigestAuthentication { private logger: Logger; private ha1: string | undefined; - private username: string | undefined; - private password: string | undefined; + private username: string | (() => string) | undefined; + private password: string | (() => string) | undefined; private cnonce: string | undefined; private nc: number; private ncHex: string; @@ -42,8 +42,8 @@ export class DigestAuthentication { constructor( loggerFactory: LoggerFactory, ha1: string | undefined, - username: string | undefined, - password: string | undefined + username: string | (() => string) | undefined, + password: string | (() => string) | undefined ) { this.logger = loggerFactory.getLogger("sipjs.digestauthentication"); this.username = username; @@ -135,7 +135,7 @@ export class DigestAuthentication { } authParams.push("algorithm=" + this.algorithm); - authParams.push('username="' + this.username + '"'); + authParams.push('username="' + this.getUsername() + '"'); authParams.push('realm="' + this.realm + '"'); authParams.push('nonce="' + this.nonce + '"'); authParams.push('uri="' + this.uri + '"'); @@ -168,7 +168,7 @@ export class DigestAuthentication { // HA1 = MD5(A1) = MD5(username:realm:password) ha1 = this.ha1; if (ha1 === "" || ha1 === undefined) { - ha1 = MD5(this.username + ":" + this.realm + ":" + this.password); + ha1 = MD5(this.getUsername() + ":" + this.realm + ":" + this.getPassword()); } if (this.qop === "auth") { @@ -188,4 +188,20 @@ export class DigestAuthentication { this.response = MD5(ha1 + ":" + this.nonce + ":" + ha2); } } + + private getUsername(): string | undefined { + if (typeof this.username === "function") { + return this.username(); + } else { + return this.username; + } + } + + private getPassword(): string | undefined { + if (typeof this.password === "function") { + return this.password(); + } else { + return this.password; + } + } }