Skip to content

Commit

Permalink
EW-995 added tsp rest client
Browse files Browse the repository at this point in the history
  • Loading branch information
Fshmit committed Sep 11, 2024
1 parent c7708ca commit 11bffa9
Showing 1 changed file with 66 additions and 0 deletions.
66 changes: 66 additions & 0 deletions apps/server/src/infra/tsp-client/tsp-rest-client.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
import { HttpService } from '@nestjs/axios';
import moment from 'moment';
import { v4 as uuidv4 } from 'uuid';
import * as jwt from 'jsonwebtoken';

export class TSPRestClient {
// private readonly TSP_API_BASE_URL: string;
// private readonly TSP_API_CLIENT_ID: string;
// private readonly TSP_API_CLIENT_SECRET: string;
// private readonly TSP_API_TOKEN_ENDPOINT: string;

private lastToken!: string;

private lastTokenExpires!: number;

constructor(private readonly httpService: HttpService) {}

private getJwt(lifetime = 30000): string {
const issueDate = Date.now();

// check if the current token is still valid
if (issueDate < this.lastTokenExpires - 1000) {
return this.lastToken;
}

// update the token and expiration date
this.lastTokenExpires = issueDate + lifetime;

// create the payload for the jwt
const payload = {
apiClientSecret: 'secret', // TSP_API_CLIENT_SECRET
iss: 'locahost', // process.env.SC_DOMAIN
aud: 'base_url', // TSP_API_BASE_URL
sub: 'host', // process.env.HOST
exp: issueDate + lifetime,
iat: issueDate,
jti: uuidv4(),
};

const token: string = jwt.sign(payload, 'secret');

Check failure

Code scanning / CodeQL

Hard-coded credentials Critical

The hard-coded value "secret" is used as
jwt key
.

// store the token for future use
this.lastToken = token;

return token;
}

private getHeaders(): Record<string, string> {
const token = this.getJwt();
return { Authorization: `AUTH-JWT apiClientId=${'client_id'},jwt=${token}` };
}

public request<T>(path: string, lastChange: Date = new Date(0)): Promise<T> {
const lastChangeDate = moment(lastChange).format('YYYY-MM-DD HH:mm:ss.SSS');
const requestUrl = new URL('base_url', path); // TSP_API_BASE_URL

const response = this.httpService.get(requestUrl.toString(), {
headers: this.getHeaders(),
params: {
dtLetzteAenderung: lastChangeDate,
},
});

return response.data;

Check failure on line 64 in apps/server/src/infra/tsp-client/tsp-rest-client.ts

View workflow job for this annotation

GitHub Actions / nest_lint

Unsafe return of an `any` typed value
}
}

0 comments on commit 11bffa9

Please sign in to comment.