Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 30 additions & 38 deletions src/DigestAuthentication.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,24 @@ import { calculateMD5, createRandomToken } from './Utils';

const logger = new Logger('DigestAuthentication');

/* NOTE: All of them are nullable because they come straight from the UA
* configuration, whose defaults are `null`.
*/
export type DigestCredentials = {
username?: string;
password?: string;
realm?: string;
username?: string | null;
password?: string | null;
realm?: string | null;
ha1?: string | null;
};

// A parsed WWW-Authenticate or Proxy-Authenticate header field.
export type DigestChallenge = {
algorithm?: string | null;
realm?: string | null;
nonce?: string | null;
opaque?: string | null;
stale?: boolean | null;
qop?: string | string[] | null;
algorithm?: string;
realm?: string;
nonce?: string;
opaque?: string;
stale?: boolean;
qop?: string | string[];
};

export type DigestAuthenticateRequest = {
Expand All @@ -27,44 +31,32 @@ export type DigestAuthenticateRequest = {

export class DigestAuthentication {
#credentials: DigestCredentials;
#cnonce: string | null;
#nc: number;
#ncHex: string;
#algorithm: string | null | undefined;
#realm: string | null | undefined;
#nonce: string | null | undefined;
#opaque: string | null | undefined;
#nc = 0;
#ncHex = '00000000';
#cnonce?: string;
#algorithm?: string;
#realm?: string;
#nonce?: string;
#opaque?: string;
// eslint-disable-next-line no-unused-private-class-members
#stale: boolean | null | undefined;
#qop: 'auth' | 'auth-int' | null;
#method: string | null;
#uri: string | { toString(): string } | null;
#ha1: string | null | undefined;
#response: string | null;
#stale?: boolean;
#qop?: 'auth' | 'auth-int';
#method?: string;
#uri?: string | { toString(): string };
// NOTE: Nullable because it may come from the UA configuration.
#ha1?: string | null;
#response?: string;

constructor(credentials: DigestCredentials) {
this.#credentials = credentials;
this.#cnonce = null;
this.#nc = 0;
this.#ncHex = '00000000';
this.#algorithm = null;
this.#realm = null;
this.#nonce = null;
this.#opaque = null;
this.#stale = null;
this.#qop = null;
this.#method = null;
this.#uri = null;
this.#ha1 = null;
this.#response = null;
}

/**
* Get the generated Digest "response" value.
*
* NOTE: Exposed for testing purposes only.
*/
get response(): string | null {
get response(): string | undefined {
return this.#response;
}

Expand Down Expand Up @@ -168,7 +160,7 @@ export class DigestAuthentication {
return false;
}
} else {
this.#qop = null;
this.#qop = undefined;
}

// Fill other attributes.
Expand Down Expand Up @@ -226,7 +218,7 @@ export class DigestAuthentication {
this.#response = calculateMD5(
`${this.#ha1}:${this.#nonce}:${this.#ncHex}:${this.#cnonce}:auth-int:${ha2}`
);
} else if (this.#qop === null) {
} else if (this.#qop === undefined) {
// HA2 = MD5(A2) = MD5(method:digestURI).
a2 = `${this.#method}:${this.#uri}`;
ha2 = calculateMD5(a2);
Expand Down
74 changes: 74 additions & 0 deletions src/Grammar.d.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,80 @@
import NameAddrHeader = require('./NameAddrHeader');

/**
* The result of `parse()`, whose shape depends on the start rule used.
*/
// eslint-disable-next-line @typescript-eslint/no-explicit-any
export type Grammar = any;

/**
* Generic header field parameters, as collected by the `generic_param` rule.
*/
export type GenericParams = Record<string, string | null>;

/**
* How a host was written, as set by the `hostname`, `IPv4address` and
* `IPv6address` rules.
*/
export type HostType = 'domain' | 'IPv4' | 'IPv6';

/**
* Result of the `Via` start rule.
*/
export type ViaHeader = {
protocol: string;
transport: string;
host: string;
host_type: HostType;
port?: number;
ttl?: number;
maddr?: string;
received?: string;
branch?: string;
rport?: number;
params?: GenericParams;
};

/**
* Result of the `CSeq` start rule.
*/
export type CSeqHeader = {
value: number;
method: string;
};

/**
* Result of the `Session_Expires` start rule.
*/
export type SessionExpiresHeader = {
expires: number;
refresher?: 'uac' | 'uas';
params?: GenericParams;
};

/**
* Result of the `Replaces` start rule.
*/
export type ReplacesHeader = {
call_id: string;
to_tag?: string;
from_tag?: string;
early_only?: true;
params?: GenericParams;
};

/**
* Result of the `Event` start rule.
*/
export type EventHeader = {
event: string;
params?: GenericParams;
};

/**
* Result of the `Refer_To` start rule.
*/
export type ReferToHeader = NameAddrHeader;

export function parse(input: string, startRule?: string): Grammar;

// eslint-disable-next-line @typescript-eslint/no-explicit-any
Expand Down
Loading
Loading