From a4addf9aee3be4ecff82a6c9fbf0268e7e26d502 Mon Sep 17 00:00:00 2001 From: Alex Gleason Date: Wed, 25 Jan 2023 16:32:59 -0600 Subject: [PATCH] Fix external login scopes --- app/soapbox/actions/external-auth.ts | 12 ++++++------ app/soapbox/utils/scopes.ts | 14 +++++++++++--- 2 files changed, 17 insertions(+), 9 deletions(-) diff --git a/app/soapbox/actions/external-auth.ts b/app/soapbox/actions/external-auth.ts index bf0618395..87840cfe7 100644 --- a/app/soapbox/actions/external-auth.ts +++ b/app/soapbox/actions/external-auth.ts @@ -15,7 +15,7 @@ import sourceCode from 'soapbox/utils/code'; import { getWalletAndSign } from 'soapbox/utils/ethereum'; import { getFeatures } from 'soapbox/utils/features'; import { getQuirks } from 'soapbox/utils/quirks'; -import { getScopes } from 'soapbox/utils/scopes'; +import { getInstanceScopes } from 'soapbox/utils/scopes'; import { baseClient } from '../api'; @@ -38,7 +38,7 @@ const fetchExternalInstance = (baseURL?: string) => { }; const createExternalApp = (instance: Instance, baseURL?: string) => - (dispatch: AppDispatch, getState: () => RootState) => { + (dispatch: AppDispatch, _getState: () => RootState) => { // Mitra: skip creating the auth app if (getQuirks(instance).noApps) return new Promise(f => f({})); @@ -46,15 +46,15 @@ const createExternalApp = (instance: Instance, baseURL?: string) => client_name: sourceCode.displayName, redirect_uris: `${window.location.origin}/login/external`, website: sourceCode.homepage, - scopes: getScopes(getState()), + scopes: getInstanceScopes(instance), }; return dispatch(createApp(params, baseURL)); }; const externalAuthorize = (instance: Instance, baseURL: string) => - (dispatch: AppDispatch, getState: () => RootState) => { - const scopes = getScopes(getState()); + (dispatch: AppDispatch, _getState: () => RootState) => { + const scopes = getInstanceScopes(instance); return dispatch(createExternalApp(instance, baseURL)).then((app) => { const { client_id, redirect_uri } = app as Record; @@ -88,7 +88,7 @@ const externalEthereumLogin = (instance: Instance, baseURL?: string) => client_secret: client_secret, password: signature as string, redirect_uri: 'urn:ietf:wg:oauth:2.0:oob', - scope: getScopes(getState()), + scope: getInstanceScopes(instance), }; return dispatch(obtainOAuthToken(params, baseURL)) diff --git a/app/soapbox/utils/scopes.ts b/app/soapbox/utils/scopes.ts index 32c5ef04f..d98118ae8 100644 --- a/app/soapbox/utils/scopes.ts +++ b/app/soapbox/utils/scopes.ts @@ -1,13 +1,14 @@ -import { RootState } from 'soapbox/store'; import { PLEROMA, parseVersion } from './features'; +import type { RootState } from 'soapbox/store'; +import type { Instance } from 'soapbox/types/entities'; + /** * Get the OAuth scopes to use for login & signup. * Mastodon will refuse scopes it doesn't know, so care is needed. */ -const getScopes = (state: RootState) => { - const instance = state.instance; +const getInstanceScopes = (instance: Instance) => { const v = parseVersion(instance.version); switch (v.software) { @@ -18,6 +19,13 @@ const getScopes = (state: RootState) => { } }; +/** Convenience function to get scopes from instance in store. */ +const getScopes = (state: RootState) => { + return getInstanceScopes(state.instance); +}; + + export { + getInstanceScopes, getScopes, }; \ No newline at end of file