Skip to content

Commit

Permalink
Add removeState as an option to processSigninResponse (#1691)
Browse files Browse the repository at this point in the history
* Add flag whether to delete state for processSigninResponse
  • Loading branch information
ZephireNZ authored Oct 14, 2024
1 parent ff211c5 commit 76b88db
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 3 deletions.
2 changes: 1 addition & 1 deletion docs/oidc-client-ts.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -346,7 +346,7 @@ export class OidcClient {
// (undocumented)
processResourceOwnerPasswordCredentials({ username, password, skipUserInfo, extraTokenParams, }: ProcessResourceOwnerPasswordCredentialsArgs): Promise<SigninResponse>;
// (undocumented)
processSigninResponse(url: string, extraHeaders?: Record<string, ExtraHeader>): Promise<SigninResponse>;
processSigninResponse(url: string, extraHeaders?: Record<string, ExtraHeader>, removeState?: boolean): Promise<SigninResponse>;
// (undocumented)
processSignoutResponse(url: string): Promise<SignoutResponse>;
// (undocumented)
Expand Down
24 changes: 24 additions & 0 deletions src/OidcClient.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -465,6 +465,30 @@ describe("OidcClient", () => {
expect(getDpopProofMock).toHaveBeenCalledTimes(2);
expect(getDpopProofMock).toHaveBeenNthCalledWith(2, { "_dbName": "oidc", "_storeName": "dpop" }, "some-nonce");
});

it("should not delete state when removeState = false", async () => {
// arrange
const item = await SigninState.create({
id: "1",
authority: "authority",
client_id: "client",
redirect_uri: "http://app/cb",
scope: "scope",
request_type: "type",
});
const getStateMock = jest.spyOn(subject.settings.stateStore, "get")
.mockImplementation(async () => item.toStorageString());
const removeStateMock = jest.spyOn(subject.settings.stateStore, "remove")
.mockImplementation(async () => item.toStorageString());
jest.spyOn(subject["_validator"], "validateSigninResponse").mockResolvedValue();

// act
await subject.processSigninResponse("http://app/cb?state=1", undefined, false);

// assert
expect(getStateMock).toHaveBeenCalled();
expect(removeStateMock).not.toHaveBeenCalled();
});
});

describe("processResourceOwnerPasswordCredentials", () => {
Expand Down
4 changes: 2 additions & 2 deletions src/OidcClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -172,10 +172,10 @@ export class OidcClient {
return { state, response };
}

public async processSigninResponse(url: string, extraHeaders?: Record<string, ExtraHeader>): Promise<SigninResponse> {
public async processSigninResponse(url: string, extraHeaders?: Record<string, ExtraHeader>, removeState = true): Promise<SigninResponse> {
const logger = this._logger.create("processSigninResponse");

const { state, response } = await this.readSigninResponseState(url, true);
const { state, response } = await this.readSigninResponseState(url, removeState);
logger.debug("received state from storage; validating response");

if (this.settings.dpop && this.settings.dpop.store) {
Expand Down

0 comments on commit 76b88db

Please sign in to comment.