Skip to content

Commit

Permalink
Merge pull request #1635 from govuk-one-login/AUT-2578/use-common-hea…
Browse files Browse the repository at this point in the history
…ders-in-all-services

Aut 2578/use common headers in all services
  • Loading branch information
BeckaL authored May 22, 2024
2 parents b8b7608 + 4726215 commit 20a7a10
Show file tree
Hide file tree
Showing 82 changed files with 645 additions and 362 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ export const resendMfaCodePost = (
req.ip,
persistentSessionId,
xss(req.cookies.lng as string),
req,
journeyType,
phoneNumber
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ describe("resend mfa controller", () => {
"127.0.0.1",
"123123-djjad",
"",
req,
"ACCOUNT_RECOVERY"
);
});
Expand Down Expand Up @@ -110,6 +111,7 @@ describe("resend mfa controller", () => {
"127.0.0.1",
"123123-djjad",
"",
req,
"REGISTRATION"
);
});
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {
createApiResponse,
getRequestConfig,
getInternalRequestConfigWithSecurityHeaders,
Http,
http,
} from "../../utils/http";
Expand All @@ -10,6 +10,7 @@ import {
AccountInterventionsInterface,
} from "./types";
import { ApiResponseResult } from "../../types";
import { Request } from "express";

export function accountInterventionService(
axios: Http = http
Expand All @@ -19,19 +20,24 @@ export function accountInterventionService(
emailAddress: string,
sourceIp: string,
clientSessionId: string,
persistentSessionId: string
persistentSessionId: string,
req: Request
): Promise<ApiResponseResult<AccountInterventionStatus>> {
const response = await axios.client.post<AccountInterventionStatus>(
API_ENDPOINTS.ACCOUNT_INTERVENTIONS,
{
email: emailAddress.toLowerCase(),
},
getRequestConfig({
sessionId: sessionId,
sourceIp: sourceIp,
clientSessionId: clientSessionId,
persistentSessionId: persistentSessionId,
})
getInternalRequestConfigWithSecurityHeaders(
{
sessionId: sessionId,
sourceIp: sourceIp,
clientSessionId: clientSessionId,
persistentSessionId: persistentSessionId,
},
req,
API_ENDPOINTS.ACCOUNT_INTERVENTIONS
)
);

return createApiResponse<AccountInterventionStatus>(response);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,14 @@ import { accountInterventionService } from "../account-intervention-service";
import {
checkApiCallMadeWithExpectedBodyAndHeaders,
commonVariables,
expectedHeadersFromCommonVarsWithoutSecurityHeaders,
expectedHeadersFromCommonVarsWithSecurityHeaders,
requestHeadersWithIpAndAuditEncoded,
resetApiKeyAndBaseUrlEnvVars,
setupApiKeyAndBaseUrlEnvVars,
} from "../../../../test/helpers/service-test-helper";
import { API_ENDPOINTS } from "../../../app.constants";
import { API_ENDPOINTS, PATH_NAMES } from "../../../app.constants";
import { Http } from "../../../utils/http";
import { createMockRequest } from "../../../../test/helpers/mock-request-helper";

describe("account interventions service", () => {
const httpInstance = new Http();
Expand All @@ -29,26 +31,30 @@ describe("account interventions service", () => {
});

it("successfully calls the API to check a user's account interventions status", async () => {
const { sessionId, clientSessionId, email, ip, diPersistentSessionId } =
commonVariables;
const req = createMockRequest(PATH_NAMES.AUTH_CODE, {
headers: requestHeadersWithIpAndAuditEncoded,
});
const axiosResponse = Promise.resolve({
data: {},
status: 200,
statusText: "OK",
});
postStub.resolves(axiosResponse);
const { sessionId, clientSessionId, email, ip, diPersistentSessionId } =
commonVariables;

const result = await service.accountInterventionStatus(
sessionId,
email,
ip,
clientSessionId,
diPersistentSessionId
diPersistentSessionId,
req
);

const expectedApiCallDetails = {
expectedPath: API_ENDPOINTS.ACCOUNT_INTERVENTIONS,
expectedHeaders: expectedHeadersFromCommonVarsWithoutSecurityHeaders,
expectedHeaders: expectedHeadersFromCommonVarsWithSecurityHeaders,
expectedBody: { email: commonVariables.email },
};

Expand Down
4 changes: 3 additions & 1 deletion src/components/account-intervention/types.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
import { ApiResponseResult, DefaultApiResponse } from "../../types";
import { Request } from "express";

export interface AccountInterventionsInterface {
accountInterventionStatus: (
sessionId: string,
emailAddress: string,
sourceIp: string,
clientSessionId: string,
persistentSessionId: string
persistentSessionId: string,
req: Request
) => Promise<ApiResponseResult<AccountInterventionStatus>>;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ export function accountNotFoundPost(
req.ip,
persistentSessionId,
xss(req.cookies.lng as string),
req,
JOURNEY_TYPE.REGISTRATION
);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ export function sendEmailOtp(
req.ip,
persistentSessionId,
xss(req.cookies.lng as string),
req,
JOURNEY_TYPE.ACCOUNT_RECOVERY
);

Expand Down
1 change: 1 addition & 0 deletions src/components/authorize/authorize-controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ export function authorizeGet(
clientSessionId,
req.ip,
persistentSessionId,
req,
claims.reauthenticate
);

Expand Down
22 changes: 14 additions & 8 deletions src/components/authorize/authorize-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@ import { ApiResponseResult } from "../../types";
import { API_ENDPOINTS } from "../../app.constants";
import {
createApiResponse,
getRequestConfig,
getInternalRequestConfigWithSecurityHeaders,
http,
Http,
} from "../../utils/http";
import { supportReauthentication } from "../../config";
import { Request } from "express";

export function authorizeService(
axios: Http = http
Expand All @@ -17,6 +18,7 @@ export function authorizeService(
clientSessionId: string,
sourceIp: string,
persistentSessionId: string,
req: Request,
reauthenticate?: string
): Promise<ApiResponseResult<StartAuthResponse>> {
let reauthenticateOption = undefined;
Expand All @@ -25,13 +27,17 @@ export function authorizeService(
}
const response = await axios.client.get<StartAuthResponse>(
API_ENDPOINTS.START,
getRequestConfig({
sessionId: sessionId,
clientSessionId: clientSessionId,
sourceIp: sourceIp,
persistentSessionId: persistentSessionId,
reauthenticate: reauthenticateOption,
})
getInternalRequestConfigWithSecurityHeaders(
{
sessionId: sessionId,
clientSessionId: clientSessionId,
sourceIp: sourceIp,
persistentSessionId: persistentSessionId,
reauthenticate: reauthenticateOption,
},
req,
API_ENDPOINTS.START
)
);

return createApiResponse<StartAuthResponse>(response);
Expand Down
49 changes: 32 additions & 17 deletions src/components/authorize/tests/authorize-service.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,44 +3,58 @@ import { expect } from "chai";
import { Http } from "../../../utils/http";
import { authorizeService } from "../authorize-service";
import { sinon } from "../../../../test/utils/test-utils";
import { API_ENDPOINTS } from "../../../app.constants";
import { API_ENDPOINTS, PATH_NAMES } from "../../../app.constants";
import { SinonStub } from "sinon";
import { AuthorizeServiceInterface } from "../types";
import { createMockRequest } from "../../../../test/helpers/mock-request-helper";
import {
commonVariables,
expectedHeadersFromCommonVarsWithSecurityHeaders,
requestHeadersWithIpAndAuditEncoded,
resetApiKeyAndBaseUrlEnvVars,
setupApiKeyAndBaseUrlEnvVars,
} from "../../../../test/helpers/service-test-helper";

describe("authorize service", () => {
const sessionId = "some-session-id";
const clientSessionId = "client-session-id";
const ip = "123.123.123.123";
const persistentSessionId = "persistent-session-id";
let getStub: SinonStub;
let service: AuthorizeServiceInterface;
const { sessionId, clientSessionId, ip, diPersistentSessionId } =
commonVariables;
const req = createMockRequest(PATH_NAMES.AUTHORIZE, {
headers: requestHeadersWithIpAndAuditEncoded,
});

beforeEach(() => {
process.env.API_KEY = "api-key";
process.env.FRONTEND_API_BASE_URL = "some-base-url";
setupApiKeyAndBaseUrlEnvVars();
process.env.API_BASE_URL = "another-base-url";
const httpInstance = new Http();
service = authorizeService(httpInstance);
getStub = sinon.stub(httpInstance.client, "get");
});

afterEach(() => {
resetApiKeyAndBaseUrlEnvVars();
delete process.env.SUPPORT_REAUTHENTICATION;
getStub.reset();
});

it("sends a request with the reauth header set to true when reauth is requested and the feature flag is set", () => {
it("sends a request with the correct headers set to true when reauth is requested and the feature flag is set", () => {
process.env.SUPPORT_REAUTHENTICATION = "1";
service.start(
sessionId,
clientSessionId,
ip,
persistentSessionId,
diPersistentSessionId,
req,
"123456"
);

expect(
getStub.calledWithMatch(API_ENDPOINTS.START, {
headers: { Reauthenticate: true },
getStub.calledOnceWithExactly(API_ENDPOINTS.START, {
headers: {
...expectedHeadersFromCommonVarsWithSecurityHeaders,
Reauthenticate: true,
},
proxy: false,
})
).to.be.true;
Expand All @@ -52,25 +66,26 @@ describe("authorize service", () => {
sessionId,
clientSessionId,
ip,
persistentSessionId,
diPersistentSessionId,
req,
"123456"
);

expect(
getStub.calledWithMatch(API_ENDPOINTS.START, {
headers: { Reauthenticate: undefined },
getStub.calledOnceWithExactly(API_ENDPOINTS.START, {
headers: { ...expectedHeadersFromCommonVarsWithSecurityHeaders },
proxy: false,
})
).to.be.true;
});

it("sends a request without a reauth header when reauth is not requested", () => {
process.env.SUPPORT_REAUTHENTICATION = "1";
service.start(sessionId, clientSessionId, ip, persistentSessionId);
service.start(sessionId, clientSessionId, ip, diPersistentSessionId, req);

expect(
getStub.calledWithMatch(API_ENDPOINTS.START, {
headers: { Reauthenticate: undefined },
getStub.calledOnceWithExactly(API_ENDPOINTS.START, {
headers: { ...expectedHeadersFromCommonVarsWithSecurityHeaders },
proxy: false,
})
).to.be.true;
Expand Down
2 changes: 2 additions & 0 deletions src/components/authorize/types.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { ApiResponseResult, DefaultApiResponse } from "../../types";
import { Claims } from "./claims-config";
import { Request } from "express";

export interface StartAuthResponse extends DefaultApiResponse {
user: UserSessionInfo;
Expand All @@ -23,6 +24,7 @@ export interface AuthorizeServiceInterface {
clientSessionId: string,
sourceIp: string,
persistentSessionId: string,
req: Request,
reauthenticate?: string
) => Promise<ApiResponseResult<StartAuthResponse>>;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {
createApiResponse,
getRequestConfig,
getInternalRequestConfigWithSecurityHeaders,
Http,
http,
} from "../../utils/http";
Expand All @@ -10,6 +10,7 @@ import {
CheckEmailFraudBlockInterface,
CheckEmailFraudBlockResponse,
} from "./types";
import { Request } from "express";

export function checkEmailFraudBlockService(
axios: Http = http
Expand All @@ -19,19 +20,24 @@ export function checkEmailFraudBlockService(
sessionId: string,
sourceIp: string,
clientSessionId: string,
persistentSessionId: string
persistentSessionId: string,
req: Request
): Promise<ApiResponseResult<CheckEmailFraudBlockResponse>> {
const response = await axios.client.post<CheckEmailFraudBlockResponse>(
API_ENDPOINTS.CHECK_EMAIL_FRAUD_BLOCK,
{
email: email.toLowerCase(),
},
getRequestConfig({
sessionId: sessionId,
sourceIp: sourceIp,
clientSessionId: clientSessionId,
persistentSessionId: persistentSessionId,
})
getInternalRequestConfigWithSecurityHeaders(
{
sessionId: sessionId,
sourceIp: sourceIp,
clientSessionId: clientSessionId,
persistentSessionId: persistentSessionId,
},
req,
API_ENDPOINTS.CHECK_EMAIL_FRAUD_BLOCK
)
);
return createApiResponse<CheckEmailFraudBlockResponse>(response);
};
Expand Down
Loading

0 comments on commit 20a7a10

Please sign in to comment.