Skip to content

Commit

Permalink
fixed incorrect generation of Sec-Vtrans-Token (#36)
Browse files Browse the repository at this point in the history
  • Loading branch information
ilyhalight committed Nov 5, 2024
1 parent 23c2ae8 commit 3c2903b
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 33 deletions.
63 changes: 30 additions & 33 deletions src/client.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import config from "./config/config";

import { yandexProtobuf } from "./protobuf";
import { getSignature, getUUID } from "./secure";
import { getSecYaHeaders, getSignature, getUUID } from "./secure";
import type {
VOTOpts,
FetchFunction,
Expand Down Expand Up @@ -66,7 +66,6 @@ export default class VOTClient {
responseLang: ResponseLang;

userAgent: string = config.userAgent;
componentVersion: string = config.componentVersion;

paths = {
videoTranslation: "/video-translation/translate",
Expand Down Expand Up @@ -280,7 +279,7 @@ export default class VOTClient {
}: VideoTranslationOpts): Promise<VideoTranslationResponse> {
const { url, duration = config.defaultDuration } = videoData;

const { secretKey, uuid } = await this.getSession("video-translation");
const session = await this.getSession("video-translation");
const body = yandexProtobuf.encodeTranslationRequest(
url,
duration,
Expand All @@ -289,11 +288,11 @@ export default class VOTClient {
translationHelp,
);

const sign = await getSignature(body);
const res = await this.request(this.paths.videoTranslation, body, {
"Vtrans-Signature": sign,
"Sec-Vtrans-Sk": secretKey,
"Sec-Vtrans-Token": `${sign}:${uuid}:${this.paths.videoTranslation}:${this.componentVersion}`,
const path = this.paths.videoTranslation;
const vtransHeaders = await getSecYaHeaders("Vtrans", session, body, path);

const res = await this.request(path, body, {
...vtransHeaders,
...headers,
});

Expand Down Expand Up @@ -461,20 +460,20 @@ export default class VOTClient {
translationId: string,
headers: Record<string, string> = {},
) {
const { secretKey, uuid } = await this.getSession("video-translation");
const session = await this.getSession("video-translation");
const body = yandexProtobuf.encodeTranslationAudioRequest(
url,
translationId,
);

const sign = await getSignature(body);
const path = this.paths.videoTranslationAudio;
const vtransHeaders = await getSecYaHeaders("Vtrans", session, body, path);

const res = await this.request(
this.paths.videoTranslationAudio,
path,
body,
{
"Vtrans-Signature": sign,
"Sec-Vtrans-Sk": secretKey,
"Sec-Vtrans-Token": `${sign}:${uuid}:${this.paths.videoTranslationAudio}:${this.componentVersion}`,
...vtransHeaders,
...headers,
},
"PUT",
Expand Down Expand Up @@ -530,15 +529,14 @@ export default class VOTClient {
throw new VOTJSError("Unsupported video URL for getting subtitles");
}

const { secretKey, uuid } = await this.getSession("video-translation");
const session = await this.getSession("video-translation");
const body = yandexProtobuf.encodeSubtitlesRequest(url, requestLang);

const sign = await getSignature(body);
const path = this.paths.videoSubtitles;
const vsubsHeaders = await getSecYaHeaders("Vsubs", session, body, path);

const res = await this.request(this.paths.videoSubtitles, body, {
"Vsubs-Signature": await getSignature(body),
"Sec-Vsubs-Sk": secretKey,
"Sec-Vsubs-Token": `${sign}:${uuid}:${this.paths.videoSubtitles}:${this.componentVersion}`,
const res = await this.request(path, body, {
...vsubsHeaders,
...headers,
});

Expand All @@ -553,14 +551,14 @@ export default class VOTClient {
* @includeExample examples/stream.ts:7-44
*/
async pingStream({ pingId, headers = {} }: StreamPingOptions) {
const { secretKey, uuid } = await this.getSession("video-translation");
const session = await this.getSession("video-translation");
const body = yandexProtobuf.encodeStreamPingRequest(pingId);

const sign = await getSignature(body);
const res = await this.request(this.paths.streamPing, body, {
"Vtrans-Signature": await getSignature(body),
"Sec-Vtrans-Sk": secretKey,
"Sec-Vtrans-Token": `${sign}:${uuid}:${this.paths.streamPing}:${this.componentVersion}`,
const path = this.paths.streamPing;
const vtransHeaders = await getSecYaHeaders("Vtrans", session, body, path);

const res = await this.request(path, body, {
...vtransHeaders,
...headers,
});

Expand Down Expand Up @@ -588,19 +586,18 @@ export default class VOTClient {
);
}

const { secretKey, uuid } = await this.getSession("video-translation");

const session = await this.getSession("video-translation");
const body = yandexProtobuf.encodeStreamRequest(
url,
requestLang,
responseLang,
);

const sign = await getSignature(body);
const res = await this.request(this.paths.streamTranslation, body, {
"Vtrans-Signature": await getSignature(body),
"Sec-Vtrans-Sk": secretKey,
"Sec-Vtrans-Token": `${sign}:${uuid}:${this.paths.streamTranslation}:${this.componentVersion}`,
const path = this.paths.streamTranslation;
const vtransHeaders = await getSecYaHeaders("Vtrans", session, body, path);

const res = await this.request(path, body, {
...vtransHeaders,
...headers,
});

Expand Down
23 changes: 23 additions & 0 deletions src/secure.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import crypto from "node:crypto";
import config from "./config/config";
import Logger from "./utils/logger";
import { ClientSession } from "./types/client";
import { SecType } from "./types/yandex";

const utf8Encoder = new TextEncoder();
type HashName = "SHA-256" | "SHA-1";
Expand Down Expand Up @@ -32,6 +34,27 @@ export async function getSignature(body: Uint8Array) {
);
}

export async function getSecYaHeaders(
secType: SecType,
session: ClientSession,
body: Uint8Array,
path: string,
) {
const { secretKey, uuid } = session;
const sign = await getSignature(body);

// https://github.com/FOSWLY/vot.js/issues/36
const token = `${uuid}:${path}:${config.componentVersion}`;
const tokenBody = utf8Encoder.encode(token);
const tokenSign = await getSignature(tokenBody);

return {
[`${secType}-Signature`]: sign,
[`Sec-${secType}-Sk`]: secretKey,
[`Sec-${secType}-Token`]: `${tokenSign}:${token}`,
};
}

// yandex uuid
export function getUUID() {
const hexDigits = "0123456789ABCDEF";
Expand Down
1 change: 1 addition & 0 deletions src/types/yandex.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { availableLangs, availableTTS } from "../consts";

export type TranslationHelpTarget = "video_file_url" | "subtitles_file_url";
export type SessionModule = "video-translation" | "summarization";
export type SecType = "Vtrans" | "Vsubs" | "Summary";

export type TranslationHelp = {
target: TranslationHelpTarget;
Expand Down

0 comments on commit 3c2903b

Please sign in to comment.