Skip to content

Commit

Permalink
Update Authorization and Credential Offer
Browse files Browse the repository at this point in the history
 * Send updates over websocket from backend at the callback
 * Update UI from websocket interactions based on user interaction
Works with Jolocom Smart Wallet for example.
  • Loading branch information
maltabba authored and Muhammad-Altabba committed Aug 11, 2021
1 parent 180861d commit 95b2a51
Show file tree
Hide file tree
Showing 3 changed files with 114 additions and 11 deletions.
23 changes: 23 additions & 0 deletions frontend/src/flows/Authentication/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,28 @@ export const Authentication: React.FC<IAuthenticationProps> = ({
serviceAPI,
}) => {
const [description, setDescription] = useState<string>('Lorem ipsum')
const [msg, setMsg] = useState<string | undefined>()

const startAuth = async () => {
const resp: {
qr: string
err: string
id: string | undefined
} = await serviceAPI.sendRPC(RpcRoutes.authnInterxn, { description })
console.log('resp', resp)
if (resp.id) {
receiveUpdate(resp)
}
return resp
}

const receiveUpdate = async (resp: any) => {
const processedRes = await resp.originalMsg.followUps[1].processed
const responderDid = processedRes.participants.responder.didDocument.id
setMsg(responderDid)
console.log('processedRes', processedRes)
}

return (
<InteractionTemplate
startText="Start Authentication Interaction"
Expand All @@ -31,6 +44,16 @@ export const Authentication: React.FC<IAuthenticationProps> = ({
value={description}
setValue={setDescription}
/>
<p>
{msg && (
<>
<br />
<b>Result: </b>
The user successfully authenticate using his DID:
<b> {msg}</b>
</>
)}
</p>
</InteractionTemplate>
)
}
64 changes: 56 additions & 8 deletions frontend/src/flows/CredentialOffer/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ export const CredentialOffer: React.FC<ICredentialOfferProps> = ({
const [issuedCredentials, setIssued] = useState<Array<string>>([])
const [invalidCredentials, setInvalid] = useState<Array<string>>([])
const availableIssueCredentials = credTypes
const [msg, setMsg] = useState<string | undefined>()
const [err, setErr] = useState<string | undefined>()

const handleSelect = (array: string[], item: string) => {
return !array.includes(item)
Expand All @@ -27,20 +29,48 @@ export const CredentialOffer: React.FC<ICredentialOfferProps> = ({
if (issuedCredentials.length === 0 && credTypes && credTypes.length > 0) {
setIssued(credTypes.slice(0, 1))
}
})
}, [issuedCredentials.length, credTypes])

const startCredOffer = async () => {
const resp: { qr: string; err: string } = await serviceAPI.sendRPC(
RpcRoutes.offerCred,
{
types: Array.from(new Set(issuedCredentials)),
invalid: Array.from(new Set(invalidCredentials)),
},
)
const resp: {
qr: string
err: string
id: string
} = await serviceAPI.sendRPC(RpcRoutes.offerCred, {
types: Array.from(new Set(issuedCredentials)),
invalid: Array.from(new Set(invalidCredentials)),
})
console.log(resp)
if (resp.id) {
receiveUpdate(resp)
}

return resp
}

const receiveUpdate = async (resp: any) => {
const processedRes = await resp.originalMsg.followUps[1].processed

const successfulCredentials = processedRes.credentials
.filter((c: any) => c.claim.id !== 'INVALID')
.map((c: any) => c.name)
.join(', ')
const failedCredentials = processedRes.credentials
.filter((c: any) => c.claim.id === 'INVALID')
.map((c: any) => c.name)
.join(', ')

setMsg(`${successfulCredentials}`)
if (failedCredentials.length > 0) setErr(`${failedCredentials}`)

receiveFinal(resp)
}

const receiveFinal = async (resp: any) => {
const processed = await resp.originalMsg.followUps[2].processed
console.log('The final update has been recived from server', processed)
}

return (
<InteractionTemplate
startText="Start Credential Offer"
Expand All @@ -59,6 +89,24 @@ export const CredentialOffer: React.FC<ICredentialOfferProps> = ({
onSelect={type => setInvalid(handleSelect(invalidCredentials, type))}
selectedItems={invalidCredentials}
/>
<p>
{msg && (
<>
<b>Result: </b>
The user successfully got the Credentials:
<b> {msg}</b>
</>
)}
</p>
<p>
{err && (
<>
<b>Error: </b>
The user failed to get the Credentials:
<b> {err}</b>
</>
)}
</p>
</InteractionTemplate>
)
}
38 changes: 35 additions & 3 deletions service_agent/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -213,9 +213,20 @@ export const init = async () => {
{ createInteractionCallbackURL, wrapJWT },
) => {
const callbackURL = createInteractionCallbackURL(
async (jwt: string) => {
async (jwt: string, websocket: WebSocket) => {
const interxn = await jolo.processJWT(jwt)
console.log('auth request handled for', interxn.participants)

websocket.send(
JSON.stringify({
id: interxn.id,
status: 'update',
response: {
id: interxn.id,
participants: interxn.participants,
},
}),
)
},
)
return wrapJWT(
Expand Down Expand Up @@ -288,7 +299,7 @@ export const init = async () => {
const invalidTypes = req.invalid || []

const callbackURL = createInteractionCallbackURL(
async (jwt: string) => {
async (jwt: string, websocket: WebSocket) => {
const interxn = await jolo.processJWT(jwt)
console.log('offerCred called back for', interxn.id)

Expand Down Expand Up @@ -322,6 +333,16 @@ export const init = async () => {
'credentials issued',
credentials.map((c) => last(c.type)),
)
websocket.send(
JSON.stringify({
id: interxn.id,
status: 'update',
response: {
id: interxn.id,
credentials: credentials,
},
}),
)
return interxn.createCredentialReceiveToken(credentials)
},
)
Expand Down Expand Up @@ -349,9 +370,20 @@ export const init = async () => {
)

const callbackURL = createInteractionCallbackURL(
async (jwt: string) => {
async (jwt: string, websocket: WebSocket) => {
const interxn = await jolo.processJWT(jwt)
console.log('credShareRequest called back for', interxn.id)

websocket.send(
JSON.stringify({
id: interxn.id,
status: 'update',
response: {
id: interxn.id,
state: interxn.flow.state,
},
}),
)
},
)

Expand Down

0 comments on commit 95b2a51

Please sign in to comment.