-
Notifications
You must be signed in to change notification settings - Fork 48
feat: configurable SAML attributes and audience via environment variables #1200
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
chipach
wants to merge
3
commits into
ory:main
Choose a base branch
from
chipach:feat/configurable-saml-attributes
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
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1 @@ | ||
| import Login from '../../../saml/login'; | ||
|
|
||
| export default Login; | ||
| export { default, getServerSideProps } from '../../../saml/login'; |
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 |
|---|---|---|
| @@ -1,19 +1,33 @@ | ||
| import Head from 'next/head'; | ||
| import { useRouter } from 'next/router'; | ||
| import type { GetServerSideProps } from 'next'; | ||
| import type { FormEvent } from 'react'; | ||
| import { useEffect, useRef, useState } from 'react'; | ||
|
|
||
| export default function Login() { | ||
| type Attribute = { key: number; name: string; value: string }; | ||
|
|
||
| type Props = { | ||
| defaultAttributes: Omit<Attribute, 'key'>[]; | ||
| defaultAudience: string; | ||
| }; | ||
|
|
||
|
|
||
| export default function Login({ defaultAttributes, defaultAudience }: Props) { | ||
| const router = useRouter(); | ||
| const { id, audience, acsUrl, providerName, relayState, namespace } = router.query; | ||
|
|
||
| const authUrl = namespace ? `/api/namespace/${namespace}/saml/auth` : '/api/saml/auth'; | ||
| const nextKey = useRef(defaultAttributes.length); | ||
| const [state, setState] = useState({ | ||
| username: 'jackson', | ||
| domain: 'example.com', | ||
| acsUrl: 'https://sso.eu.boxyhq.com/api/oauth/saml', | ||
| audience: 'https://saml.boxyhq.com', | ||
| audience: defaultAudience, | ||
| }); | ||
| const [attributes, setAttributes] = useState<Attribute[]>( | ||
| defaultAttributes.map((a, i) => ({ ...a, key: i })) | ||
| ); | ||
| const [newAttr, setNewAttr] = useState({ name: '', value: '' }); | ||
|
|
||
| const acsUrlInp = useRef<HTMLInputElement>(null); | ||
| const emailInp = useRef<HTMLInputElement>(null); | ||
|
|
@@ -33,21 +47,39 @@ export default function Login() { | |
| setState({ ...state, [name]: value }); | ||
| }; | ||
|
|
||
| const handleAttrChange = (index: number, field: 'name' | 'value', value: string) => { | ||
| setAttributes((prev) => prev.map((a, i) => (i === index ? { ...a, [field]: value } : a))); | ||
| }; | ||
|
|
||
| const handleAttrRemove = (index: number) => { | ||
| setAttributes((prev) => prev.filter((_, i) => i !== index)); | ||
| }; | ||
|
|
||
| const handleAttrAdd = () => { | ||
| if (!newAttr.name) return; | ||
| setAttributes((prev) => [...prev, { ...newAttr, key: nextKey.current++ }]); | ||
| setNewAttr({ name: '', value: '' }); | ||
| }; | ||
|
|
||
| const handleSubmit = async (e: FormEvent<HTMLFormElement>) => { | ||
| e.preventDefault(); | ||
|
|
||
| const { username, domain } = state; | ||
| const email = `${username}@${domain}`; | ||
|
|
||
| const resolvedAttributes = attributes.map(({ name, value }) => ({ name, value })); | ||
|
|
||
| const response = await fetch(authUrl, { | ||
| method: 'POST', | ||
| headers: { 'Content-Type': 'application/json' }, | ||
| body: JSON.stringify({ | ||
| email: `${username}@${domain}`, | ||
| email, | ||
| id, | ||
| audience: audience || state.audience, | ||
| acsUrl: acsUrl || state.acsUrl, | ||
| providerName, | ||
| relayState, | ||
| attributes: resolvedAttributes, | ||
| }), | ||
| }); | ||
|
|
||
|
|
@@ -60,6 +92,10 @@ export default function Login() { | |
| } | ||
| }; | ||
|
|
||
| const inputBase = | ||
|
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not completely required for this PR, but since classes were being added for the attributes, a little DRYing was in order. |
||
| 'rounded-md border border-gray-300 px-3 py-2 text-sm focus:border-primary focus:ring-2 focus:ring-primary/30'; | ||
| const inputClass = `w-full ${inputBase}`; | ||
|
|
||
| return ( | ||
| <> | ||
| <Head> | ||
|
|
@@ -89,7 +125,7 @@ export default function Login() { | |
| value={state.acsUrl} | ||
| onChange={handleChange} | ||
| placeholder='https://sso.eu.boxyhq.com/api/oauth/saml' | ||
| className='w-full rounded-md border border-gray-300 px-3 py-2 text-sm focus:border-primary focus:ring-2 focus:ring-primary/30' | ||
| className={inputClass} | ||
| /> | ||
| <p className='mt-1 text-xs text-gray-500'> | ||
| This is where we will post the SAML Response | ||
|
|
@@ -106,7 +142,7 @@ export default function Login() { | |
| value={state.audience} | ||
| onChange={handleChange} | ||
| placeholder='https://saml.boxyhq.com' | ||
| className='w-full rounded-md border border-gray-300 px-3 py-2 text-sm focus:border-primary focus:ring-2 focus:ring-primary/30' | ||
| className={inputClass} | ||
| /> | ||
| </div> | ||
| </div> | ||
|
|
@@ -123,7 +159,7 @@ export default function Login() { | |
| value={state.username} | ||
| onChange={handleChange} | ||
| placeholder='jackson' | ||
| className='w-full rounded-md border border-gray-300 px-3 py-2 text-sm focus:border-primary focus:ring-2 focus:ring-primary/30' | ||
| className={inputClass} | ||
| /> | ||
| </div> | ||
|
|
||
|
|
@@ -134,7 +170,7 @@ export default function Login() { | |
| id='domain' | ||
| value={state.domain} | ||
| onChange={handleChange} | ||
| className='w-full rounded-md border border-gray-300 px-3 py-2 text-sm focus:border-primary focus:ring-2 focus:ring-primary/30'> | ||
| className={inputClass}> | ||
| <option value='example.com'>@example.com</option> | ||
| <option value='example.org'>@example.org</option> | ||
| </select> | ||
|
|
@@ -147,11 +183,78 @@ export default function Login() { | |
| type='password' | ||
| autoComplete='off' | ||
| defaultValue='samlstrongpassword' | ||
| className='w-full rounded-md border border-gray-300 px-3 py-2 text-sm focus:border-primary focus:ring-2 focus:ring-primary/30' | ||
| className={inputClass} | ||
| /> | ||
| <p className='mt-1 text-xs text-gray-500'>Any password works</p> | ||
| </div> | ||
|
|
||
| {/* Attributes section */} | ||
| <div className='col-span-2 space-y-2'> | ||
| <label className='block text-sm font-medium text-gray-700'>Attributes</label> | ||
|
|
||
| {attributes.map((attr, i) => ( | ||
| <div key={attr.key} className='flex gap-2 items-center'> | ||
| <input | ||
| type='text' | ||
| value={attr.name} | ||
| onChange={(e) => handleAttrChange(i, 'name', e.target.value)} | ||
| placeholder='name' | ||
| className={`w-2/5 ${inputBase}`} | ||
| /> | ||
| <input | ||
| type='text' | ||
| value={attr.value} | ||
| onChange={(e) => handleAttrChange(i, 'value', e.target.value)} | ||
| placeholder='value' | ||
| className={`flex-1 ${inputBase}`} | ||
| /> | ||
| <button | ||
| type='button' | ||
| onClick={() => handleAttrRemove(i)} | ||
| className='shrink-0 rounded-md border border-gray-300 px-2 py-2 text-sm text-gray-500 hover:bg-gray-100'> | ||
| ✕ | ||
| </button> | ||
| </div> | ||
| ))} | ||
|
|
||
| {/* Add row */} | ||
| <div className='flex gap-2 items-center'> | ||
| <input | ||
| type='text' | ||
| value={newAttr.name} | ||
| onChange={(e) => setNewAttr({ ...newAttr, name: e.target.value })} | ||
| placeholder='name' | ||
| onKeyDown={(e) => { | ||
| if (e.key === 'Enter') { | ||
| e.preventDefault(); | ||
| handleAttrAdd(); | ||
| } | ||
| }} | ||
| className={`w-2/5 ${inputBase}`} | ||
| /> | ||
|
coderabbitai[bot] marked this conversation as resolved.
|
||
| <input | ||
| type='text' | ||
| value={newAttr.value} | ||
| onChange={(e) => setNewAttr({ ...newAttr, value: e.target.value })} | ||
| placeholder='value' | ||
| onKeyDown={(e) => { | ||
| if (e.key === 'Enter') { | ||
| e.preventDefault(); | ||
| handleAttrAdd(); | ||
| } | ||
| }} | ||
| className={`flex-1 ${inputBase}`} | ||
| /> | ||
| <button | ||
| type='button' | ||
| onClick={handleAttrAdd} | ||
| disabled={!newAttr.name} | ||
| className='shrink-0 rounded-md border border-gray-300 px-2 py-2 text-sm text-gray-500 hover:bg-gray-100 disabled:opacity-40'> | ||
| + | ||
| </button> | ||
| </div> | ||
| </div> | ||
|
|
||
| <button | ||
| type='submit' | ||
| className='col-span-2 mt-2 rounded-md bg-primary px-4 py-2 text-sm font-semibold text-white hover:bg-primary-hover focus:outline-none focus:ring-2 focus:ring-primary/40'> | ||
|
|
@@ -174,3 +277,12 @@ export default function Login() { | |
| </> | ||
| ); | ||
| } | ||
|
|
||
| export const getServerSideProps: GetServerSideProps<Props> = async () => { | ||
| const { default: config } = await import('lib/env'); | ||
| const defaultAttributes = Object.entries(config.extraAttributes).map(([name, value]) => ({ | ||
| name, | ||
| value, | ||
| })); | ||
| return { props: { defaultAttributes, defaultAudience: config.audience } }; | ||
| }; | ||
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.
Uh oh!
There was an error while loading. Please reload this page.