Skip to content

Commit

Permalink
chore: rework to use factory methods
Browse files Browse the repository at this point in the history
  • Loading branch information
43081j committed Oct 22, 2023
1 parent 6e9fc85 commit 92d1891
Show file tree
Hide file tree
Showing 10 changed files with 165 additions and 195 deletions.
40 changes: 17 additions & 23 deletions docs/oidc-client-ts.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -629,13 +629,16 @@ export type SigninRedirectArgs = RedirectParams & ExtraSigninRequestArgs;

// @public (undocumented)
export class SigninRequest {
constructor({ authority, client_id, redirect_uri, response_type, scope, state_data, response_mode, request_type, client_secret, nonce, resource, skipUserInfo, extraQueryParams, extraTokenParams, disablePKCE, ...optionalParams }: SigninRequestArgs);
constructor(args: {
url: string;
state: SigninState;
});
// (undocumented)
setUrl(baseUrl: string): Promise<void>;
static create({ url, authority, client_id, redirect_uri, response_type, scope, state_data, response_mode, request_type, client_secret, nonce, resource, skipUserInfo, extraQueryParams, extraTokenParams, disablePKCE, ...optionalParams }: SigninRequestArgs): Promise<SigninRequest>;
// (undocumented)
readonly state: SigninState;
// (undocumented)
get url(): string;
readonly url: string;
}

// @public (undocumented)
Expand Down Expand Up @@ -687,6 +690,8 @@ export interface SigninRequestArgs {
state_data?: unknown;
// (undocumented)
ui_locales?: string;
// (undocumented)
url: string;
}

// @public (undocumented)
Expand Down Expand Up @@ -734,35 +739,24 @@ export type SigninSilentArgs = IFrameWindowParams & ExtraSigninRequestArgs;

// @public (undocumented)
export class SigninState extends State {
constructor(args: {
id?: string;
data?: unknown;
created?: number;
request_type?: string;
code_verifier?: string | boolean;
authority: string;
client_id: string;
redirect_uri: string;
scope: string;
client_secret?: string;
extraTokenParams?: Record<string, unknown>;
response_mode?: "query" | "fragment";
skipUserInfo?: boolean;
});
// Warning: (ae-forgotten-export) The symbol "SigninStateArgs" needs to be exported by the entry point index.d.ts
constructor(args: SigninStateArgs);
// (undocumented)
readonly authority: string;
// (undocumented)
readonly client_id: string;
// (undocumented)
readonly client_secret: string | undefined;
protected readonly _code_challenge: string | undefined;
readonly code_challenge: string | undefined;
readonly code_verifier: string | undefined;
// Warning: (ae-forgotten-export) The symbol "SigninStateCreateArgs" needs to be exported by the entry point index.d.ts
//
// (undocumented)
readonly extraTokenParams: Record<string, unknown> | undefined;
static create(args: SigninStateCreateArgs): Promise<SigninState>;
// (undocumented)
static fromStorageString(storageString: string): SigninState;
readonly extraTokenParams: Record<string, unknown> | undefined;
// (undocumented)
getChallenge(): Promise<string | undefined>;
static fromStorageString(storageString: string): Promise<SigninState>;
// (undocumented)
readonly redirect_uri: string;
// (undocumented)
Expand Down Expand Up @@ -841,7 +835,7 @@ export class State {
readonly created: number;
readonly data?: unknown;
// (undocumented)
static fromStorageString(storageString: string): State;
static fromStorageString(storageString: string): Promise<State>;
// (undocumented)
readonly id: string;
// (undocumented)
Expand Down
4 changes: 2 additions & 2 deletions src/OidcClient.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ describe("OidcClient", () => {

// assert
expect(request.state.data).toEqual("foo");
const url = await request.getUrl();
const url = request.url;
expect(url).toContain("http://sts/authorize");
expect(url).toContain("response_type=code");
expect(url).toContain("scope=baz");
Expand Down Expand Up @@ -132,7 +132,7 @@ describe("OidcClient", () => {

// assert
expect(request.state.data).toEqual("foo");
const url = await request.getUrl();
const url = request.url;
expect(url).toContain("http://sts/authorize");
expect(url).toContain("response_type=code");
expect(url).toContain("scope=baz");
Expand Down
9 changes: 4 additions & 5 deletions src/OidcClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,8 @@ export class OidcClient {
const url = await this.metadataService.getAuthorizationEndpoint();
logger.debug("Received authorization endpoint", url);

const signinRequest = new SigninRequest({
const signinRequest = await SigninRequest.create({
url,
authority: this.settings.authority,
client_id: this.settings.client_id,
redirect_uri,
Expand All @@ -129,8 +130,6 @@ export class OidcClient {
disablePKCE: this.settings.disablePKCE,
});

await signinRequest.setUrl(url);

// house cleaning
await this.clearStaleState();

Expand All @@ -155,7 +154,7 @@ export class OidcClient {
throw null; // https://github.com/microsoft/TypeScript/issues/46972
}

const state = SigninState.fromStorageString(storedStateString);
const state = await SigninState.fromStorageString(storedStateString);
return { state, response };
}

Expand Down Expand Up @@ -285,7 +284,7 @@ export class OidcClient {
throw null; // https://github.com/microsoft/TypeScript/issues/46972
}

const state = State.fromStorageString(storedStateString);
const state = await State.fromStorageString(storedStateString);
return { state, response };
}

Expand Down
76 changes: 38 additions & 38 deletions src/SigninRequest.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ describe("SigninRequest", () => {
let subject: SigninRequest;
let settings: SigninRequestArgs;

beforeEach(() => {
beforeEach(async () => {
settings = {
url: "http://sts/signin",
client_id: "client",
Expand All @@ -18,26 +18,26 @@ describe("SigninRequest", () => {
authority : "op",
state_data: { data: "test" },
};
subject = new SigninRequest(settings);
subject = await SigninRequest.create(settings);
});

describe("constructor", () => {
it.each(["url", "client_id", "redirect_uri", "response_type", "scope", "authority"])("should require a %s param", (param) => {
it.each(["url", "client_id", "redirect_uri", "response_type", "scope", "authority"])("should require a %s param", async (param) => {
// arrange
Object.assign(settings, { [param]: undefined });

// act
expect(() => new SigninRequest(settings))
await expect(() => SigninRequest.create(settings))
// assert
.toThrow(param);
.rejects.toThrow(param);
});
});

describe("url", () => {
let url: string;

beforeEach(async () => {
url = await subject.getUrl();
url = subject.url;
});

it("should include url", () => {
Expand Down Expand Up @@ -75,8 +75,8 @@ describe("SigninRequest", () => {
settings.prompt = "foo";

// act
subject = new SigninRequest(settings);
url = await subject.getUrl();
subject = await SigninRequest.create(settings);
url = subject.url;

// assert
expect(url).toContain("prompt=foo");
Expand All @@ -87,8 +87,8 @@ describe("SigninRequest", () => {
settings.display = "foo";

// act
subject = new SigninRequest(settings);
url = await subject.getUrl();
subject = await SigninRequest.create(settings);
url = subject.url;

// assert
expect(url).toContain("display=foo");
Expand All @@ -99,8 +99,8 @@ describe("SigninRequest", () => {
settings.max_age = 42;

// act
subject = new SigninRequest(settings);
url = await subject.getUrl();
subject = await SigninRequest.create(settings);
url = subject.url;

// assert
expect(url).toContain("max_age=42");
Expand All @@ -111,8 +111,8 @@ describe("SigninRequest", () => {
settings.ui_locales = "foo";

// act
subject = new SigninRequest(settings);
url = await subject.getUrl();
subject = await SigninRequest.create(settings);
url = subject.url;

// assert
expect(url).toContain("ui_locales=foo");
Expand All @@ -123,8 +123,8 @@ describe("SigninRequest", () => {
settings.id_token_hint = "foo";

// act
subject = new SigninRequest(settings);
url = await subject.getUrl();
subject = await SigninRequest.create(settings);
url = subject.url;

// assert
expect(url).toContain("id_token_hint=foo");
Expand All @@ -135,8 +135,8 @@ describe("SigninRequest", () => {
settings.login_hint = "foo";

// act
subject = new SigninRequest(settings);
url = await subject.getUrl();
subject = await SigninRequest.create(settings);
url = subject.url;

// assert
expect(url).toContain("login_hint=foo");
Expand All @@ -147,8 +147,8 @@ describe("SigninRequest", () => {
settings.acr_values = "foo";

// act
subject = new SigninRequest(settings);
url = await subject.getUrl();
subject = await SigninRequest.create(settings);
url = subject.url;

// assert
expect(url).toContain("acr_values=foo");
Expand All @@ -159,8 +159,8 @@ describe("SigninRequest", () => {
settings.resource = "foo";

// act
subject = new SigninRequest(settings);
url = await subject.getUrl();
subject = await SigninRequest.create(settings);
url = subject.url;

// assert
expect(url).toContain("resource=foo");
Expand All @@ -171,8 +171,8 @@ describe("SigninRequest", () => {
settings.resource = ["foo", "bar"];

// act
subject = new SigninRequest(settings);
url = await subject.getUrl();
subject = await SigninRequest.create(settings);
url = subject.url;

// assert
expect(url).toContain("resource=foo&resource=bar");
Expand All @@ -183,8 +183,8 @@ describe("SigninRequest", () => {
settings.response_mode = "fragment";

// act
subject = new SigninRequest(settings);
url = await subject.getUrl();
subject = await SigninRequest.create(settings);
url = subject.url;

// assert
expect(url).toContain("response_mode=fragment");
Expand All @@ -195,8 +195,8 @@ describe("SigninRequest", () => {
settings.request = "foo";

// act
subject = new SigninRequest(settings);
url = await subject.getUrl();
subject = await SigninRequest.create(settings);
url = subject.url;

// assert
expect(url).toContain("request=foo");
Expand All @@ -207,8 +207,8 @@ describe("SigninRequest", () => {
settings.request_uri = "foo";

// act
subject = new SigninRequest(settings);
url = await subject.getUrl();
subject = await SigninRequest.create(settings);
url = subject.url;

// assert
expect(url).toContain("request_uri=foo");
Expand All @@ -222,21 +222,21 @@ describe("SigninRequest", () => {
};

// act
subject = new SigninRequest(settings);
url = await subject.getUrl();
subject = await SigninRequest.create(settings);
url = subject.url;

// assert
expect(url).toContain("hd=domain.com&foo=bar");
});

it("should store extra token params in state", () => {
it("should store extra token params in state", async () => {
// arrange
settings.extraTokenParams = {
"resourceServer": "abc",
};

// act
subject = new SigninRequest(settings);
subject = await SigninRequest.create(settings);

// assert
expect(subject.state.extraTokenParams).toEqual({
Expand All @@ -249,8 +249,8 @@ describe("SigninRequest", () => {
settings.response_type = "code";

// act
subject = new SigninRequest(settings);
url = await subject.getUrl();
subject = await SigninRequest.create(settings);
url = subject.url;

// assert
expect(url).toContain("code_challenge=");
Expand All @@ -262,8 +262,8 @@ describe("SigninRequest", () => {
settings.nonce = "random_nonce";

// act
subject = new SigninRequest(settings);
url = await subject.getUrl();
subject = await SigninRequest.create(settings);
url = subject.url;

// assert
expect(url).toContain("nonce=");
Expand Down
Loading

0 comments on commit 92d1891

Please sign in to comment.