-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: support openid invitations and deeplinking improvements (#97)
Signed-off-by: Timo Glastra <timo@animo.id>
- Loading branch information
1 parent
58989b4
commit 347d7b0
Showing
27 changed files
with
666 additions
and
283 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
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
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.