Skip to content

Commit

Permalink
Fix typing and tests
Browse files Browse the repository at this point in the history
  • Loading branch information
sandhose committed Aug 1, 2022
1 parent a4994ef commit e23a06b
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 15 deletions.
3 changes: 3 additions & 0 deletions src/domain/navigation/URLRouter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ export interface IURLRouter<T> {
urlForPath(path: Path<T>): string;
openRoomActionUrl(roomId: string): string;
createSSOCallbackURL(): string;
createOIDCRedirectURL(): string;
absoluteAppUrl(): string;
absoluteUrlForAsset(asset: string): string;
normalizeUrl(): void;
}

Expand Down
38 changes: 26 additions & 12 deletions src/domain/navigation/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,16 @@ export type SegmentType = {
"details": true;
"members": true;
"member": string;
"oidc-callback": (string | null)[];
"oidc-error": (string | null)[];
"oidc": {
state: string,
} &
({
code: string,
} | {
error: string,
errorDescription: string | null,
errorUri: string | null ,
});
};

export function createNavigation(): Navigation<SegmentType> {
Expand Down Expand Up @@ -131,18 +139,21 @@ export function parseUrlPath(urlPath: string, currentNavPath: Path<SegmentType>,
// Special case for OIDC callback
if (urlPath.includes("state")) {
const params = new URLSearchParams(urlPath);
if (params.has("state")) {
const state = params.get("state");
const code = params.get("code");
const error = params.get("error");
if (state) {
// This is a proper OIDC callback
if (params.has("code")) {
if (code) {
segments.push(new Segment("oidc", {
state: params.get("state"),
code: params.get("code"),
state,
code,
}));
return segments;
} else if (params.has("error")) {
} else if (error) {
segments.push(new Segment("oidc", {
state: params.get("state"),
error: params.get("error"),
state,
error,
errorDescription: params.get("error_description"),
errorUri: params.get("error_uri"),
}));
Expand Down Expand Up @@ -514,19 +525,22 @@ export function tests() {
assert.equal(newPath?.segments[1].value, "b");
},
"Parse OIDC callback": assert => {
const segments = parseUrlPath("state=tc9CnLU7&code=cnmUnwIYtY7V8RrWUyhJa4yvX72jJ5Yx");
const path = createEmptyPath();
const segments = parseUrlPath("state=tc9CnLU7&code=cnmUnwIYtY7V8RrWUyhJa4yvX72jJ5Yx", path);
assert.equal(segments.length, 1);
assert.equal(segments[0].type, "oidc");
assert.deepEqual(segments[0].value, {state: "tc9CnLU7", code: "cnmUnwIYtY7V8RrWUyhJa4yvX72jJ5Yx"});
},
"Parse OIDC error": assert => {
const segments = parseUrlPath("state=tc9CnLU7&error=invalid_request");
const path = createEmptyPath();
const segments = parseUrlPath("state=tc9CnLU7&error=invalid_request", path);
assert.equal(segments.length, 1);
assert.equal(segments[0].type, "oidc");
assert.deepEqual(segments[0].value, {state: "tc9CnLU7", error: "invalid_request", errorUri: null, errorDescription: null});
},
"Parse OIDC error with description": assert => {
const segments = parseUrlPath("state=tc9CnLU7&error=invalid_request&error_description=Unsupported%20response_type%20value");
const path = createEmptyPath();
const segments = parseUrlPath("state=tc9CnLU7&error=invalid_request&error_description=Unsupported%20response_type%20value", path);
assert.equal(segments.length, 1);
assert.equal(segments[0].type, "oidc");
assert.deepEqual(segments[0].value, {state: "tc9CnLU7", error: "invalid_request", errorDescription: "Unsupported response_type value", errorUri: null});
Expand Down
7 changes: 4 additions & 3 deletions src/matrix/net/OidcApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ limitations under the License.
*/

import type {RequestFunction} from "../../platform/types/types";
import type {URLRouter} from "../../domain/navigation/URLRouter.js";
import type {IURLRouter} from "../../domain/navigation/URLRouter.js";
import type {SegmentType} from "../../domain/navigation";

const WELL_KNOWN = ".well-known/openid-configuration";

Expand Down Expand Up @@ -69,12 +70,12 @@ const clientIds: Record<IssuerUri, ClientConfig> = {
},
};

export class OidcApi {
export class OidcApi<N extends object = SegmentType> {
_issuer: string;
_requestFn: RequestFunction;
_encoding: any;
_crypto: any;
_urlCreator: URLRouter;
_urlCreator: IURLRouter<N>;
_metadataPromise: Promise<any>;
_registrationPromise: Promise<any>;

Expand Down

0 comments on commit e23a06b

Please sign in to comment.