diff --git a/CHANGELOG.md b/CHANGELOG.md index 4851cff..8920884 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,14 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). +## [Unreleased] + +### Fixed + +- **Social login on Authorizer's login page now resumes the authorization request** ([authorizerdev/authorizer#855](https://github.com/authorizerdev/authorizer/issues/855)). When `/authorize` opens the login page (`/app` or `/app/signup`), `AuthorizerSocialLogin` sends that page's own URL as `redirect_uri`, with the `/authorize` parameters in the query string, instead of the client's callback. The login then comes back to `/app`, which replays the request to `/authorize`. A client whose callback is outside `--allowed-origins` can now use social login, and self-registered clients get the consent screen. Fragment parameters (`response_mode=fragment`) move into the query string. + - **Requires Authorizer 2.4.6 or later**, which accepts its own `/app` as a social-login `redirect_uri`. The hosted login page ships in the same image as the server, so it always meets this. + - **No change anywhere else:** it applies only when the page's origin equals `authorizerURL` and its URL carries both `client_id` and `response_type`. On your own site, `AuthorizerSocialLogin` sends the same `redirect_uri` as before. + ## [2.0.0-rc.1] - 2025-12-30 ### 🚀 Major Changes diff --git a/eslint.config.js b/eslint.config.js index b575bc9..a810c90 100644 --- a/eslint.config.js +++ b/eslint.config.js @@ -1,34 +1,57 @@ -module.exports = { - root: true, - env: { - browser: true, - es2021: true, - node: true, +// ESLint flat config (ESLint 9). Same intent as the previous .eslintrc-style +// config: eslint:recommended + @typescript-eslint recommended + react +// recommended + react-hooks recommended. +const js = require('@eslint/js'); +const globals = require('globals'); +const tsPlugin = require('@typescript-eslint/eslint-plugin'); +const reactPlugin = require('eslint-plugin-react'); +const reactHooksPlugin = require('eslint-plugin-react-hooks'); + +module.exports = [ + { + ignores: [ + 'dist/**', + 'storybook-static/**', + 'node_modules/**', + 'example/**', + '*.config.js', + '*.config.ts', + ], }, - extends: [ - 'eslint:recommended', - 'plugin:@typescript-eslint/recommended', - 'plugin:react/recommended', - 'plugin:react-hooks/recommended', - ], - parser: '@typescript-eslint/parser', - parserOptions: { - ecmaVersion: 'latest', - sourceType: 'module', - ecmaFeatures: { - jsx: true, + js.configs.recommended, + ...tsPlugin.configs['flat/recommended'], + reactPlugin.configs.flat.recommended, + reactHooksPlugin.configs['recommended-latest'], + { + files: ['**/*.{ts,tsx}'], + languageOptions: { + ecmaVersion: 'latest', + sourceType: 'module', + parserOptions: { + ecmaFeatures: { + jsx: true, + }, + }, + globals: { + ...globals.browser, + ...globals.es2021, + ...globals.node, + }, }, - }, - plugins: ['react', 'react-hooks', '@typescript-eslint'], - settings: { - react: { - version: 'detect', + settings: { + react: { + version: 'detect', + }, + }, + rules: { + 'react/react-in-jsx-scope': 'off', + 'react/prop-types': 'off', + // warn, not error: ~40 pre-existing `any`s; typing them properly is a separate change. + '@typescript-eslint/no-explicit-any': 'warn', + '@typescript-eslint/no-unused-vars': [ + 'warn', + { argsIgnorePattern: '^_' }, + ], }, }, - rules: { - 'react/react-in-jsx-scope': 'off', - 'react/prop-types': 'off', - '@typescript-eslint/no-unused-vars': ['warn', { argsIgnorePattern: '^_' }], - }, - ignorePatterns: ['dist/**', 'node_modules/**', '*.config.js', '*.config.ts'], -}; +]; diff --git a/package-lock.json b/package-lock.json index db37e32..93074be 100644 --- a/package-lock.json +++ b/package-lock.json @@ -13,6 +13,7 @@ "validator": "^13.11.0" }, "devDependencies": { + "@eslint/js": "^9.39.5", "@size-limit/preset-small-lib": "^11.1.0", "@storybook/addon-essentials": "8.4.0", "@storybook/addon-interactions": "8.4.0", @@ -34,6 +35,7 @@ "eslint": "^9.17.0", "eslint-plugin-react": "^7.37.0", "eslint-plugin-react-hooks": "^5.1.0", + "globals": "^17.14.0", "husky": "^9.1.7", "jsdom": "^26.1.0", "postcss": "^8.4.49", @@ -342,6 +344,16 @@ "node": ">=6.9.0" } }, + "node_modules/@babel/traverse/node_modules/globals": { + "version": "11.12.0", + "resolved": "https://registry.npmjs.org/globals/-/globals-11.12.0.tgz", + "integrity": "sha512-WOBp/EEGUiIsJSp7wcv/y6MO+lV9UoncWqxuFfm8eBwzWNgyfBd6Gz+IeKQ9jCmyhoH99g15M3T+QaVHFjizVA==", + "dev": true, + "license": "MIT", + "engines": { + "node": ">=4" + } + }, "node_modules/@babel/types": { "version": "7.29.0", "resolved": "https://registry.npmjs.org/@babel/types/-/types-7.29.0.tgz", @@ -1088,9 +1100,9 @@ "license": "MIT" }, "node_modules/@eslint/js": { - "version": "9.39.2", - "resolved": "https://registry.npmjs.org/@eslint/js/-/js-9.39.2.tgz", - "integrity": "sha512-q1mjIoW1VX4IvSocvM/vbTiveKC4k9eLrajNEuSsmjymSDEbpGddtpfOoN7YGAqBK3NG+uqo8ia4PDTt8buCYA==", + "version": "9.39.5", + "resolved": "https://registry.npmjs.org/@eslint/js/-/js-9.39.5.tgz", + "integrity": "sha512-QywQuszQh77pIXCsq998c8hbhSTI/azTty1Z6N53dmAudKHhy573j3yvRLsX2BSp8YpLtoCEG8E9DJe+8zUh4A==", "dev": true, "license": "MIT", "engines": { @@ -5591,6 +5603,19 @@ "node": ">=4.0" } }, + "node_modules/eslint/node_modules/@eslint/js": { + "version": "9.39.2", + "resolved": "https://registry.npmjs.org/@eslint/js/-/js-9.39.2.tgz", + "integrity": "sha512-q1mjIoW1VX4IvSocvM/vbTiveKC4k9eLrajNEuSsmjymSDEbpGddtpfOoN7YGAqBK3NG+uqo8ia4PDTt8buCYA==", + "dev": true, + "license": "MIT", + "engines": { + "node": "^18.18.0 || ^20.9.0 || >=21.1.0" + }, + "funding": { + "url": "https://eslint.org/donate" + } + }, "node_modules/eslint/node_modules/ajv": { "version": "6.14.0", "resolved": "https://registry.npmjs.org/ajv/-/ajv-6.14.0.tgz", @@ -6577,12 +6602,16 @@ "license": "BSD-2-Clause" }, "node_modules/globals": { - "version": "11.12.0", - "resolved": "https://registry.npmjs.org/globals/-/globals-11.12.0.tgz", - "integrity": "sha512-WOBp/EEGUiIsJSp7wcv/y6MO+lV9UoncWqxuFfm8eBwzWNgyfBd6Gz+IeKQ9jCmyhoH99g15M3T+QaVHFjizVA==", + "version": "17.14.0", + "resolved": "https://registry.npmjs.org/globals/-/globals-17.14.0.tgz", + "integrity": "sha512-UtdkTVyAJePQv0J2M9Xe4L0ruNSDV314XiIJsADI2Zf40bcdAonE/D/OhAU8AT99oij9mhLByb+NvA3QWZYgCg==", "dev": true, + "license": "MIT", "engines": { - "node": ">=4" + "node": ">=18" + }, + "funding": { + "url": "https://github.com/sponsors/sindresorhus" } }, "node_modules/globalthis": { @@ -10588,24 +10617,6 @@ "node": ">= 12" } }, - "node_modules/tsup/node_modules/yaml": { - "version": "2.9.0", - "resolved": "https://registry.npmjs.org/yaml/-/yaml-2.9.0.tgz", - "integrity": "sha512-2AvhNX3mb8zd6Zy7INTtSpl1F15HW6Wnqj0srWlkKLcpYl/gMIMJiyuGq2KeI2YFxUPjdlB+3Lc10seMLtL4cA==", - "dev": true, - "license": "ISC", - "optional": true, - "peer": true, - "bin": { - "yaml": "bin.mjs" - }, - "engines": { - "node": ">= 14.6" - }, - "funding": { - "url": "https://github.com/sponsors/eemeli" - } - }, "node_modules/tween-functions": { "version": "1.2.0", "resolved": "https://registry.npmjs.org/tween-functions/-/tween-functions-1.2.0.tgz", @@ -11470,24 +11481,6 @@ } } }, - "node_modules/vite-node/node_modules/yaml": { - "version": "2.9.0", - "resolved": "https://registry.npmjs.org/yaml/-/yaml-2.9.0.tgz", - "integrity": "sha512-2AvhNX3mb8zd6Zy7INTtSpl1F15HW6Wnqj0srWlkKLcpYl/gMIMJiyuGq2KeI2YFxUPjdlB+3Lc10seMLtL4cA==", - "dev": true, - "license": "ISC", - "optional": true, - "peer": true, - "bin": { - "yaml": "bin.mjs" - }, - "engines": { - "node": ">= 14.6" - }, - "funding": { - "url": "https://github.com/sponsors/eemeli" - } - }, "node_modules/vitest": { "version": "3.2.7", "resolved": "https://registry.npmjs.org/vitest/-/vitest-3.2.7.tgz", @@ -12256,24 +12249,6 @@ } } }, - "node_modules/vitest/node_modules/yaml": { - "version": "2.9.0", - "resolved": "https://registry.npmjs.org/yaml/-/yaml-2.9.0.tgz", - "integrity": "sha512-2AvhNX3mb8zd6Zy7INTtSpl1F15HW6Wnqj0srWlkKLcpYl/gMIMJiyuGq2KeI2YFxUPjdlB+3Lc10seMLtL4cA==", - "dev": true, - "license": "ISC", - "optional": true, - "peer": true, - "bin": { - "yaml": "bin.mjs" - }, - "engines": { - "node": ">= 14.6" - }, - "funding": { - "url": "https://github.com/sponsors/eemeli" - } - }, "node_modules/w3c-xmlserializer": { "version": "5.0.0", "resolved": "https://registry.npmjs.org/w3c-xmlserializer/-/w3c-xmlserializer-5.0.0.tgz", diff --git a/package.json b/package.json index e9326b5..7c0d0c3 100644 --- a/package.json +++ b/package.json @@ -24,7 +24,7 @@ "start": "tsup --watch", "build": "tsup", "test": "vitest run", - "lint": "eslint src --ext .ts,.tsx --fix", + "lint": "eslint src --fix", "type-check": "tsc --noEmit", "prepare": "tsup", "size": "size-limit", @@ -59,6 +59,7 @@ } ], "devDependencies": { + "@eslint/js": "^9.39.5", "@size-limit/preset-small-lib": "^11.1.0", "@storybook/addon-essentials": "8.4.0", "@storybook/addon-interactions": "8.4.0", @@ -80,6 +81,7 @@ "eslint": "^9.17.0", "eslint-plugin-react": "^7.37.0", "eslint-plugin-react-hooks": "^5.1.0", + "globals": "^17.14.0", "husky": "^9.1.7", "jsdom": "^26.1.0", "postcss": "^8.4.49", diff --git a/src/__tests__/AuthorizerSocialLogin.test.tsx b/src/__tests__/AuthorizerSocialLogin.test.tsx new file mode 100644 index 0000000..7772159 --- /dev/null +++ b/src/__tests__/AuthorizerSocialLogin.test.tsx @@ -0,0 +1,117 @@ +import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'; +import { fireEvent, render, screen } from '@testing-library/react'; + +import { AuthorizerSocialLogin } from '../components/AuthorizerSocialLogin'; + +const AUTHORIZER = 'https://auth.example.com'; +const CLIENT_CALLBACK = 'https://app.example.org/cb'; + +let authorizerURL = AUTHORIZER; +vi.mock('../contexts/AuthorizerContext', () => ({ + useAuthorizer: () => ({ + config: { + authorizerURL, + redirectURL: `${AUTHORIZER}/app`, + is_github_login_enabled: true, + is_mfa_enforced: true, // keeps the passkey separator logic out of the way + }, + }), +})); + +// jsdom cannot navigate, so replace window.location with a plain object that +// records the href each button assigns. +const realLocation = window.location; +const setLocation = (url: string) => { + const u = new URL(url); + Object.defineProperty(window, 'location', { + configurable: true, + writable: true, + value: { + origin: u.origin, + pathname: u.pathname, + search: u.search, + hash: u.hash, + href: url, + }, + }); +}; + +// clickGithub returns the /oauth_login query the GitHub button navigates to. +const clickGithub = () => { + fireEvent.click(screen.getByText('Continue with Github')); + const target = new URL(window.location.href); + expect(target.origin + target.pathname).toBe( + `${AUTHORIZER}/oauth_login/github` + ); + return target.searchParams; +}; + +// urlProps as web/app's Root builds them from the /app URL. +const urlPropsFor = (redirect: string) => ({ + state: 's1', + scope: ['openid', 'profile', 'email'], + redirect_uri: redirect, + redirectURL: redirect, +}); + +const AUTHORIZE_QUERY = + 'state=s1&scope=openid+profile+email&redirect_uri=https%3A%2F%2Fapp.example.org%2Fcb' + + '&response_mode=query&response_type=code&client_id=c1&nonce=n1'; + +describe('AuthorizerSocialLogin redirect_uri', () => { + beforeEach(() => { + authorizerURL = AUTHORIZER; + }); + afterEach(() => { + Object.defineProperty(window, 'location', { + configurable: true, + value: realLocation, + }); + }); + + it('inside an /authorize request on /app, returns to /app, not to the client', () => { + // The URL a browser is on: Authorizer 301s /app to /app/. + const appURL = `${AUTHORIZER}/app/?${AUTHORIZE_QUERY}`; + setLocation(appURL); + render(); + + const q = clickGithub(); + expect(q.get('redirect_uri')).toBe(appURL); + // The server reads redirectURL first, so a stale client callback there + // would win; it must not be sent. + expect(q.has('redirectURL')).toBe(false); + expect(q.get('state')).toBe('s1'); + }); + + it('with response_mode=fragment, sends the parameters in the query string', () => { + setLocation(`${AUTHORIZER}/app#${AUTHORIZE_QUERY}`); + render(); + + const back = new URL(clickGithub().get('redirect_uri')!); + expect(back.hash).toBe(''); + expect(back.searchParams.get('nonce')).toBe('n1'); + }); + + it('on your own site, keeps the redirect_uri you passed', () => { + authorizerURL = AUTHORIZER; + setLocation(`https://myapp.example.com/login?${AUTHORIZE_QUERY}`); + render( + + ); + + const q = clickGithub(); + expect(q.get('redirect_uri')).toBe('https://myapp.example.com/dashboard'); + expect(q.get('redirectURL')).toBe('https://myapp.example.com/dashboard'); + }); + + it('on /app with no /authorize request, keeps the default redirect', () => { + setLocation(`${AUTHORIZER}/app`); + render( + + ); + + expect(clickGithub().get('redirect_uri')).toBe(`${AUTHORIZER}/app`); + }); +}); diff --git a/src/__tests__/authorizeResume.test.ts b/src/__tests__/authorizeResume.test.ts new file mode 100644 index 0000000..2c556f3 --- /dev/null +++ b/src/__tests__/authorizeResume.test.ts @@ -0,0 +1,111 @@ +import { describe, expect, it } from 'vitest'; + +import { getAuthorizeResumeURL } from '../utils/authorizeResume'; + +const AUTHORIZER = 'https://auth.example.com'; + +// What /authorize appends when it hands an unauthenticated user to /app. +const AUTHORIZE_QUERY = + 'state=s1&scope=openid+profile+email&redirect_uri=https%3A%2F%2Fapp.example.org%2Fcb' + + '&response_mode=query&response_type=code&client_id=c1&nonce=n1' + + '&code_challenge=ch&code_challenge_method=S256'; + +const loc = (url: string) => { + const u = new URL(url); + return { + origin: u.origin, + pathname: u.pathname, + search: u.search, + hash: u.hash, + }; +}; + +describe('getAuthorizeResumeURL', () => { + it('returns the /app URL with every /authorize parameter', () => { + const got = getAuthorizeResumeURL( + AUTHORIZER, + loc(`${AUTHORIZER}/app?${AUTHORIZE_QUERY}`) + ); + expect(got).not.toBeNull(); + const u = new URL(got!); + expect(u.origin + u.pathname).toBe(`${AUTHORIZER}/app`); + expect(Object.fromEntries(u.searchParams)).toEqual( + Object.fromEntries(new URLSearchParams(AUTHORIZE_QUERY)) + ); + }); + + it('works on /app/, the URL a browser is actually on (Authorizer 301s /app to /app/)', () => { + const got = getAuthorizeResumeURL( + AUTHORIZER, + loc(`${AUTHORIZER}/app/?${AUTHORIZE_QUERY}`) + ); + const u = new URL(got!); + expect(u.pathname).toBe('/app/'); + expect(u.searchParams.get('client_id')).toBe('c1'); + }); + + it('works on /app/signup (screen_hint=signup)', () => { + const got = getAuthorizeResumeURL( + AUTHORIZER, + loc(`${AUTHORIZER}/app/signup?${AUTHORIZE_QUERY}`) + ); + expect(new URL(got!).pathname).toBe('/app/signup'); + }); + + it('moves fragment parameters (response_mode=fragment) into the query', () => { + const got = getAuthorizeResumeURL( + AUTHORIZER, + loc(`${AUTHORIZER}/app#${AUTHORIZE_QUERY}`) + ); + const u = new URL(got!); + expect(u.hash).toBe(''); + expect(u.searchParams.get('client_id')).toBe('c1'); + expect(u.searchParams.get('nonce')).toBe('n1'); + }); + + it('lets query values win over fragment values, as /app does', () => { + const got = getAuthorizeResumeURL( + AUTHORIZER, + loc( + `${AUTHORIZER}/app?client_id=fromQuery&response_type=code#client_id=fromHash&state=s` + ) + ); + const u = new URL(got!); + expect(u.searchParams.get('client_id')).toBe('fromQuery'); + expect(u.searchParams.get('state')).toBe('s'); + }); + + it.each([ + [ + 'a login UI on your own site', + `https://myapp.example.com/app?${AUTHORIZE_QUERY}`, + ], + ['another port', `https://auth.example.com:8443/app?${AUTHORIZE_QUERY}`], + ['another scheme', `http://auth.example.com/app?${AUTHORIZE_QUERY}`], + ['another path', `${AUTHORIZER}/app/forgot-password?${AUTHORIZE_QUERY}`], + [ + '/app/signup/ with a trailing slash', + `${AUTHORIZER}/app/signup/?${AUTHORIZE_QUERY}`, + ], + ['plain /app sign-in, no /authorize request', `${AUTHORIZER}/app`], + ['no response_type', `${AUTHORIZER}/app?client_id=c1&state=s`], + ['no client_id', `${AUTHORIZER}/app?response_type=code&state=s`], + ])('returns null for %s', (_name, url) => { + expect(getAuthorizeResumeURL(AUTHORIZER, loc(url))).toBeNull(); + }); + + it('returns null when authorizerURL is not a URL', () => { + expect( + getAuthorizeResumeURL('', loc(`${AUTHORIZER}/app?${AUTHORIZE_QUERY}`)) + ).toBeNull(); + }); + + it('accepts authorizerURL with a trailing slash', () => { + expect( + getAuthorizeResumeURL( + `${AUTHORIZER}/`, + loc(`${AUTHORIZER}/app?${AUTHORIZE_QUERY}`) + ) + ).not.toBeNull(); + }); +}); diff --git a/src/components/AuthorizerBasicAuthLogin.tsx b/src/components/AuthorizerBasicAuthLogin.tsx index 81667fb..8bc4a1e 100644 --- a/src/components/AuthorizerBasicAuthLogin.tsx +++ b/src/components/AuthorizerBasicAuthLogin.tsx @@ -347,7 +347,7 @@ export const AuthorizerBasicAuthLogin: FC<{ {config.is_sign_up_enabled && (
- Don't have an account?{' '} + Don't have an account?{' '} setView(Views.Signup)}> Sign Up diff --git a/src/components/AuthorizerSocialLogin.tsx b/src/components/AuthorizerSocialLogin.tsx index b6c9065..9310dc6 100644 --- a/src/components/AuthorizerSocialLogin.tsx +++ b/src/components/AuthorizerSocialLogin.tsx @@ -7,6 +7,8 @@ import { StyledButton, StyledSeparator } from '../styledComponents'; import { useAuthorizer } from '../contexts/AuthorizerContext'; import { ButtonAppearance } from '../constants'; import { createQueryParams } from '../utils/common'; +import { getAuthorizeResumeURL } from '../utils/authorizeResume'; +import { hasWindow } from '../utils/window'; import { LinkedIn } from '../icons/linkedin'; import { Apple } from '../icons/apple'; import { Twitter } from '../icons/twitter'; @@ -54,6 +56,18 @@ export const AuthorizerSocialLogin: FC<{ data.redirect_uri = config.redirectURL; } + // On Authorizer's own login page during an /authorize request, return to + // this page instead of the client's callback, so the login resumes through + // /authorize (client redirect_uri check, consent, PKCE). redirectURL is + // dropped too: the server reads it before redirect_uri. + const resumeURL = hasWindow() + ? getAuthorizeResumeURL(config.authorizerURL, window.location) + : null; + if (resumeURL) { + data.redirect_uri = resumeURL; + delete data.redirectURL; + } + const queryParams = createQueryParams(data); return ( diff --git a/src/components/AuthorizerVerifyOtp.tsx b/src/components/AuthorizerVerifyOtp.tsx index ac309a6..2e8b201 100644 --- a/src/components/AuthorizerVerifyOtp.tsx +++ b/src/components/AuthorizerVerifyOtp.tsx @@ -504,7 +504,7 @@ export const AuthorizerVerifyOtp: FC<{ ))} {config.is_sign_up_enabled && (
- Don't have an account?{' '} + Don't have an account?{' '} setView(Views.Signup)}> Sign Up diff --git a/src/components/PasswordStrengthIndicator.tsx b/src/components/PasswordStrengthIndicator.tsx index fe34b09..2f5252a 100644 --- a/src/components/PasswordStrengthIndicator.tsx +++ b/src/components/PasswordStrengthIndicator.tsx @@ -8,7 +8,7 @@ import { validatePassword } from '../utils/validations'; interface PropTypes { value: string; - setDisableButton: Function; + setDisableButton: (disabled: boolean) => void; } const PasswordStrengthIndicator = ({ value, setDisableButton }: PropTypes) => { diff --git a/src/contexts/AuthorizerContext.tsx b/src/contexts/AuthorizerContext.tsx index 23324aa..b28cf30 100644 --- a/src/contexts/AuthorizerContext.tsx +++ b/src/contexts/AuthorizerContext.tsx @@ -94,7 +94,7 @@ function reducer( } } -let initialState: AuthorizerState = { +const initialState: AuthorizerState = { user: null, token: null, loading: true, @@ -233,7 +233,7 @@ export const AuthorizerProvider: FC<{ }, }); } - } catch (err) { + } catch { dispatch({ type: AuthorizerProviderActionType.SET_AUTH_DATA, payload: { diff --git a/src/stories/decorators.tsx b/src/stories/decorators.tsx index bb80eda..3825335 100644 --- a/src/stories/decorators.tsx +++ b/src/stories/decorators.tsx @@ -7,7 +7,7 @@ import StyledWrapper from '../styledComponents/StyledWrapper'; // inside Storybook regardless of the host browser. export const withWebauthnSupport = (supported: boolean): Decorator => - (Story) => { + function WithWebauthnSupport(Story) { if (supported) { (window as any).PublicKeyCredential = { parseCreationOptionsFromJSON: () => undefined, diff --git a/src/utils/authorizeResume.ts b/src/utils/authorizeResume.ts new file mode 100644 index 0000000..f5a1397 --- /dev/null +++ b/src/utils/authorizeResume.ts @@ -0,0 +1,61 @@ +// Login page paths Authorizer's /authorize hands an unauthenticated user to. +// The server accepts only these, exactly, as a social-login return target. +// /app/ is the one a browser is actually on: /authorize redirects to /app, +// and Authorizer 301s that to /app/. +const RESUME_PATHS = ['/app', '/app/', '/app/signup']; + +/** + * getAuthorizeResumeURL returns the URL a social login should come back to so + * it resumes the pending /authorize request, or null when there is none. + * + * When /authorize needs a sign-in it sends the browser to Authorizer's /app + * with the request's parameters (client_id, response_type, redirect_uri, + * state, nonce, PKCE, ...). Password login then replays them to /authorize, + * which checks the client's registered redirect_uri, runs consent and binds + * PKCE before issuing the code. Social login must take the same route, so its + * redirect_uri is this /app URL rather than the client's callback. + * + * Returns a URL only when ALL of these hold, so nothing changes anywhere else: + * - the page is served by Authorizer itself: its origin equals the origin of + * `authorizerURL`. A login UI on your own site never resumes, because + * /authorize never sends users there; + * - the path is exactly /app, /app/ or /app/signup; + * - the URL carries an /authorize request: client_id AND response_type. + * + * Parameters arriving in the fragment (response_mode=fragment) move into the + * query string, since a server redirect cannot carry a fragment and Authorizer + * refuses one. Query values win over fragment values, as on /app. + * + * Requires Authorizer 2.4.6 or later, which accepts its own /app here. The + * hosted login page is built into the same image as the server, so the two + * always match. + */ +export const getAuthorizeResumeURL = ( + authorizerURL: string, + location: Pick +): string | null => { + let authorizerOrigin: string; + try { + authorizerOrigin = new URL(authorizerURL).origin; + } catch { + return null; + } + if (location.origin !== authorizerOrigin) { + return null; + } + if (!RESUME_PATHS.includes(location.pathname)) { + return null; + } + + const params = new URLSearchParams( + location.hash ? location.hash.replace(/^#/, '') : '' + ); + new URLSearchParams(location.search).forEach((value, key) => { + params.set(key, value); + }); + if (!params.get('client_id') || !params.get('response_type')) { + return null; + } + + return `${location.origin}${location.pathname}?${params.toString()}`; +}; diff --git a/src/utils/url.ts b/src/utils/url.ts index 6eee7e1..4c70c44 100644 --- a/src/utils/url.ts +++ b/src/utils/url.ts @@ -6,7 +6,6 @@ export const getSearchParams = (search = ''): Record => { searchPrams = window.location.search; } const urlSearchParams = new URLSearchParams(`${searchPrams}`); - // @ts-ignore const params = Object.fromEntries(urlSearchParams.entries()); return params; }; diff --git a/src/utils/validations.ts b/src/utils/validations.ts index ee95f7b..591e434 100644 --- a/src/utils/validations.ts +++ b/src/utils/validations.ts @@ -4,7 +4,7 @@ export const isValidOtp = (otp: string): boolean => { }; export const hasSpecialChar = (char: string): boolean => { - const re = /[`!@#$%^&*()_+\-=\[\]{};':"\\|,.<>\/?~]/; + const re = /[`!@#$%^&*()_+\-=[\]{};':"\\|,.<>/?~]/; return re.test(char); };