-
Notifications
You must be signed in to change notification settings - Fork 12
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
feat: support openid invitations and deeplinking improvements #97
Merged
Merged
Changes from 3 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
3601755
feat: support openid invitations and deeplinking improvements
TimoGlastra 3023258
improve didcomm invitation handling
TimoGlastra 48f5985
openid4vp
TimoGlastra fcaf727
chore: address feedback
TimoGlastra 76e5b4c
chore: update version to 1.3.0
TimoGlastra 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
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,16 @@ | ||
import { usePathname, useGlobalSearchParams } from 'expo-router' | ||
|
||
// NOTE: for all unmatched routes we render null, as it's good chance that | ||
// we got here due to deep-linking, and we already handle that somewhere else | ||
export default () => { | ||
const pathname = usePathname() | ||
const searchParams = useGlobalSearchParams() | ||
|
||
// eslint-disable-next-line no-console | ||
console.warn( | ||
'Landed on unmatched route (probably due to deeplinking in which case this is not an error)', | ||
{ pathname, searchParams } | ||
) | ||
|
||
return null | ||
} |
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 was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import { DidCommNotificationScreen } from 'app/features/notifications' | ||
|
||
export default function Screen() { | ||
return ( | ||
<> | ||
<DidCommNotificationScreen /> | ||
</> | ||
) | ||
} |
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,30 +1,65 @@ | ||
import type { ReactNode } from 'react' | ||
|
||
import { QrTypes } from '@internal/agent' | ||
import { InvitationQrTypes } from '@internal/agent' | ||
import { useToastController } from '@internal/ui' | ||
import { CommonActions } from '@react-navigation/native' | ||
import { useCredentialDataHandler } from 'app/hooks/useCredentialDataHandler' | ||
import * as Linking from 'expo-linking' | ||
import { useNavigation } from 'expo-router' | ||
import { useEffect, useState } from 'react' | ||
|
||
interface DeeplinkHandlerProps { | ||
children: ReactNode | ||
} | ||
|
||
const deeplinkSchemes = Object.values(QrTypes) | ||
const deeplinkSchemes = Object.values(InvitationQrTypes) | ||
|
||
export const DeeplinkHandler = ({ children }: DeeplinkHandlerProps) => { | ||
const url = Linking.useURL() | ||
const [lastDeeplink, setLastDeeplink] = useState<string | null>(null) | ||
const { handleCredentialData } = useCredentialDataHandler() | ||
const toast = useToastController() | ||
const navigation = useNavigation() | ||
|
||
useEffect(() => { | ||
if (!url || url === lastDeeplink) return | ||
// TODO: I'm not sure if we need this? Or whether an useEffect without any deps is enough? | ||
const [hasHandledInitialUrl, setHasHandledInitialUrl] = useState(false) | ||
|
||
function handleUrl(url: string) { | ||
const isRecognizedDeeplink = deeplinkSchemes.some((scheme) => url.startsWith(scheme)) | ||
|
||
// Whenever a deeplink comes in, we reset the state. This is due to expo | ||
// routing us always and we can't intercept that. It seems they are working on | ||
// more control, but for now this is the cleanest approach | ||
navigation.dispatch( | ||
CommonActions.reset({ | ||
routes: [{ key: 'index', name: 'index' }], | ||
}) | ||
) | ||
|
||
// Ignore deeplinks that don't start with the schemes for credentials | ||
if (!deeplinkSchemes.some((scheme) => url.startsWith(scheme))) return | ||
if (isRecognizedDeeplink) { | ||
void handleCredentialData(url).then((result) => { | ||
if (!result.success) { | ||
toast.show(result.error) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
setLastDeeplink(url) | ||
void handleCredentialData(url) | ||
}, [url]) | ||
// NOTE: we use getInitialURL and the event listener over useURL as we don't know | ||
// using that method whether the same url is opened multiple times. As we need to make | ||
// sure to handle ALL incoming deeplinks (to prevent default expo-router behaviour) we | ||
// handle them ourselves. On startup getInitialUrl will be called once. | ||
useEffect(() => { | ||
if (hasHandledInitialUrl) return | ||
void Linking.getInitialURL().then((url) => { | ||
if (url) handleUrl(url) | ||
setHasHandledInitialUrl(true) | ||
}) | ||
}, [hasHandledInitialUrl]) | ||
|
||
useEffect(() => { | ||
const eventListener = Linking.addEventListener('url', (event) => handleUrl(event.url)) | ||
return () => eventListener.remove() | ||
}, []) | ||
|
||
return <>{children}</> | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
import type { ParseInvitationResult } from './parsers' | ||
|
||
const errorResponse = (message: string) => { | ||
return { | ||
success: false, | ||
error: message, | ||
} as const | ||
} | ||
|
||
export async function fetchInvitationDataUrl(dataUrl: string): Promise<ParseInvitationResult> { | ||
// If we haven't had a response after 10 seconds, we will handle as if the invitation is not valid. | ||
const abortController = new AbortController() | ||
const timeout = setTimeout(() => abortController.abort('timeout reached'), 10000) | ||
|
||
try { | ||
// If we still don't know what type of invitation it is, we assume it is a URL that we need to fetch to retrieve the invitation. | ||
const response = await fetch(dataUrl, { | ||
headers: { | ||
// for DIDComm out of band invitations we should include application/json | ||
// but we are flexible and also want to support other types of invitations | ||
// as e.g. the OpenID SIOP request is a signed encoded JWT string | ||
Accept: 'application/json, text/plain, */*', | ||
}, | ||
}) | ||
clearTimeout(timeout) | ||
if (!response.ok) { | ||
return errorResponse('Unable to retrieve invitation.') | ||
} | ||
|
||
const contentType = response.headers.get('content-type') | ||
if (contentType?.includes('application/json')) { | ||
const json: unknown = await response.json() | ||
return handleJsonResponse(json) | ||
} else { | ||
const text = await response.text() | ||
return handleTextResponse(text) | ||
} | ||
} catch (error) { | ||
clearTimeout(timeout) | ||
return errorResponse('Unable to retrieve invitation.') | ||
} | ||
} | ||
|
||
function handleJsonResponse(json: unknown): ParseInvitationResult { | ||
// We expect a JSON object | ||
if (!json || typeof json !== 'object' || Array.isArray(json)) { | ||
return errorResponse('Invitation not recognized.') | ||
} | ||
|
||
if ('@type' in json) { | ||
return { | ||
success: true, | ||
result: { | ||
format: 'parsed', | ||
type: 'didcomm', | ||
data: json, | ||
}, | ||
} | ||
} | ||
|
||
if ('credential_issuer' in json) { | ||
return { | ||
success: true, | ||
result: { | ||
format: 'parsed', | ||
type: 'openid-credential-offer', | ||
data: json, | ||
}, | ||
} | ||
} | ||
|
||
return errorResponse('Invitation not recognized.') | ||
} | ||
|
||
function handleTextResponse(text: string): ParseInvitationResult { | ||
// If the text starts with 'ey' we assume it's a JWT and thus an OpenID authorization request | ||
if (text.startsWith('ey')) { | ||
return { | ||
success: true, | ||
result: { | ||
format: 'parsed', | ||
type: 'openid-authorization-request', | ||
data: text, | ||
}, | ||
} | ||
} | ||
|
||
// Otherwise we still try to parse it as JSON | ||
try { | ||
const json: unknown = JSON.parse(text) | ||
return handleJsonResponse(json) | ||
|
||
// handel like above | ||
} catch (error) { | ||
return errorResponse('Invitation not recognized.') | ||
} | ||
} |
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.
Quite a nicer workaround 👍