webrtc: add authentication parameters to publisher.js and reader.js (#5026)

add user, pass and token configuration parameters.
This commit is contained in:
Alessandro Ros
2025-09-24 18:28:53 +02:00
committed by GitHub
parent 5203a0f04b
commit 6b57c5187b
2 changed files with 39 additions and 1 deletions

View File

@@ -13,6 +13,9 @@
* @typedef Conf * @typedef Conf
* @type {object} * @type {object}
* @property {string} url - absolute URL of the WHIP endpoint. * @property {string} url - absolute URL of the WHIP endpoint.
* @property {string} user - username.
* @property {string} pass - password.
* @property {string} token - token.
* @property {MediaStream} stream - stream that contains outgoing tracks. * @property {MediaStream} stream - stream that contains outgoing tracks.
* @property {string} videoCodec - outgoing video codec. * @property {string} videoCodec - outgoing video codec.
* @property {number} videoBitrate - outgoing video bitrate. * @property {number} videoBitrate - outgoing video bitrate.
@@ -276,9 +279,23 @@ class MediaMTXWebRTCPublisher {
} }
} }
#authHeader() {
if (this.conf.user !== undefined && this.conf.user !== '') {
const credentials = btoa(`${this.conf.user}:${this.conf.pass}`);
return {'Authorization': `Basic ${credentials}`};
}
if (this.conf.token !== undefined && this.conf.token !== '') {
return {'Authorization': `Bearer ${this.conf.token}`};
}
return {};
}
#requestICEServers() { #requestICEServers() {
return fetch(this.conf.url, { return fetch(this.conf.url, {
method: 'OPTIONS', method: 'OPTIONS',
headers: {
...this.#authHeader(),
},
}) })
.then((res) => MediaMTXWebRTCPublisher.#linkToIceServers(res.headers.get('Link'))); .then((res) => MediaMTXWebRTCPublisher.#linkToIceServers(res.headers.get('Link')));
} }
@@ -325,6 +342,7 @@ class MediaMTXWebRTCPublisher {
return fetch(this.conf.url, { return fetch(this.conf.url, {
method: 'POST', method: 'POST',
headers: { headers: {
...this.#authHeader(),
'Content-Type': 'application/sdp', 'Content-Type': 'application/sdp',
}, },
body: offer, body: offer,

View File

@@ -14,6 +14,9 @@
* @typedef Conf * @typedef Conf
* @type {object} * @type {object}
* @property {string} url - absolute URL of the WHEP endpoint. * @property {string} url - absolute URL of the WHEP endpoint.
* @property {string} user - username.
* @property {string} pass - password.
* @property {string} token - token.
* @property {OnError} onError - called when there's an error. * @property {OnError} onError - called when there's an error.
* @property {OnTrack} onTrack - called when there's a track available. * @property {OnTrack} onTrack - called when there's a track available.
*/ */
@@ -403,9 +406,23 @@ class MediaMTXWebRTCReader {
}); });
} }
#authHeader() {
if (this.conf.user !== undefined && this.conf.user !== '') {
const credentials = btoa(`${this.conf.user}:${this.conf.pass}`);
return {'Authorization': `Basic ${credentials}`};
}
if (this.conf.token !== undefined && this.conf.token !== '') {
return {'Authorization': `Bearer ${this.conf.token}`};
}
return {};
}
#requestICEServers() { #requestICEServers() {
return fetch(this.conf.url, { return fetch(this.conf.url, {
method: 'OPTIONS', method: 'OPTIONS',
headers: {
...this.#authHeader(),
},
}) })
.then((res) => MediaMTXWebRTCReader.#linkToIceServers(res.headers.get('Link'))); .then((res) => MediaMTXWebRTCReader.#linkToIceServers(res.headers.get('Link')));
} }
@@ -446,7 +463,10 @@ class MediaMTXWebRTCReader {
return fetch(this.conf.url, { return fetch(this.conf.url, {
method: 'POST', method: 'POST',
headers: {'Content-Type': 'application/sdp'}, headers: {
...this.#authHeader(),
'Content-Type': 'application/sdp',
},
body: offer, body: offer,
}) })
.then((res) => { .then((res) => {