Skip to content

Commit

Permalink
Update ekaterinburg-rf backend API (#96)
Browse files Browse the repository at this point in the history
* Update ekaterinburg-rf backend API

* Move getRequestToken to separate token
  • Loading branch information
sashachabin authored Dec 2, 2023
1 parent a92fab6 commit 40eda84
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 30 deletions.
2 changes: 1 addition & 1 deletion api/model/ekaterinburg-rf/ekaterinburg-rf.constants.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
export const marhsrutEkaterinburgRfJsonRpcLink =
'http://xn--80axnakf7a.xn--80acgfbsl1azdqr.xn--p1ai/api/rpc.php';
export const jsonrpc = '2.0';
export const jsonrpc = '2.1';
export const responseType = 'json';

export enum JsonRpcMethods {
Expand Down
38 changes: 9 additions & 29 deletions api/model/ekaterinburg-rf/ekaterinburg-rf.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import fetch from 'node-fetch';
import _ from 'lodash';
import crypto from 'crypto';

import {
ServerRoute,
ServerStopArriveUnit,
ServerUnitArrive,
} from 'transport-common/types/ekaterinburg-rf';

import { ClientUnit, TransportTree } from 'transport-common/types/masstrans';
import { createStrapiMethods } from 'transport-common/strapi/create-methods';
import { StrapiContentTypes, StrapiTree } from 'transport-common/types/strapi';
Expand All @@ -16,6 +16,7 @@ import {
JsonRpcMethods,
marhsrutEkaterinburgRfJsonRpcLink,
} from './ekaterinburg-rf.constants';

import {
JsonRpcResponse,
GetUnitsResponse,
Expand All @@ -25,6 +26,9 @@ import {
GetStopArriveResponse,
} from './ekaterinburg-rf.types';

import { getCurrentTimestamp } from '../../utils/get-current-timestamp';
import { getRequestToken } from '../../utils/get-request-token';

const fetchCommonOptions = {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
Expand Down Expand Up @@ -152,11 +156,11 @@ export class EkaterinburgRfModel {
jsonrpc,
method: JsonRpcMethods.StartSession,
params: {},
ts: getCurrentTimestamp()
}),
});

const body = (await response.json()) as JsonRpcResponse<InitSessionResponse>;

this.sid = body.result.sid;
}

Expand All @@ -170,15 +174,17 @@ export class EkaterinburgRfModel {
const token = getRequestToken(method, this.requestId, this.sid);

const requestBody = {
id: this.requestId,
jsonrpc,
method,
id: this.requestId,
ts: getCurrentTimestamp(),
params: {
...params,
sid: this.sid,
magic: token.magic,
},
};


const fetchOptions = {
...fetchCommonOptions,
Expand Down Expand Up @@ -238,29 +244,3 @@ export class EkaterinburgRfModel {
this.requestId++;
}
}

// 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');

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

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

return {
guid,
magic
};
}
12 changes: 12 additions & 0 deletions api/utils/get-current-timestamp.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
let lastTimestamp = 0;

export function getCurrentTimestamp() {
let timestamp = Math.floor((new Date).getTime() / 1000);

if (timestamp <= lastTimestamp) {
timestamp++;
}

lastTimestamp = timestamp;
return timestamp;
}
29 changes: 29 additions & 0 deletions api/utils/get-request-token.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import crypto from 'crypto';
import type { JsonRpcMethods } from "../model/ekaterinburg-rf/ekaterinburg-rf.constants";

const EKATERINBURG_TOKEN = 'ekt';

export function getRequestToken(method: JsonRpcMethods, id: number, sid: string) {
const token = `${method}~${EKATERINBURG_TOKEN}~${id}~${sid}`;
const tokenEnc = crypto.createHash('sha1').update(token).digest('hex');

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

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

return {
guid,
magic
};
}

0 comments on commit 40eda84

Please sign in to comment.