Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix samples #1038

Merged
merged 6 commits into from
Jun 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions docs/oidc-client-ts.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,7 @@ export class OidcClient {
// (undocumented)
createSigninRequest({ state, request, request_uri, request_type, id_token_hint, login_hint, skipUserInfo, nonce, response_type, scope, redirect_uri, prompt, display, max_age, ui_locales, acr_values, resource, response_mode, extraQueryParams, extraTokenParams, }: CreateSigninRequestArgs): Promise<SigninRequest>;
// (undocumented)
createSignoutRequest({ state, id_token_hint, request_type, post_logout_redirect_uri, extraQueryParams, }?: CreateSignoutRequestArgs): Promise<SignoutRequest>;
createSignoutRequest({ state, id_token_hint, client_id, request_type, post_logout_redirect_uri, extraQueryParams, }?: CreateSignoutRequestArgs): Promise<SignoutRequest>;
// (undocumented)
protected readonly _logger: Logger;
// (undocumented)
Expand Down Expand Up @@ -720,7 +720,7 @@ export type SignoutRedirectArgs = RedirectParams & ExtraSignoutRequestArgs;

// @public (undocumented)
export class SignoutRequest {
constructor({ url, state_data, id_token_hint, post_logout_redirect_uri, extraQueryParams, request_type, }: SignoutRequestArgs);
constructor({ url, state_data, id_token_hint, post_logout_redirect_uri, extraQueryParams, request_type, client_id, }: SignoutRequestArgs);
// (undocumented)
readonly state?: State;
// (undocumented)
Expand All @@ -729,6 +729,8 @@ export class SignoutRequest {

// @public (undocumented)
export interface SignoutRequestArgs {
// (undocumented)
client_id?: string;
// (undocumented)
extraQueryParams?: Record<string, string | number | boolean>;
// (undocumented)
Expand Down
6 changes: 3 additions & 3 deletions samples/Parcel/src/oidc-client/sample.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ function signin() {

var signinResponse;
function processSigninResponse() {
client.processSigninResponse().then(function(response) {
client.processSigninResponse(window.location.href).then(function(response) {
signinResponse = response;
log("signin response", signinResponse);
}).catch(function(err) {
Expand All @@ -59,7 +59,7 @@ function processSigninResponse() {
}

function signout() {
client.createSignoutRequest({ state: { foo: 5 } }).then(function(req) {
client.createSignoutRequest({ state: { foo: 5 }, client_id: settings.client_id }).then(function(req) {
log("signout request", req, "<a href='" + req.url + "'>go signout</a>");
if (followLinks()) {
window.location = req.url;
Expand All @@ -68,7 +68,7 @@ function signout() {
}

function processSignoutResponse() {
client.processSignoutResponse().then(function(response) {
client.processSignoutResponse(window.location.href).then(function(response) {
signinResponse = null;
log("signout response", response);
}).catch(function(err) {
Expand Down
39 changes: 39 additions & 0 deletions src/OidcClient.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -563,6 +563,45 @@ describe("OidcClient", () => {
expect(url).toContain("id_token_hint=baz");
});

it("should pass params to SignoutRequest w/o id_token_hint and client_id", async () => {
// arrange
jest.spyOn(subject.metadataService, "getEndSessionEndpoint").mockImplementation(() => Promise.resolve("http://sts/signout"));

// act
const request = await subject.createSignoutRequest({
state: "foo",
post_logout_redirect_uri: "bar",
});

// assert
expect(request.state).toBeDefined();
expect(request.state?.data).toEqual("foo");
const url = request.url;
expect(url).toContain("http://sts/signout");
expect(url).toContain("post_logout_redirect_uri=bar");
expect(url).toContain("client_id=client");
});

it("should pass params to SignoutRequest with client_id", async () => {
// arrange
jest.spyOn(subject.metadataService, "getEndSessionEndpoint").mockImplementation(() => Promise.resolve("http://sts/signout"));

// act
const request = await subject.createSignoutRequest({
state: "foo",
post_logout_redirect_uri: "bar",
client_id: "baz",
});

// assert
expect(request.state).toBeDefined();
expect(request.state?.data).toEqual("foo");
const url = request.url;
expect(url).toContain("http://sts/signout");
expect(url).toContain("post_logout_redirect_uri=bar");
expect(url).toContain("client_id=baz");
});

it("should fail if metadata fails", async () => {
// arrange
jest.spyOn(subject.metadataService, "getEndSessionEndpoint").mockRejectedValue(new Error("test"));
Expand Down
7 changes: 7 additions & 0 deletions src/OidcClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,7 @@ export class OidcClient {
public async createSignoutRequest({
state,
id_token_hint,
client_id,
request_type,
post_logout_redirect_uri = this.settings.post_logout_redirect_uri,
extraQueryParams = this.settings.extraQueryParams,
Expand All @@ -232,9 +233,15 @@ export class OidcClient {

logger.debug("Received end session endpoint", url);

// specify the client identifier when post_logout_redirect_uri is used but id_token_hint is not
if (!client_id && post_logout_redirect_uri && !id_token_hint) {
client_id = this.settings.client_id;
}

const request = new SignoutRequest({
url,
id_token_hint,
client_id,
post_logout_redirect_uri,
state_data: state,
extraQueryParams,
Expand Down
6 changes: 5 additions & 1 deletion src/SignoutRequest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ export interface SignoutRequestArgs {

// optional
id_token_hint?: string;
client_id?: string;
post_logout_redirect_uri?: string;
extraQueryParams?: Record<string, string | number | boolean>;

Expand All @@ -34,7 +35,7 @@ export class SignoutRequest {

public constructor({
url,
state_data, id_token_hint, post_logout_redirect_uri, extraQueryParams, request_type,
state_data, id_token_hint, post_logout_redirect_uri, extraQueryParams, request_type, client_id,
}: SignoutRequestArgs) {
if (!url) {
this._logger.error("ctor: No url passed");
Expand All @@ -45,6 +46,9 @@ export class SignoutRequest {
if (id_token_hint) {
parsedUrl.searchParams.append("id_token_hint", id_token_hint);
}
if (client_id) {
parsedUrl.searchParams.append("client_id", client_id);
}

if (post_logout_redirect_uri) {
parsedUrl.searchParams.append("post_logout_redirect_uri", post_logout_redirect_uri);
Expand Down