Skip to content

Commit

Permalink
fix: decode organisation details from access_token for Uniform auth…
Browse files Browse the repository at this point in the history
…entication (#2571)
  • Loading branch information
jessicamcinchak authored Dec 15, 2023
1 parent 0864da6 commit cf8a6ab
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 15 deletions.
23 changes: 12 additions & 11 deletions api.planx.uk/modules/send/uniform/uniform.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import axios, { AxiosRequestConfig, isAxiosError } from "axios";
import { NextFunction, Request, Response } from "express";
import { Buffer } from "node:buffer";
import FormData from "form-data";
import fs from "fs";
import { markSessionAsSubmitted } from "../../saveAndReturn/service/utils";
import { gql } from "graphql-request";
import jwt from "jsonwebtoken";
import { Buffer } from "node:buffer";
import { $api } from "../../../client";
import { markSessionAsSubmitted } from "../../saveAndReturn/service/utils";
import { buildSubmissionExportZip } from "../utils/exportZip";

interface UniformClient {
Expand All @@ -21,8 +22,6 @@ interface UniformSubmissionResponse {

interface RawUniformAuthResponse {
access_token: string;
"organisation-name": string;
"organisation-id": string;
}

interface UniformAuthResponse {
Expand Down Expand Up @@ -204,19 +203,21 @@ async function authenticate({
throw Error("Failed to authenticate to Uniform - no access token returned");
}

if (
!response.data["organisation-name"] ||
!response.data["organisation-id"]
) {
// Decode access_token to get "organisation-name" & "organisation-id"
const decodedAccessToken = jwt.decode(response.data.access_token) as any;
const organisation = decodedAccessToken?.["organisation-name"];
const organisationId = decodedAccessToken?.["organisation-id"];

if (!organisation || !organisationId) {
throw Error(
"Failed to authenticate to Uniform - no organisation details returned",
"Failed to authenticate to Uniform - failed to decode organisation details from access_token",
);
}

const uniformAuthResponse: UniformAuthResponse = {
token: response.data.access_token,
organisation: response.data["organisation-name"],
organisationId: response.data["organisation-id"],
organisation: organisation,
organisationId: organisationId,
};

return uniformAuthResponse;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
"messsage": "MOCKED RESPONSE"
}
# UNIFORM token generation
# UNIFORM token generation, access_token is mock JWT with "organisation-name" & "organisation-id" properties
- request:
method: POST
path: /
Expand All @@ -21,9 +21,9 @@
Content-Type: application/json
body: >
{
"access_token": "TEST_TOKEN",
"organisation-name": "MOCKED",
"organisation-id": "MOCKED"
"access_token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJvcmdhbmlzYXRpb24tbmFtZSI6Ik1PQ0tFRCIsIm9yZ2FuaXNhdGlvbi1pZCI6Ik1PQ0tFRCJ9.p0DE8MUc9obE751XWOYPQWWtLXtq8-kJMPre4VuOBHg",
"token_type": "Bearer",
"expires_in": 35999
}
# UNIFORM submissions
Expand Down

0 comments on commit cf8a6ab

Please sign in to comment.