Skip to content

Commit

Permalink
decode organisation details from access_token
Browse files Browse the repository at this point in the history
  • Loading branch information
jessicamcinchak committed Dec 15, 2023
1 parent f48f5c8 commit ebe2611
Showing 1 changed file with 12 additions and 11 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

0 comments on commit ebe2611

Please sign in to comment.