Skip to content

fix: [#41] add authentication bearer token to all requests #42

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
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
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "torrust-index-api-lib",
"version": "1.0.0-alpha.4",
"version": "1.0.0-alpha.5",
"description": "Contains API functions for the Torrust project.",
"repository": "https://github.com/torrust/torrust-index-api-lib",
"license": "SEE LICENSE IN COPYRIGHT",
Expand Down
23 changes: 14 additions & 9 deletions src/modes/rest/resources/torrent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,14 @@ export class TorrentResource implements IRestResource {
this.client = client;
}

headers(): HeadersInit | undefined {
return this.client.authToken ? { "Authorization": `Bearer ${this.client.authToken}` } : undefined;
}

async getTorrentInfo(infoHash: string): Promise<TorrentResponse> {
return await fetchGet<GetTorrentResponse>(
`${this.client.apiBaseUrl}/torrent/${infoHash}`
`${this.client.apiBaseUrl}/torrent/${infoHash}`,
this.headers()
)
.then((res) => {
return Promise.resolve(res.data);
Expand All @@ -84,7 +89,8 @@ export class TorrentResource implements IRestResource {

async getTorrents(params: GetTorrentsParams): Promise<GetTorrentsResponseData> {
return await fetchGet<GetTorrentsResponse>(
`${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 : ""}`
`${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 : ""}`,
this.headers()
)
.then((res) => {
return Promise.resolve(res.data);
Expand All @@ -98,7 +104,7 @@ export class TorrentResource implements IRestResource {
return await fetchDelete<any, DeleteTorrentResponse>(
`${this.client.apiBaseUrl}/torrent/${infoHash}`,
{},
{ "Authorization": `Bearer ${this.client.authToken}` }
this.headers()
)
.then((res) => {
return Promise.resolve(res.data);
Expand All @@ -112,7 +118,7 @@ export class TorrentResource implements IRestResource {
return await fetchPut<UpdateTorrentParams, UpdateTorrentResponse>(
`${this.client.apiBaseUrl}/torrent/${infoHash}`,
params,
{ "Authorization": `Bearer ${this.client.authToken}`, "Content-Type": "application/json" }
this.headers()
)
.then((res) => {
return Promise.resolve(res.data);
Expand All @@ -134,7 +140,7 @@ export class TorrentResource implements IRestResource {
return await fetchPost<NewTorrentResponse>(
`${this.client.apiBaseUrl}/torrent/upload`,
formData,
{ "Authorization": `Bearer ${this.client.authToken}` }
this.headers()
)
.then((res) => {
return Promise.resolve(res.data);
Expand All @@ -146,7 +152,8 @@ export class TorrentResource implements IRestResource {

async downloadTorrent(infoHash: string): Promise<Blob> {
return await fetchGetBlob(
`${this.client.apiBaseUrl}/torrent/download/${infoHash}`
`${this.client.apiBaseUrl}/torrent/download/${infoHash}`,
this.headers()
)
.then((blob) => {
return Promise.resolve(blob);
Expand All @@ -157,11 +164,9 @@ export class TorrentResource implements IRestResource {
}

async proxiedImage(url: string): Promise<Blob> {
const headers = this.client.authToken ? { "Authorization": `Bearer ${this.client.authToken}` } : undefined;

return await fetchGetBlob(
`${this.client.apiBaseUrl}/proxy/image/${encodeURIComponent(url)}`,
headers
this.headers()
)
.then((blob) => {
return Promise.resolve(blob);
Expand Down