Skip to content

Commit 6b16a87

Browse files
committed
Merge #42: fix: [#41] add authentication bearer token to all requests
5536b1e fix: [#41] add authentication bearer token to all requests (Jose Celano) Pull request description: Relates to: torrust/torrust-index-gui#412 Add authentication bearer token to all requests. The Index does not have any session data on the backend. If the client does not provide the Authentication Bearer Token the request is processed as a anonymous request. Some endpoints like the one to download the torrent file return different data depending on wether the user is logged in or not. So we have to include always the token is it's available. ACKs for top commit: josecelano: ACK 5536b1e Tree-SHA512: bc2907486e5facfb232119c2322740897dea219506e5ff1a91b49c2da205fc79409ec063d44ca917bc8305f594fad2fcc1560a7c82dbf47275ec5d2bb970b751
2 parents abab43d + 5536b1e commit 6b16a87

File tree

2 files changed

+15
-10
lines changed

2 files changed

+15
-10
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "torrust-index-api-lib",
3-
"version": "1.0.0-alpha.4",
3+
"version": "1.0.0-alpha.5",
44
"description": "Contains API functions for the Torrust project.",
55
"repository": "https://github.com/torrust/torrust-index-api-lib",
66
"license": "SEE LICENSE IN COPYRIGHT",

src/modes/rest/resources/torrent.ts

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -70,9 +70,14 @@ export class TorrentResource implements IRestResource {
7070
this.client = client;
7171
}
7272

73+
headers(): HeadersInit | undefined {
74+
return this.client.authToken ? { "Authorization": `Bearer ${this.client.authToken}` } : undefined;
75+
}
76+
7377
async getTorrentInfo(infoHash: string): Promise<TorrentResponse> {
7478
return await fetchGet<GetTorrentResponse>(
75-
`${this.client.apiBaseUrl}/torrent/${infoHash}`
79+
`${this.client.apiBaseUrl}/torrent/${infoHash}`,
80+
this.headers()
7681
)
7782
.then((res) => {
7883
return Promise.resolve(res.data);
@@ -84,7 +89,8 @@ export class TorrentResource implements IRestResource {
8489

8590
async getTorrents(params: GetTorrentsParams): Promise<GetTorrentsResponseData> {
8691
return await fetchGet<GetTorrentsResponse>(
87-
`${this.client.apiBaseUrl}/torrents?page_size=${params.pageSize}&page=${params.page - 1}&sort=${params.sorting}${ params.categories ? "&categories=" + params.categories.join(",") : ""}${ params.tags ? "&tags=" + params.tags.join(",") : ""}${params.searchQuery ? "&search=" + params.searchQuery : ""}`
92+
`${this.client.apiBaseUrl}/torrents?page_size=${params.pageSize}&page=${params.page - 1}&sort=${params.sorting}${params.categories ? "&categories=" + params.categories.join(",") : ""}${params.tags ? "&tags=" + params.tags.join(",") : ""}${params.searchQuery ? "&search=" + params.searchQuery : ""}`,
93+
this.headers()
8894
)
8995
.then((res) => {
9096
return Promise.resolve(res.data);
@@ -98,7 +104,7 @@ export class TorrentResource implements IRestResource {
98104
return await fetchDelete<any, DeleteTorrentResponse>(
99105
`${this.client.apiBaseUrl}/torrent/${infoHash}`,
100106
{},
101-
{ "Authorization": `Bearer ${this.client.authToken}` }
107+
this.headers()
102108
)
103109
.then((res) => {
104110
return Promise.resolve(res.data);
@@ -112,7 +118,7 @@ export class TorrentResource implements IRestResource {
112118
return await fetchPut<UpdateTorrentParams, UpdateTorrentResponse>(
113119
`${this.client.apiBaseUrl}/torrent/${infoHash}`,
114120
params,
115-
{ "Authorization": `Bearer ${this.client.authToken}`, "Content-Type": "application/json" }
121+
this.headers()
116122
)
117123
.then((res) => {
118124
return Promise.resolve(res.data);
@@ -134,7 +140,7 @@ export class TorrentResource implements IRestResource {
134140
return await fetchPost<NewTorrentResponse>(
135141
`${this.client.apiBaseUrl}/torrent/upload`,
136142
formData,
137-
{ "Authorization": `Bearer ${this.client.authToken}` }
143+
this.headers()
138144
)
139145
.then((res) => {
140146
return Promise.resolve(res.data);
@@ -146,7 +152,8 @@ export class TorrentResource implements IRestResource {
146152

147153
async downloadTorrent(infoHash: string): Promise<Blob> {
148154
return await fetchGetBlob(
149-
`${this.client.apiBaseUrl}/torrent/download/${infoHash}`
155+
`${this.client.apiBaseUrl}/torrent/download/${infoHash}`,
156+
this.headers()
150157
)
151158
.then((blob) => {
152159
return Promise.resolve(blob);
@@ -157,11 +164,9 @@ export class TorrentResource implements IRestResource {
157164
}
158165

159166
async proxiedImage(url: string): Promise<Blob> {
160-
const headers = this.client.authToken ? { "Authorization": `Bearer ${this.client.authToken}` } : undefined;
161-
162167
return await fetchGetBlob(
163168
`${this.client.apiBaseUrl}/proxy/image/${encodeURIComponent(url)}`,
164-
headers
169+
this.headers()
165170
)
166171
.then((blob) => {
167172
return Promise.resolve(blob);

0 commit comments

Comments
 (0)