-
Notifications
You must be signed in to change notification settings - Fork 187
✨ [FFL-2597] Feature Flags tab — OAuth sign-in (stacked PR 1 of 3) #4913
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
kellyw1806
wants to merge
1
commit into
main
Choose a base branch
from
kelly.wang/ffl-2597
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
78 changes: 78 additions & 0 deletions
78
developer-extension/src/panel/components/tabs/flagsTab/connectScreen.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,78 @@ | ||
| import { Anchor, Badge, Button, Center, Group, Select, Stack, Text } from '@mantine/core' | ||
| import React, { useState } from 'react' | ||
| import { useSettings } from '../../../hooks/useSettings' | ||
| import type { FlagAuthState } from './useFlagAuth' | ||
| import { FLAG_SITES } from './oauth' | ||
|
|
||
| export function ConnectScreen({ auth }: { auth: FlagAuthState }) { | ||
| const [advancedOpen, setAdvancedOpen] = useState(false) | ||
|
|
||
| return ( | ||
| <Center h="100%" className="dd-privacy-allow"> | ||
| <Stack align="center" gap="md" maw={460} px="md"> | ||
| <Text size="xl" fw={600} ta="center"> | ||
| Authenticate with Datadog to access your feature flags | ||
| </Text> | ||
| <Button color="violet" onClick={auth.connect} loading={auth.connecting}> | ||
| Sign in to Datadog | ||
| </Button> | ||
| {auth.error && ( | ||
| <Text c="red" size="xs" ta="center"> | ||
| {auth.error} | ||
| </Text> | ||
| )} | ||
|
|
||
| <Anchor component="button" type="button" size="xs" c="dimmed" onClick={() => setAdvancedOpen((open) => !open)}> | ||
| {advancedOpen ? '− Hide advanced' : 'Advanced: site'} | ||
| </Anchor> | ||
| {advancedOpen && ( | ||
| <Stack gap="sm" style={{ width: '100%' }}> | ||
| <SiteField /> | ||
| </Stack> | ||
| )} | ||
| </Stack> | ||
| </Center> | ||
| ) | ||
| } | ||
|
|
||
| export function ConnectionHeader({ auth }: { auth: FlagAuthState }) { | ||
| return ( | ||
| <Stack gap={4}> | ||
| <Group justify="space-between"> | ||
| <Group gap="xs"> | ||
| <Badge color="green" variant="light"> | ||
| Connected via OAuth | ||
| </Badge> | ||
| <Text c="dimmed" size="xs"> | ||
| {auth.site} | ||
| </Text> | ||
| </Group> | ||
| <Button size="compact-xs" variant="subtle" color="gray" onClick={auth.disconnect}> | ||
| Disconnect | ||
| </Button> | ||
| </Group> | ||
| {/* Surface disconnect failures here too — otherwise a failed Disconnect looks like a no-op. */} | ||
| {auth.error && ( | ||
| <Text c="red" size="xs" ta="right"> | ||
| {auth.error} | ||
| </Text> | ||
| )} | ||
| </Stack> | ||
| ) | ||
| } | ||
|
|
||
| function SiteField() { | ||
| const [{ flagsSite }, setSetting] = useSettings() | ||
|
|
||
| return ( | ||
| <Select | ||
| label="Datadog site" | ||
| description="Your organization's Datadog site." | ||
| data={FLAG_SITES.map(({ site, label }) => ({ value: site, label }))} | ||
| value={flagsSite} | ||
| onChange={(value) => value && setSetting('flagsSite', value)} | ||
| allowDeselect={false} | ||
| size="xs" | ||
| /> | ||
| ) | ||
| } |
31 changes: 31 additions & 0 deletions
31
developer-extension/src/panel/components/tabs/flagsTab/flagsTab.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| import { Box } from '@mantine/core' | ||
| import React from 'react' | ||
| import { TabBase } from '../../tabBase' | ||
| import { useFlagAuth } from './useFlagAuth' | ||
| import { ConnectScreen, ConnectionHeader } from './connectScreen' | ||
|
|
||
| export function FlagsTab() { | ||
| const auth = useFlagAuth() | ||
|
|
||
| // Gate the whole tab: nothing shows until the user connects via OAuth. Browsing the flag catalog | ||
| // is added on top of this in the follow-up PR. | ||
| if (!auth.isConnected) { | ||
| return ( | ||
| <TabBase> | ||
| <ConnectScreen auth={auth} /> | ||
| </TabBase> | ||
| ) | ||
| } | ||
|
|
||
| return ( | ||
| <TabBase | ||
| top={ | ||
| <Box px="md" className="dd-privacy-allow"> | ||
| <ConnectionHeader auth={auth} /> | ||
| </Box> | ||
| } | ||
| > | ||
| <Box px="md" py="sm" className="dd-privacy-allow" /> | ||
| </TabBase> | ||
| ) | ||
| } |
1 change: 1 addition & 0 deletions
1
developer-extension/src/panel/components/tabs/flagsTab/index.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| export { FlagsTab } from './flagsTab' |
75 changes: 75 additions & 0 deletions
75
developer-extension/src/panel/components/tabs/flagsTab/oauth.spec.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,75 @@ | ||
| import { registerCleanupTask, replaceMockable } from '../../../../../../packages/browser-core/test' | ||
| import { getFlagsApiHost, loginWithOAuth, sha256 } from './oauth' | ||
|
|
||
| describe('oauth', () => { | ||
| describe('getFlagsApiHost', () => { | ||
| it('maps each site to its frontend host (US1/EU1 → app, staging → dd, regional sites as-is)', () => { | ||
| expect(getFlagsApiHost('datadoghq.com')).toBe('app.datadoghq.com') | ||
| expect(getFlagsApiHost('datadoghq.eu')).toBe('app.datadoghq.eu') | ||
| expect(getFlagsApiHost('datad0g.com')).toBe('dd.datad0g.com') | ||
| expect(getFlagsApiHost('us3.datadoghq.com')).toBe('us3.datadoghq.com') | ||
| expect(getFlagsApiHost('ddog-gov.com')).toBe('ddog-gov.com') | ||
| }) | ||
|
|
||
| it('throws on a site that is not in the known list', () => { | ||
| expect(() => getFlagsApiHost('evil.example')).toThrowError(/Unknown Datadog site/) | ||
| expect(() => getFlagsApiHost('')).toThrowError(/Unknown Datadog site/) | ||
| }) | ||
| }) | ||
|
|
||
| describe('loginWithOAuth', () => { | ||
| // loginWithOAuth's PKCE step hashes with crypto.subtle, which is only exposed in a secure | ||
| // context — some CI browsers (mobile devices reached over http) don't provide it. Stub the hash | ||
| // via its mockable seam so these tests don't depend on the runtime's secure-context status. | ||
| // (Production runs on the extension's chrome-extension:// origin, always a secure context.) | ||
| beforeEach(() => { | ||
| replaceMockable(sha256, () => Promise.resolve(new Uint8Array(32).buffer)) | ||
| }) | ||
|
|
||
| // Stub chrome.identity so launchWebAuthFlow echoes back a redirect built from the state that | ||
| // loginWithOAuth actually generated (so the state check passes and we exercise the domain check). | ||
| function mockChromeIdentity(makeRedirect: (params: { state: string }) => string) { | ||
| const previousChrome = (globalThis as any).chrome | ||
| ;(globalThis as any).chrome = { | ||
| identity: { | ||
| getRedirectURL: () => 'https://ext-id.chromiumapp.org/', | ||
| launchWebAuthFlow: ({ url }: { url: string }) => { | ||
| const state = new URL(url).searchParams.get('state')! | ||
| return Promise.resolve(makeRedirect({ state })) | ||
| }, | ||
| }, | ||
| } | ||
| registerCleanupTask(() => { | ||
| ;(globalThis as any).chrome = previousChrome | ||
| }) | ||
| } | ||
|
|
||
| it('aborts when the redirect domain does not match the selected site', async () => { | ||
| mockChromeIdentity(({ state }) => `https://ext-id.chromiumapp.org/?code=abc&state=${state}&domain=datadoghq.com`) | ||
| const fetchSpy = spyOn(globalThis, 'fetch') | ||
|
|
||
| await expectAsync(loginWithOAuth('datad0g.com')).toBeRejectedWithError(/but "datad0g.com" was selected/) | ||
| expect(fetchSpy).not.toHaveBeenCalled() | ||
| }) | ||
|
|
||
| it('exchanges the code when the redirect domain matches the selected site', async () => { | ||
| mockChromeIdentity(({ state }) => `https://ext-id.chromiumapp.org/?code=abc&state=${state}&domain=datad0g.com`) | ||
| spyOn(globalThis, 'fetch').and.returnValue( | ||
| Promise.resolve(new Response(JSON.stringify({ access_token: 'tok', expires_in: 3600 }))) | ||
| ) | ||
|
|
||
| const tokens = await loginWithOAuth('datad0g.com') | ||
| expect(tokens.accessToken).toBe('tok') | ||
| }) | ||
|
|
||
| it('proceeds when the redirect omits a domain', async () => { | ||
| mockChromeIdentity(({ state }) => `https://ext-id.chromiumapp.org/?code=abc&state=${state}`) | ||
| spyOn(globalThis, 'fetch').and.returnValue( | ||
| Promise.resolve(new Response(JSON.stringify({ access_token: 'tok', expires_in: 3600 }))) | ||
| ) | ||
|
|
||
| const tokens = await loginWithOAuth('datad0g.com') | ||
| expect(tokens.accessToken).toBe('tok') | ||
| }) | ||
| }) | ||
| }) | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
issue: This spec replaces the entire
globalThis.chromeobject and directly spies on browser globals instead of wrapping those source dependencies withmockable()and usingreplaceMockable()orreplaceMockableWithSpy(). Besides discarding unrelated Chrome APIs for the duration of each test, this bypasses the repository's required auto-cleanup mocking pattern and makes the OAuth tests brittle as the implementation starts using additional APIs; expose narrow mockable wrappers rather than replacing the browser globals.AGENTS.md reference: AGENTS.md:L105-L105
Useful? React with 👍 / 👎.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Agreed. @kellyw1806 would it be possible to use something like: https://github.com/DataDog/browser-sdk/blob/kelly.wang/ffl-2597/packages/browser-core/src/tools/mockable.ts#L25?