-
Notifications
You must be signed in to change notification settings - Fork 135
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #616 from aws-samples/feat/extension-saml
ブラウザ拡張機能: SAML認証対応
- Loading branch information
Showing
23 changed files
with
523 additions
and
96 deletions.
There are no files selected for viewing
This file contains 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 |
---|---|---|
@@ -1,4 +1,6 @@ | ||
.DS_Store | ||
node_modules | ||
|
||
!.gitkeep | ||
!.gitkeep | ||
|
||
/*/dist/** |
This file contains 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 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
66 changes: 66 additions & 0 deletions
66
browser-extension/src/app/features/common/components/AuthWithSAML.tsx
This file contains 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,66 @@ | ||
import React, { useEffect } from 'react'; | ||
import { Amplify } from 'aws-amplify'; | ||
import '@aws-amplify/ui-react/styles.css'; | ||
import useSettings from '../../settings/useSettings'; | ||
import Browser from 'webextension-polyfill'; | ||
import useAuth from '../hooks/useAuth'; | ||
import Button from './Button'; | ||
import { PiCircleNotchBold } from 'react-icons/pi'; | ||
|
||
type Props = { | ||
children: React.ReactNode; | ||
}; | ||
|
||
const AuthWithSAML: React.FC<Props> = (props) => { | ||
const { settings } = useSettings(); | ||
const { loading, authenticate, hasAuthenticated } = useAuth(); | ||
|
||
useEffect(() => { | ||
if (settings) { | ||
Amplify.configure({ | ||
Auth: { | ||
Cognito: { | ||
userPoolId: import.meta.env.VITE_APP_USER_POOL_ID, | ||
userPoolClientId: import.meta.env.VITE_APP_USER_POOL_CLIENT_ID, | ||
identityPoolId: import.meta.env.VITE_APP_IDENTITY_POOL_ID, | ||
loginWith: { | ||
oauth: { | ||
domain: settings.cognitoDomain ?? '', | ||
scopes: ['openid', 'email', 'profile'], | ||
redirectSignIn: [`${window.location.origin}/index.html`], | ||
redirectSignOut: [window.location.origin], | ||
responseType: 'code', | ||
}, | ||
}, | ||
}, | ||
}, | ||
}); | ||
// 認証の設定をしたら認証を実行 | ||
authenticate(); | ||
} | ||
}, [authenticate, settings]); | ||
|
||
const signIn = () => { | ||
const url = Browser.runtime.getURL('/index.html'); | ||
Browser.tabs.create({ url }); | ||
}; | ||
|
||
return ( | ||
<div className="flex justify-center mt-3"> | ||
{loading ? ( | ||
<div> | ||
<div className="italic">Loading...</div> | ||
<PiCircleNotchBold className="text-6xl animate-spin" /> | ||
</div> | ||
) : !hasAuthenticated ? ( | ||
<div> | ||
<Button onClick={() => signIn()}>ログイン画面へ</Button> | ||
</div> | ||
) : ( | ||
<>{props.children}</> | ||
)} | ||
</div> | ||
); | ||
}; | ||
|
||
export default AuthWithSAML; |
38 changes: 38 additions & 0 deletions
38
browser-extension/src/app/features/common/components/AuthWithUserPool.tsx
This file contains 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,38 @@ | ||
import { Authenticator, translations } from '@aws-amplify/ui-react'; | ||
import React, { useEffect, useMemo } from 'react'; | ||
import useSettings from '../../settings/useSettings'; | ||
import { Amplify } from 'aws-amplify'; | ||
import { I18n } from 'aws-amplify/utils'; | ||
|
||
type Props = { | ||
children: React.ReactNode; | ||
}; | ||
|
||
const AuthWithUserPool: React.FC<Props> = (props) => { | ||
const { settings } = useSettings(); | ||
|
||
useEffect(() => { | ||
if (settings) { | ||
Amplify.configure({ | ||
Auth: { | ||
Cognito: { | ||
userPoolId: settings.userPoolId, | ||
userPoolClientId: settings.userPoolClientId, | ||
identityPoolId: settings.identityPoolId, | ||
}, | ||
}, | ||
}); | ||
} | ||
}, [settings]); | ||
|
||
const enabledSelfSignUp = useMemo(() => { | ||
return settings?.enabledSelfSignUp ?? false; | ||
}, [settings?.enabledSelfSignUp]); | ||
|
||
I18n.putVocabularies(translations); | ||
I18n.setLanguage('ja'); | ||
|
||
return <Authenticator hideSignUp={!enabledSelfSignUp}>{props.children}</Authenticator>; | ||
}; | ||
|
||
export default AuthWithUserPool; |
31 changes: 31 additions & 0 deletions
31
browser-extension/src/app/features/common/components/RequiresAuth.tsx
This file contains 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 React from 'react'; | ||
import useSettings from '../../settings/useSettings'; | ||
import AuthWithUserPool from './AuthWithUserPool'; | ||
import AuthWithSAML from './AuthWithSAML'; | ||
import { PiCircleNotchBold } from 'react-icons/pi'; | ||
|
||
type Props = { | ||
children: React.ReactNode; | ||
}; | ||
|
||
const RequiresAuth: React.FC<Props> = (props) => { | ||
const { settings } = useSettings(); | ||
|
||
return ( | ||
<> | ||
{!settings ? ( | ||
<div className="flex flex-col items-center"> | ||
<div className="italic">Loading...</div> | ||
<PiCircleNotchBold className="text-6xl animate-spin" /> | ||
</div> | ||
) : ( | ||
<> | ||
{settings.enabledSamlAuth && <AuthWithSAML>{props.children}</AuthWithSAML>} | ||
{!settings.enabledSamlAuth && <AuthWithUserPool>{props.children}</AuthWithUserPool>} | ||
</> | ||
)} | ||
</> | ||
); | ||
}; | ||
|
||
export default RequiresAuth; |
30 changes: 30 additions & 0 deletions
30
browser-extension/src/app/features/common/components/Switch.tsx
This file contains 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,30 @@ | ||
import React from 'react'; | ||
import { BaseProps } from '../../../../@types/common'; | ||
|
||
type Props = BaseProps & { | ||
checked: boolean; | ||
onSwitch: (newValue: boolean) => void; | ||
label: string; | ||
}; | ||
|
||
const Switch: React.FC<Props> = (props) => { | ||
return ( | ||
<div> | ||
<label className="relative inline-flex cursor-pointer items-center hover:underline"> | ||
<input | ||
type="checkbox" | ||
value="" | ||
className="peer sr-only" | ||
checked={props.checked} | ||
onChange={() => { | ||
props.onSwitch(!props.checked); | ||
}} | ||
/> | ||
<div className="peer-checked:bg-aws-smile peer h-6 w-11 rounded-full bg-gray-200 after:absolute after:start-[2px] after:top-[2px] after:size-5 after:rounded-full after:border after:border-gray-300 after:bg-white after:transition-all after:content-[''] peer-checked:after:translate-x-full peer-checked:after:border-white rtl:peer-checked:after:-translate-x-full"></div> | ||
<span className="ml-1 text-xs font-medium">{props.label}</span> | ||
</label> | ||
</div> | ||
); | ||
}; | ||
|
||
export default Switch; |
14 changes: 12 additions & 2 deletions
14
browser-extension/src/app/features/common/hooks/useAuth.ts
This file contains 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 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
Oops, something went wrong.