|
| 1 | +import React, { useMemo, useState } from 'react' |
| 2 | +import { useLocation } from 'react-router-dom' |
| 3 | +import { |
| 4 | + useProcessOAuthConsentMutation, |
| 5 | + useValidateOAuthAuthorizeQuery, |
| 6 | +} from 'common/services/useOAuthAuthorize' |
| 7 | +import Utils from 'common/utils/utils' |
| 8 | +import Icon from 'components/Icon' |
| 9 | +import Logo from 'components/Logo' |
| 10 | + |
| 11 | +// Frontend-maintained scope descriptions. The backend returns `mcp` as an |
| 12 | +// umbrella scope; we expand it into granular descriptions for the consent UI. |
| 13 | +// If a scope is not found here, the backend description is used as fallback. |
| 14 | +const SCOPE_DESCRIPTIONS: Record<string, string[]> = { |
| 15 | + mcp: [ |
| 16 | + 'Manage feature flags, toggle states, and update values', |
| 17 | + 'Create and manage audience targeting segments', |
| 18 | + 'View and configure environments', |
| 19 | + 'View and update project settings', |
| 20 | + 'Create and review change requests', |
| 21 | + 'View organisation details, roles, and groups', |
| 22 | + ], |
| 23 | +} |
| 24 | + |
| 25 | +const OAuthAuthorizePage = () => { |
| 26 | + const location = useLocation() |
| 27 | + const [isRedirecting, setIsRedirecting] = useState(false) |
| 28 | + const [consentError, setConsentError] = useState<string | null>(null) |
| 29 | + |
| 30 | + const oauthParams = useMemo(() => { |
| 31 | + const params = Utils.fromParam(location.search) |
| 32 | + return { |
| 33 | + client_id: params.client_id || '', |
| 34 | + code_challenge: params.code_challenge || '', |
| 35 | + code_challenge_method: params.code_challenge_method || '', |
| 36 | + redirect_uri: params.redirect_uri || '', |
| 37 | + response_type: params.response_type || '', |
| 38 | + scope: params.scope || 'mcp', |
| 39 | + state: params.state || '', |
| 40 | + } |
| 41 | + }, [location.search]) |
| 42 | + |
| 43 | + const hasRequiredParams = !!( |
| 44 | + oauthParams.client_id && |
| 45 | + oauthParams.redirect_uri && |
| 46 | + oauthParams.response_type && |
| 47 | + oauthParams.code_challenge && |
| 48 | + oauthParams.code_challenge_method |
| 49 | + ) |
| 50 | + |
| 51 | + const { data, error, isLoading } = useValidateOAuthAuthorizeQuery( |
| 52 | + oauthParams, |
| 53 | + { |
| 54 | + refetchOnFocus: false, |
| 55 | + refetchOnReconnect: false, |
| 56 | + skip: !hasRequiredParams, |
| 57 | + }, |
| 58 | + ) |
| 59 | + |
| 60 | + const [processConsent, { isLoading: isProcessing }] = |
| 61 | + useProcessOAuthConsentMutation() |
| 62 | + |
| 63 | + const handleConsent = async (allow: boolean) => { |
| 64 | + try { |
| 65 | + setConsentError(null) |
| 66 | + setIsRedirecting(true) |
| 67 | + const result = await processConsent({ |
| 68 | + ...oauthParams, |
| 69 | + allow, |
| 70 | + }).unwrap() |
| 71 | + window.location.href = result.redirect_uri |
| 72 | + } catch (e) { |
| 73 | + console.error('OAuth consent error:', e) |
| 74 | + setIsRedirecting(false) |
| 75 | + setConsentError('Something went wrong. Please try again.') |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | + const renderContent = () => { |
| 80 | + if (!hasRequiredParams) { |
| 81 | + return ( |
| 82 | + <div className='oauth-authorize__card card shadow p-4'> |
| 83 | + <h3>Invalid authorisation request</h3> |
| 84 | + <p className='text-muted'> |
| 85 | + Required OAuth parameters are missing. Please return to the |
| 86 | + application and try again. |
| 87 | + </p> |
| 88 | + </div> |
| 89 | + ) |
| 90 | + } |
| 91 | + |
| 92 | + if (isLoading) { |
| 93 | + return ( |
| 94 | + <div className='oauth-authorize__card card shadow p-4'> |
| 95 | + <div className='centered-container'> |
| 96 | + <Loader /> |
| 97 | + </div> |
| 98 | + </div> |
| 99 | + ) |
| 100 | + } |
| 101 | + |
| 102 | + if (error || !data) { |
| 103 | + return ( |
| 104 | + <div className='oauth-authorize__card card shadow p-4'> |
| 105 | + <h3>Authorisation error</h3> |
| 106 | + <p className='text-muted'> |
| 107 | + The authorisation request is invalid. The application may have |
| 108 | + provided incorrect parameters. |
| 109 | + </p> |
| 110 | + </div> |
| 111 | + ) |
| 112 | + } |
| 113 | + |
| 114 | + return ( |
| 115 | + <div className='oauth-authorize__card card shadow p-4'> |
| 116 | + <div className='text-center mb-4'> |
| 117 | + <Logo size={48} /> |
| 118 | + <h3 className='oauth-authorize__title mb-0'> |
| 119 | + <strong>{data.application.name}</strong> would like to connect to |
| 120 | + your Flagsmith account |
| 121 | + </h3> |
| 122 | + </div> |
| 123 | + |
| 124 | + {!data.is_verified && ( |
| 125 | + <div className='oauth-authorize__warning'> |
| 126 | + <Icon name='warning' width={16} fill='#e5a00d' /> |
| 127 | + <span> |
| 128 | + This application is unverified. Only authorise if you trust it. |
| 129 | + </span> |
| 130 | + </div> |
| 131 | + )} |
| 132 | + |
| 133 | + <div className='oauth-authorize__scope-box'> |
| 134 | + <p className='oauth-authorize__scope-header mb-3 mt-0'> |
| 135 | + YOUR ACCOUNT WILL BE USED TO: |
| 136 | + </p> |
| 137 | + <div className='d-flex flex-column gap-3'> |
| 138 | + {Object.entries(data.scopes).flatMap(([scope, description]) => { |
| 139 | + const descriptions = SCOPE_DESCRIPTIONS[scope] |
| 140 | + if (descriptions) { |
| 141 | + return descriptions.map((desc, i) => ( |
| 142 | + <div |
| 143 | + key={`${scope}-${i}`} |
| 144 | + className='oauth-authorize__scope-item' |
| 145 | + > |
| 146 | + <Icon name='checkmark' width={20} fill='#6837fc' /> |
| 147 | + <span>{desc}</span> |
| 148 | + </div> |
| 149 | + )) |
| 150 | + } |
| 151 | + return [ |
| 152 | + <div key={scope} className='oauth-authorize__scope-item'> |
| 153 | + <Icon name='checkmark' width={20} fill='#6837fc' /> |
| 154 | + <span>{description}</span> |
| 155 | + </div>, |
| 156 | + ] |
| 157 | + })} |
| 158 | + </div> |
| 159 | + </div> |
| 160 | + |
| 161 | + <p className='oauth-authorize__redirect-text text-muted text-center mb-4'> |
| 162 | + You will be redirected to: |
| 163 | + <br /> |
| 164 | + <code className='oauth-authorize__redirect-uri'> |
| 165 | + {data.redirect_uri} |
| 166 | + </code> |
| 167 | + </p> |
| 168 | + |
| 169 | + {consentError && ( |
| 170 | + <p className='oauth-authorize__error text-center text-danger mb-2'> |
| 171 | + {consentError} |
| 172 | + </p> |
| 173 | + )} |
| 174 | + |
| 175 | + {isRedirecting || isProcessing ? ( |
| 176 | + <div className='centered-container'> |
| 177 | + <Loader /> |
| 178 | + </div> |
| 179 | + ) : ( |
| 180 | + <div className='d-flex flex-column gap-2'> |
| 181 | + <Button className='w-100' onClick={() => handleConsent(true)}> |
| 182 | + Authorise |
| 183 | + </Button> |
| 184 | + <Button |
| 185 | + theme='secondary' |
| 186 | + className='w-100' |
| 187 | + onClick={() => handleConsent(false)} |
| 188 | + > |
| 189 | + Decline |
| 190 | + </Button> |
| 191 | + </div> |
| 192 | + )} |
| 193 | + </div> |
| 194 | + ) |
| 195 | + } |
| 196 | + |
| 197 | + return <div className='oauth-authorize'>{renderContent()}</div> |
| 198 | +} |
| 199 | + |
| 200 | +export default OAuthAuthorizePage |
0 commit comments