Skip to content

Commit 51c4b7b

Browse files
Add authorization for delete endpoint in SDK (#145)
* fix: add authorization for delete endpoint in SDK (#12) * fix: don't use Authorization header if no auth key was provided (#12) --------- Co-authored-by: Frittman Detre <detre.frittman@scolia.hu>
1 parent 0e2b801 commit 51c4b7b

File tree

3 files changed

+38
-17
lines changed

3 files changed

+38
-17
lines changed

packages/sdk/spec/WHIPClient.spec.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -470,7 +470,7 @@ describe('WHIP Client', () => {
470470
});
471471

472472
it('Does not close peer connection when connection state transitions to disconnected', async () => {
473-
when(whipProtocol.delete(anyString()))
473+
when(whipProtocol.delete(anyString(), undefined))
474474
.thenResolve(instance(response));
475475

476476
when(rtcPeerConnection.connectionState)
@@ -484,7 +484,7 @@ describe('WHIP Client', () => {
484484
})
485485

486486
it('Closes peer connection when connection state transitions to failed', async () => {
487-
when(whipProtocol.delete(anyString()))
487+
when(whipProtocol.delete(anyString(), undefined))
488488
.thenResolve(instance(response));
489489

490490
when(rtcPeerConnection.connectionState)

packages/sdk/src/WHIPProtocol.ts

Lines changed: 35 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,60 @@
11
export class WHIPProtocol {
22

33
sendOffer(url: string, authKey: string | undefined, sdp: string): Promise<Response> {
4+
const headers = {
5+
"Content-Type": "application/sdp",
6+
};
7+
8+
if (authKey) {
9+
headers['Authorization'] = `Bearer ${authKey}`;
10+
}
11+
412
return fetch(url, {
513
method: "POST",
6-
headers: {
7-
"Content-Type": "application/sdp",
8-
"Authorization": `Bearer ${authKey}`
9-
},
14+
headers,
1015
body: sdp
1116
});
1217
}
1318

1419
getConfiguration(url: string, authKey: string | undefined): Promise<Response> {
20+
const headers = {};
21+
22+
if (authKey) {
23+
headers['Authorization'] = `Bearer ${authKey}`;
24+
}
25+
1526
return fetch(url, {
1627
method: "OPTIONS",
17-
headers: {
18-
"Authorization": `Bearer ${authKey}`,
19-
}
28+
headers
2029
});
2130
}
2231

23-
delete(url: string): Promise<Response> {
32+
delete(url: string, authKey: string | undefined): Promise<Response> {
33+
const headers = {};
34+
35+
if (authKey) {
36+
headers['Authorization'] = `Bearer ${authKey}`;
37+
}
38+
2439
return fetch(url, {
25-
method: "DELETE"
40+
method: "DELETE",
41+
headers
2642
});
2743
}
2844

2945
updateIce(url: string, eTag: string, sdp: string, authKey: string | undefined): Promise<Response> {
46+
const headers = {
47+
"Content-Type": "application/trickle-ice-sdpfrag",
48+
"ETag": eTag,
49+
};
50+
51+
if (authKey) {
52+
headers['Authorization'] = `Bearer ${authKey}`;
53+
}
54+
3055
return fetch(url, {
3156
method: "PATCH",
32-
headers: {
33-
"Content-Type": "application/trickle-ice-sdpfrag",
34-
"ETag": eTag,
35-
"Authorization": `Bearer ${authKey}`,
36-
},
57+
headers,
3758
body: sdp
3859
});
3960
}

packages/sdk/src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -384,7 +384,7 @@ export class WHIPClient extends EventEmitter {
384384
*/
385385
async destroy(): Promise<void> {
386386
const resourceUrl = await this.getResourceUrl();
387-
await this.whipProtocol.delete(resourceUrl).catch((e) => this.error("destroy()", e));
387+
await this.whipProtocol.delete(resourceUrl, this.opts.authkey).catch((e) => this.error("destroy()", e));
388388

389389
const senders = this.peer.getSenders();
390390
if (senders) {

0 commit comments

Comments
 (0)