Skip to content

Commit

Permalink
Merge pull request #215 from digirati-co-uk/feature/token-encoding
Browse files Browse the repository at this point in the history
Token encoding
  • Loading branch information
stephenwf authored Oct 10, 2023
2 parents a9e4b84 + a2151f8 commit 21337fb
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 4 deletions.
8 changes: 7 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [Unreleased](https://github.com/digirati-co-uk/tasks-api/compare/v1.1.2...main)
## [Unreleased](https://github.com/digirati-co-uk/tasks-api/compare/v1.1.3...main)

<!--
### Added
Expand All @@ -15,6 +15,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Security
-->

## [v1.1.3](https://github.com/digirati-co-uk/tasks-api/compare/v1.1.2...v1.1.3)

### Fixed
- Fixed ascii characters in tokens
- Fixed Postgres connection timeout

## [v1.1.2](https://github.com/digirati-co-uk/tasks-api/compare/v1.1.1...v1.1.2)

### Added
Expand Down
5 changes: 4 additions & 1 deletion src/database/create-postgres-pool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ export function createPostgresPool(config: DBConfig) {
return createPool(
typeof config === 'string'
? config
: `postgres://${config.username}:${config.password}@${config.host}:${config.port}/${config.database}`
: `postgres://${config.username}:${config.password}@${config.host}:${config.port}/${config.database}`,
{
connectionTimeout: 'DISABLE_TIMEOUT',
}
);
}
7 changes: 6 additions & 1 deletion src/utility/parse-token.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { ApplicationState, JWTConfig, Scopes } from '../types';
import { safeJsonParse } from './safe-json-parse';

export function parseToken(
rawToken: string,
Expand All @@ -13,7 +14,11 @@ export function parseToken(

try {
const payload = Buffer.from(base64Payload, 'base64');
const token = JSON.parse(payload.toString('ascii'));
const tokenResp = safeJsonParse(payload.toString('utf-8'));
if (tokenResp.error) {
throw new Error(`Invalid token JSON encoding`);
}
const token = tokenResp.result;

if (!token || !token.sub || !token.scope || !token.iss) {
return;
Expand Down
9 changes: 9 additions & 0 deletions src/utility/safe-json-parse.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
export function safeJsonParse<T = any>(json: string): { result: T, error: false } | { error: true } {
try {
return { result: JSON.parse(json), error: false };
} catch (e) {
console.log('Error parsing JSON', e);
console.log(json);
return { error: true };
}
}
2 changes: 1 addition & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
"resolveJsonModule": true,
"noImplicitAny": true,
"downlevelIteration": true,
"noUnusedLocals": true,
"noUnusedLocals": false,
"noFallthroughCasesInSwitch": true,
"paths": {
// "PACKAGE_NAME": ["../SERVICE_NAME"]
Expand Down

0 comments on commit 21337fb

Please sign in to comment.