From a57c430b09cc57b8197c0e5b64d08a16d0d37b0e Mon Sep 17 00:00:00 2001 From: Hugh Nimmo-Smith Date: Sun, 11 Sep 2022 22:18:33 +0100 Subject: [PATCH] Implementation of MSC3824 to add action= param on SSO login (#2398) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Šimon Brandner --- spec/unit/login.spec.ts | 35 +++++++++++++++++++++++++++++++++++ src/@types/auth.ts | 8 ++++++++ src/client.ts | 20 +++++++++++++++++--- 3 files changed, 60 insertions(+), 3 deletions(-) diff --git a/spec/unit/login.spec.ts b/spec/unit/login.spec.ts index f7cf6d307..6b3ae47fe 100644 --- a/spec/unit/login.spec.ts +++ b/spec/unit/login.spec.ts @@ -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'); + }); + }); +}); diff --git a/src/@types/auth.ts b/src/@types/auth.ts index db07ce661..86d478944 100644 --- a/src/@types/auth.ts +++ b/src/@types/auth.ts @@ -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", +} diff --git a/src/client.ts b/src/client.ts index 66e3ec2da..27f698b88 100644 --- a/src/client.ts +++ b/src/client.ts @@ -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