1
0
mirror of https://github.com/matrix-org/matrix-js-sdk.git synced 2025-08-09 10:22:46 +03:00

Implementation of MSC3824 to add action= param on SSO login (#2398)

Co-authored-by: Šimon Brandner <simon.bra.ag@gmail.com>
This commit is contained in:
Hugh Nimmo-Smith
2022-09-11 22:18:33 +01:00
committed by GitHub
parent 583e48086b
commit a57c430b09
3 changed files with 60 additions and 3 deletions

View File

@@ -1,3 +1,4 @@
import { SSOAction } from '../../src/@types/auth';
import { TestClient } from '../TestClient';
describe('Login request', function() {
@@ -22,3 +23,37 @@ describe('Login request', function() {
expect(client.client.getUserId()).toBe(response.user_id);
});
});
describe('SSO login URL', function() {
let client: TestClient;
beforeEach(function() {
client = new TestClient();
});
afterEach(function() {
client.stop();
});
describe('SSOAction', function() {
const redirectUri = "https://test.com/foo";
it('No action', function() {
const urlString = client.client.getSsoLoginUrl(redirectUri, undefined, undefined, undefined);
const url = new URL(urlString);
expect(url.searchParams.has('org.matrix.msc3824.action')).toBe(false);
});
it('register', function() {
const urlString = client.client.getSsoLoginUrl(redirectUri, undefined, undefined, SSOAction.REGISTER);
const url = new URL(urlString);
expect(url.searchParams.get('org.matrix.msc3824.action')).toEqual('register');
});
it('login', function() {
const urlString = client.client.getSsoLoginUrl(redirectUri, undefined, undefined, SSOAction.LOGIN);
const url = new URL(urlString);
expect(url.searchParams.get('org.matrix.msc3824.action')).toEqual('login');
});
});
});

View File

@@ -82,3 +82,11 @@ export interface ILoginParams {
initial_device_display_name?: string;
}
/* eslint-enable camelcase */
export enum SSOAction {
/** The user intends to login to an existing account */
LOGIN = "login",
/** The user intends to register for a new account */
REGISTER = "register",
}

View File

@@ -188,13 +188,14 @@ import { IPusher, IPusherRequest, IPushRules, PushRuleAction, PushRuleKind, Rule
import { IThreepid } from "./@types/threepids";
import { CryptoStore } from "./crypto/store/base";
import { MediaHandler } from "./webrtc/mediaHandler";
import { ILoginFlowsResponse, IRefreshTokenResponse } from "./@types/auth";
import { ILoginFlowsResponse, IRefreshTokenResponse, SSOAction } from "./@types/auth";
import { TypedEventEmitter } from "./models/typed-event-emitter";
import { ReceiptType } from "./@types/read_receipts";
import { MSC3575SlidingSyncRequest, MSC3575SlidingSyncResponse, SlidingSync } from "./sliding-sync";
import { SlidingSyncSdk } from "./sliding-sync-sdk";
import { Thread, THREAD_RELATION_TYPE } from "./models/thread";
import { MBeaconInfoEventContent, M_BEACON_INFO } from "./@types/beacon";
import { UnstableValue } from "./NamespacedValue";
import { ToDeviceMessageQueue } from "./ToDeviceMessageQueue";
import { ToDeviceBatch } from "./models/ToDeviceMessage";
import { IgnoredInvites } from "./models/invites-ignorer";
@@ -881,6 +882,8 @@ export type ClientEventHandlerMap = {
& HttpApiEventHandlerMap
& BeaconEventHandlerMap;
const SSO_ACTION_PARAM = new UnstableValue("action", "org.matrix.msc3824.action");
/**
* Represents a Matrix Client. Only directly construct this if you want to use
* custom modules. Normally, {@link createClient} should be used
@@ -7156,15 +7159,26 @@ export class MatrixClient extends TypedEventEmitter<EmittedEvents, ClientEventHa
* @param {string} loginType The type of SSO login we are doing (sso or cas).
* Defaults to 'sso'.
* @param {string} idpId The ID of the Identity Provider being targeted, optional.
* @param {SSOAction} action the SSO flow to indicate to the IdP, optional.
* @return {string} The HS URL to hit to begin the SSO login process.
*/
public getSsoLoginUrl(redirectUrl: string, loginType = "sso", idpId?: string): string {
public getSsoLoginUrl(
redirectUrl: string,
loginType = "sso",
idpId?: string,
action?: SSOAction,
): string {
let url = "/login/" + loginType + "/redirect";
if (idpId) {
url += "/" + idpId;
}
return this.http.getUrl(url, { redirectUrl }, PREFIX_R0);
const params = {
redirectUrl,
[SSO_ACTION_PARAM.unstable!]: action,
};
return this.http.getUrl(url, params, PREFIX_R0);
}
/**