Skip to content

Commit 14a8e16

Browse files
committed
Add API key auth method
1 parent df58418 commit 14a8e16

File tree

8 files changed

+60
-13
lines changed

8 files changed

+60
-13
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
# Changelog
22
All changes to this project will be documented in this file.
33

4+
## [1.0.5] - 2022-03-24
5+
- Fix date attributes types
6+
- Add authentication using an API key
7+
48
## [1.0.4] - 2022-03-23
59
- Export `VideoUploadResponse` type
610

README.md

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
- [Options](#options)
2222
- [Using a delegated upload token (recommended):](#using-a-delegated-upload-token-recommended)
2323
- [Using an access token (discouraged):](#using-an-access-token-discouraged)
24+
- [Using an API key (**strongly** discouraged):](#using-an-api-key-strongly-discouraged)
2425
- [Common options](#common-options)
2526
- [Example](#example)
2627
- [Methods](#methods)
@@ -161,6 +162,18 @@ Using delegated upload tokens for authentication is best options when uploading
161162
| _common options (see bellow)_ | | | |
162163

163164

165+
#### Using an API key (**strongly** discouraged):
166+
167+
**Warning**: be aware that exposing your API key client-side can lead to huge security issues. Use this method only if you know what you're doing :).
168+
169+
170+
| Option name | Mandatory | Type | Description |
171+
| ----------------------------: | --------- | ------ | ----------------------- |
172+
| API Key | **yes** | string | your api.video API key |
173+
| videoId | **yes** | string | id of an existing video |
174+
| _common options (see bellow)_ | | | |
175+
176+
164177
#### Common options
165178

166179

@@ -262,10 +275,10 @@ Using delegated upload tokens for authentication is best options when uploading
262275
#### Common options
263276

264277

265-
| Option name | Mandatory | Type | Description |
266-
| ----------: | --------- | ------ | -------------------------------------------------------------------------- |
267-
| apiHost | no | string | api.video host (default: ws.api.video) |
268-
| retries | no | number | number of retries when an API call fails (default: 5) |
278+
| Option name | Mandatory | Type | Description |
279+
| ----------: | --------- | ------ | ----------------------------------------------------- |
280+
| apiHost | no | string | api.video host (default: ws.api.video) |
281+
| retries | no | number | number of retries when an API call fails (default: 5) |
269282

270283

271284
### Example

dist/index.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/src/progressive-video-uploader.d.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@ export interface ProgressiveUploaderOptionsWithAccessToken extends Options {
77
accessToken: string;
88
videoId: string;
99
}
10+
export interface ProgressiveUploaderOptionsWithApiKey extends Options {
11+
apiKey: string;
12+
videoId: string;
13+
}
1014
interface Options {
1115
apiHost?: string;
1216
retries?: number;
@@ -29,7 +33,7 @@ export declare class ProgressiveUploader {
2933
private currentPartBlobs;
3034
private currentPartBlobsSize;
3135
private queue;
32-
constructor(options: ProgressiveUploaderOptionsWithAccessToken | ProgressiveUploaderOptionsWithUploadToken);
36+
constructor(options: ProgressiveUploaderOptionsWithAccessToken | ProgressiveUploaderOptionsWithUploadToken | ProgressiveUploaderOptionsWithApiKey);
3337
onProgress(cb: (e: ProgressiveProgressEvent) => void): void;
3438
uploadPart(file: Blob): Promise<void>;
3539
uploadLastPart(file: Blob): Promise<VideoUploadResponse>;

dist/src/video-uploader.d.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@ export interface VideoUploaderOptionsWithAccessToken extends Options {
77
accessToken: string;
88
videoId: string;
99
}
10+
export interface VideoUploaderOptionsWithApiKey extends Options {
11+
apiKey: string;
12+
videoId: string;
13+
}
1014
interface Options {
1115
file: File;
1216
chunkSize?: number;
@@ -34,7 +38,7 @@ export declare class VideoUploader {
3438
private onProgressCallbacks;
3539
private headers;
3640
private queue;
37-
constructor(options: VideoUploaderOptionsWithAccessToken | VideoUploaderOptionsWithUploadToken);
41+
constructor(options: VideoUploaderOptionsWithAccessToken | VideoUploaderOptionsWithUploadToken | VideoUploaderOptionsWithApiKey);
3842
onProgress(cb: (e: UploadProgressEvent) => void): void;
3943
upload(): Promise<VideoUploadResponse>;
4044
private sleep;

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@api.video/video-uploader",
3-
"version": "1.0.4",
3+
"version": "1.0.5",
44
"description": "api.video video uploader",
55
"repository": {
66
"type": "git",

src/progressive-video-uploader.ts

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@ export interface ProgressiveUploaderOptionsWithAccessToken extends Options {
99
accessToken: string;
1010
videoId: string;
1111
}
12+
export interface ProgressiveUploaderOptionsWithApiKey extends Options {
13+
apiKey: string;
14+
videoId: string;
15+
}
1216
interface Options {
1317
apiHost?: string;
1418
retries?: number;
@@ -37,7 +41,7 @@ export class ProgressiveUploader {
3741
private currentPartBlobsSize = 0;
3842
private queue = new PromiseQueue();
3943

40-
constructor(options: ProgressiveUploaderOptionsWithAccessToken | ProgressiveUploaderOptionsWithUploadToken) {
44+
constructor(options: ProgressiveUploaderOptionsWithAccessToken | ProgressiveUploaderOptionsWithUploadToken | ProgressiveUploaderOptionsWithApiKey) {
4145
const apiHost = options.apiHost || DEFAULT_API_HOST;
4246

4347
if (options.hasOwnProperty("uploadToken")) {
@@ -54,8 +58,15 @@ export class ProgressiveUploader {
5458
}
5559
this.uploadEndpoint = `https://${apiHost}/videos/${optionsWithAccessToken.videoId}/source`;
5660
this.headers.Authorization = `Bearer ${optionsWithAccessToken.accessToken}`;
57-
} else {
58-
throw new Error(`You must provide either an accessToken or an uploadToken`);
61+
} else if (options.hasOwnProperty("apiKey")) {
62+
const optionsWithApiKey = options as ProgressiveUploaderOptionsWithApiKey;
63+
if (!optionsWithApiKey.videoId) {
64+
throw new Error("'videoId' is missing");
65+
}
66+
this.uploadEndpoint = `https://${apiHost}/videos/${optionsWithApiKey.videoId}/source`;
67+
this.headers.Authorization = `Basic ${btoa(optionsWithApiKey.apiKey + ":")}`;
68+
}else {
69+
throw new Error(`You must provide either an accessToken, an uploadToken or an API key`);
5970
}
6071

6172
this.retries = options.retries || DEFAULT_RETRIES;

src/video-uploader.ts

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@ export interface VideoUploaderOptionsWithAccessToken extends Options {
99
accessToken: string;
1010
videoId: string;
1111
}
12+
export interface VideoUploaderOptionsWithApiKey extends Options {
13+
apiKey: string;
14+
videoId: string;
15+
}
1216
interface Options {
1317
file: File;
1418
chunkSize?: number;
@@ -39,7 +43,7 @@ export class VideoUploader {
3943
private headers: { [name: string]: string } = {};
4044
private queue = new PromiseQueue();
4145

42-
constructor(options: VideoUploaderOptionsWithAccessToken | VideoUploaderOptionsWithUploadToken) {
46+
constructor(options: VideoUploaderOptionsWithAccessToken | VideoUploaderOptionsWithUploadToken | VideoUploaderOptionsWithApiKey) {
4347
const apiHost = options.apiHost || DEFAULT_API_HOST;
4448

4549
if (!options.file) {
@@ -60,8 +64,15 @@ export class VideoUploader {
6064
}
6165
this.uploadEndpoint = `https://${apiHost}/videos/${optionsWithAccessToken.videoId}/source`;
6266
this.headers.Authorization = `Bearer ${optionsWithAccessToken.accessToken}`;
67+
} else if (options.hasOwnProperty("apiKey")) {
68+
const optionsWithApiKey = options as VideoUploaderOptionsWithApiKey;
69+
if (!optionsWithApiKey.videoId) {
70+
throw new Error("'videoId' is missing");
71+
}
72+
this.uploadEndpoint = `https://${apiHost}/videos/${optionsWithApiKey.videoId}/source`;
73+
this.headers.Authorization = `Basic ${btoa(optionsWithApiKey.apiKey + ":")}`;
6374
} else {
64-
throw new Error(`You must provide either an accessToken or an uploadToken`);
75+
throw new Error(`You must provide either an accessToken, an uploadToken or an API key`);
6576
}
6677

6778
if(options.chunkSize && (options.chunkSize < MIN_CHUNK_SIZE || options.chunkSize > MAX_CHUNK_SIZE)) {

0 commit comments

Comments
 (0)