Skip to content

Commit

Permalink
Use crypto module for ekaterinburg.rf tokens (#83)
Browse files Browse the repository at this point in the history
  • Loading branch information
sashachabin authored Oct 11, 2023
1 parent 0cae326 commit 1d4185a
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 38 deletions.
46 changes: 21 additions & 25 deletions api/model/ekaterinburg-rf/ekaterinburg-rf.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import fetch from 'node-fetch';
import _ from 'lodash';
import sha1 from 'js-sha1';
import crypto from 'crypto';

import {
ServerRoute,
Expand Down Expand Up @@ -167,7 +167,7 @@ export class EkaterinburgRfModel {

this.incrementRequestId();

let { magicStr, guidStr } = shaenc(method, this.requestId, this.sid);
const token = getRequestToken(method, this.requestId, this.sid);

const requestBody = {
id: this.requestId,
Expand All @@ -176,7 +176,7 @@ export class EkaterinburgRfModel {
params: {
...params,
sid: this.sid,
magic: magicStr,
magic: token.magic,
},
};

Expand All @@ -186,7 +186,7 @@ export class EkaterinburgRfModel {
};

const requestUrl = new URL(marhsrutEkaterinburgRfJsonRpcLink);
requestUrl.searchParams.append('m', guidStr);
requestUrl.searchParams.append('m', token.guid);

let response = await fetch(requestUrl.href, fetchOptions);
let body = (await response.json()) as JsonRpcResponse<R>;
Expand All @@ -199,13 +199,13 @@ export class EkaterinburgRfModel {
requestBody.params.sid = this.sid;
requestBody.id = this.requestId;

({ magicStr, guidStr } = shaenc(method, this.requestId, this.sid));
requestBody.params.magic = magicStr;
const token = getRequestToken(method, this.requestId, this.sid);
requestBody.params.magic = token.magic;

fetchOptions.body = JSON.stringify(requestBody);

const retryRequestUrl = new URL(marhsrutEkaterinburgRfJsonRpcLink);
retryRequestUrl.searchParams.append('m', guidStr);
retryRequestUrl.searchParams.append('m', token.guid);

response = await fetch(retryRequestUrl.href, fetchOptions);
body = (await response.json()) as JsonRpcResponse<R>;
Expand Down Expand Up @@ -239,32 +239,28 @@ export class EkaterinburgRfModel {
}
}

// Getting magic values for requests to ekaterinburg.rf
function shaenc(method: JsonRpcMethods, id: number, sid: string) {
// connecting into one string
const str = method + '-' + id + '-' + sid;
// Getting request token for requests to ekaterinburg.rf
function getRequestToken(method: JsonRpcMethods, id: number, sid: string) {
const token = `${method}-${id}-${sid}`;
const tokenEnc = crypto.createHash('sha1').update(token).digest('hex');

// calculating hash
const shaStr = sha1(str);

// turn first and last 16 symbols into GUID
const guidStr =
shaStr.substr(0, 8) +
// transorm first and last 16 symbols into GUID
const guid =
tokenEnc.substr(0, 8) +
'-' +
shaStr.substr(8, 4) +
tokenEnc.substr(8, 4) +
'-' +
shaStr.substr(12, 4) +
tokenEnc.substr(12, 4) +
'-' +
shaStr.substr(24, 4) +
tokenEnc.substr(24, 4) +
'-' +
shaStr.substr(28, 12);
tokenEnc.substr(28, 12);

// turn 8 middle symbols into magic string
const magicStr = shaStr.substr(16, 8);
const magic = tokenEnc.substr(16, 8);

// formatting result
return {
magicStr,
guidStr,
guid,
magic
};
}
1 change: 0 additions & 1 deletion api/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
},
"dependencies": {
"express": "^4.18.2",
"js-sha1": "^0.6.0",
"lodash": "^4.17.21",
"node-fetch": "^2.7.0",
"transport-common": "workspace:*",
Expand Down
29 changes: 17 additions & 12 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 1d4185a

Please sign in to comment.