-
Notifications
You must be signed in to change notification settings - Fork 1
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
Sebankid #2
Merged
Merged
Sebankid #2
Changes from 4 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
214d471
Initial SEBankID code
mickhansen bf27f9f
implement polling and response handling
mickhansen 7b45be4
use redirect on iOS
mickhansen c9c9d4b
encodeURIComponent on redirect
mickhansen 8e3d4d3
temporarily disable refetch loop
mickhansen e2afbaf
seperate desktop/android/ios code
mickhansen 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 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
45 changes: 45 additions & 0 deletions
45
src/components/AuthMethodButton/AuthMethodButton.stories.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,45 @@ | ||
import React from 'react'; | ||
import { ComponentStory, ComponentMeta } from '@storybook/react'; | ||
|
||
import AuthMethodButton from './AuthMethodButton'; | ||
import CriiptoVerifyProvider from '../../provider'; | ||
import { acrValueToTitle } from '../../utils'; | ||
import StoryResponseRenderer from '../../stories/StoryResponseRenderer'; | ||
|
||
export default { | ||
title: 'Components/AuthMethodButton', | ||
argTypes: { | ||
completionStrategy: { | ||
name: 'Completion strategy', | ||
control: 'select', | ||
defaultValue: 'client', | ||
options: ['client', 'openidprovider'] | ||
}, | ||
response: { | ||
name: 'Response mode', | ||
control: 'select', | ||
defaultValue: 'token', | ||
options: ['token', 'code'] | ||
} | ||
} | ||
} as ComponentMeta<typeof AuthMethodButton>; | ||
|
||
// More on component templates: https://storybook.js.org/docs/react/writing-stories/introduction#using-args | ||
const Template: ComponentStory<typeof AuthMethodButton> = (args, {globals}) => { | ||
return ( | ||
<CriiptoVerifyProvider completionStrategy={(args as any).completionStrategy} response={(args as any).response} domain={globals.domain} clientID={globals.clientID} redirectUri="https://httpbin.org/get"> | ||
<StoryResponseRenderer> | ||
<AuthMethodButton {...args}> | ||
{acrValueToTitle('en', args.acrValue).title}<br /> | ||
{acrValueToTitle('en', args.acrValue).subtitle} | ||
</AuthMethodButton> | ||
</StoryResponseRenderer> | ||
</CriiptoVerifyProvider> | ||
); | ||
}; | ||
|
||
export const SEBankIDSameDeviceButton = Template.bind({}); | ||
SEBankIDSameDeviceButton.args = { | ||
acrValue: 'urn:grn:authn:se:bankid:same-device' | ||
}; | ||
SEBankIDSameDeviceButton.storyName = "se:bankid:same-device"; |
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
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
159 changes: 159 additions & 0 deletions
159
src/components/SEBankIDSameDeviceButton/SEBankIDSameDeviceButton.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,159 @@ | ||
import { PKCE, AuthorizeResponse } from '@criipto/auth-js'; | ||
import React, {useCallback, useContext, useEffect, useState} from 'react'; | ||
import CriiptoVerifyContext from '../../context'; | ||
import { getMobileOS } from '../../device'; | ||
import usePageVisibility from '../../hooks/usePageVisibility'; | ||
|
||
interface Props { | ||
className: string | ||
children: React.ReactNode | ||
href?: string | ||
redirectUri?: string | ||
} | ||
|
||
interface Links { | ||
cancelUrl: string | ||
completeUrl: string | ||
pollUrl: string | ||
launchLinks: { | ||
customFileHandlerUrl: string | ||
universalLink: string | ||
} | ||
} | ||
|
||
const mobileOS = getMobileOS(); | ||
|
||
function searchParamsToPOJO(input: URLSearchParams) { | ||
return Array.from(input.keys()).reduce((memo : {[key: string]: string}, key) => { | ||
memo[key] = input.get(key)!; | ||
return memo; | ||
}, {}); | ||
} | ||
|
||
export default function SEBankIDSameDeviceButton(props: Props) { | ||
const [href, setHref] = useState(props.href); | ||
const [links, setLinks] = useState<Links | null>(null); | ||
const [pkce, setPKCE] = useState<PKCE | undefined>(undefined); | ||
const [error, setError] = useState<string | null>(null); | ||
const [initiated, setInitiated] = useState(false); | ||
const {buildAuthorizeUrl, completionStrategy, generatePKCE, domain, handleResponse} = useContext(CriiptoVerifyContext); | ||
const {redirectUri} = props; | ||
|
||
const reset = () => { | ||
setPKCE(undefined); | ||
setLinks(null); | ||
setHref(props.href); | ||
}; | ||
|
||
const handleComplete = useCallback(async (completeUrl: string) => { | ||
const required = {pkce}; | ||
reset(); | ||
|
||
const completeResponse = await fetch(completeUrl); | ||
if (completeResponse.status >= 400) { | ||
setError(await completeResponse.text()); | ||
return; | ||
} | ||
|
||
const {location} : {location: string} = await completeResponse.json(); | ||
if (completionStrategy === 'openidprovider') { | ||
window.location.href = location; | ||
return; | ||
} | ||
const url = new URL(location); | ||
const params = searchParamsToPOJO(url.searchParams) as AuthorizeResponse; | ||
|
||
await handleResponse(params, { | ||
pkce: required.pkce, | ||
redirectUri: redirectUri | ||
}) | ||
}, [completionStrategy, pkce]); | ||
|
||
// Mobile visibility scenario | ||
usePageVisibility(async () => { | ||
if (!links || !mobileOS) return; | ||
|
||
handleComplete(links.completeUrl); | ||
}, [links]); | ||
|
||
// Desktop polling scenario | ||
useEffect(() => { | ||
if (!links) return; | ||
if (mobileOS) return; | ||
if (!initiated) return; | ||
|
||
let timeout : string | undefined; | ||
const poll = async () => { | ||
const response = await fetch(links.pollUrl); | ||
|
||
if (response.status === 202) { | ||
setTimeout(poll, 1000); | ||
return; | ||
} else if (response.status >= 400) { | ||
const error = await response.text(); | ||
setError(error); | ||
return; | ||
} else { | ||
const {targetUrl} = await response.json(); | ||
await handleComplete(`https://${domain}${targetUrl}`); | ||
return; | ||
} | ||
}; | ||
|
||
setTimeout(poll, 1000); | ||
return () => { | ||
if (timeout) clearTimeout(timeout); | ||
}; | ||
}, [links, initiated]); | ||
|
||
const refresh = useCallback(async () => { | ||
console.log('SEBankID: Refresh authorize url'); | ||
const pkce = await generatePKCE(); | ||
|
||
buildAuthorizeUrl({ | ||
acrValues: 'urn:grn:authn:se:bankid:same-device', | ||
loginHint: 'appswitch:browser', | ||
responseMode: 'json', | ||
pkce, | ||
redirectUri | ||
}).then(url => { | ||
return fetch(url).then(response => response.json() as Promise<Links>); | ||
}) | ||
.then(links => { | ||
setPKCE(pkce || undefined); | ||
setLinks(links); | ||
const redirect = mobileOS === 'ios' ? encodeURIComponent(window.location.href) : 'null'; | ||
setHref(`${mobileOS ? links.launchLinks.universalLink : links.launchLinks.customFileHandlerUrl}&redirect=${redirect}`); | ||
}) | ||
.catch(console.error); | ||
}, [buildAuthorizeUrl, redirectUri]); | ||
|
||
// Generate URL on first button render | ||
useEffect(() => { | ||
refresh(); | ||
}, [refresh]); | ||
|
||
// Continously fetch new autostart token if UI is open for a long time | ||
useEffect(() => { | ||
const interval = setInterval(() => { | ||
if (initiated) return; | ||
refresh(); | ||
}, 25000); | ||
return () => clearInterval(interval); | ||
}, [refresh, initiated]); | ||
|
||
// Track when the button is clicked to stop refreshing URL | ||
const handleClick = () => { | ||
setInitiated(true); | ||
setError(null); | ||
} | ||
|
||
return ( | ||
<React.Fragment> | ||
<a className={`criipto-verify-button ${props.className}`} href={href} onClick={handleClick}> | ||
{props.children} | ||
</a> | ||
{error && <p>{error}</p>} | ||
</React.Fragment> | ||
) | ||
} |
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,3 @@ | ||
{ | ||
"main": "./SEBankIDSameDeviceButton.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
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,21 @@ | ||
function testUA(pattern: RegExp) { | ||
return pattern.test(navigator.userAgent); | ||
}; | ||
export function isiOS() { | ||
return testUA(/iPad|iPhone|iPod/) && !(window as any).MSStream; | ||
}; | ||
|
||
export function isiOSSafari() { | ||
return isiOS() && !testUA(/ CriOS\/[.0-9]*/); | ||
}; | ||
|
||
export function isAndroid() { return testUA(/Android/); }; | ||
|
||
export type MobileOS = 'ios' | 'android'; | ||
|
||
export function getMobileOS() : MobileOS | null { | ||
if (isiOS()) return 'ios'; | ||
if (isAndroid()) return 'android'; | ||
|
||
return null; | ||
} |
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.
Could this potentially trigger while the BankID app is in foreground on iOS?
Would explain the error message seen.
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.
@sgryt Hmm maybe if the onclick handler doesnt fire on iOS actually, could be it!
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.
@sgryt Latest commit disables the refetch.
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.
Did not change the observed behavior, I’m afraid.
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.
But could it be the other way around - in the sense that the displayed response is from before the flow is completed in the BankID app ?
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.
IIRC, the reason we had to not poll in the HTML UI was because iOS lets JS continue to run when Safari is backgrounded.
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.
When does
setInterval
run the first invocation of the specified function ?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.
@sgryt We don't poll per say in the device flows, the intent is simply to refresh links if you're idle on the page so that the order doesn't expire.
We also automatically stop the refreshing as soon as the user lcicks on the button.
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.
setInterval
runs on the tail-end, so doesn't execute untill the first interval has passed.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.
@sgryt Lets debug live with ngrok and slack-huddle tomorrow, will be easier.