Skip to content
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

Use sdk #24

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,26 +12,32 @@
"register": "node ./bin/register.js"
},
"dependencies": {
"@jolocom/sdk": "^1.0.0",
"@jolocom/sdk-storage-typeorm": "^4.0.0",
"axios": "^0.18.1",
"body-parser": "^1.18.3",
"cors": "^2.8.4",
"cred-types-jolocom-core": "^0.0.10",
"cred-types-jolocom-demo": "^0.2.1",
"email-validator": "^2.0.4",
"express": "^4.16.3",
"jolocom-lib": "^4.1.0",
"jolocom-lib": "^5.1.0",
"qrcode": "^1.4.4",
"redis": "^2.8.0",
"socket.io": "^2.2.0",
"sqlite3": "^5.0.0",
"tslib": "^1.9.3",
"tslint": "^5.9.1",
"tslint-config-prettier": "^1.10.0"
"tslint-config-prettier": "^1.10.0",
"typeorm": "^0.2.29"
},
"devDependencies": {
"@babel/core": "^7.3.4",
"@babel/preset-env": "^7.0.0",
"@types/cors": "^2.8.4",
"@types/express": "^4.16.0",
"@types/node": "^11.9.5",
"@types/qrcode": "^1.3.5",
"@types/redis": "^2.8.6",
"@types/socket.io": "^2.1.2",
"prettier": "^1.15.3",
Expand Down
12 changes: 8 additions & 4 deletions src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,17 @@ import { validateEmailCredential } from './helpers/validators'
* The seed to instantiate a vaulted key provider and password for seed encryption / decryption
* The need to persist the seed in clear text will be addressed in the next minor release
*/
export const seed = Buffer.from(
'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa',
'hex'
)
export const seedPhrase =
'sample issue vendor usual train lunar observe cupboard satoshi suspect sight claw'

export const password = 'correct horse battery staple'

export const dbConf = {
type: 'sqlite',
database: './db.sqlite3',
logging: ['error', 'warn', 'schema'],
}

/* Where is your service deployed. E.g. https://demo-sso.jolocom.com, used by the frontend */
export const serviceUrl = process.env.SERVICE_URL || 'http://localhost:9000'

Expand Down
31 changes: 12 additions & 19 deletions src/controllers/authentication.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,23 @@
import { IdentityWallet } from 'jolocom-lib/js/identityWallet/identityWallet'
import { Agent } from '@jolocom/sdk'
import { RedisApi, RequestWithInteractionTokens } from 'src/types'
import { Request, Response } from 'express'
import { password, serviceUrl } from '../config'
import { setStatusPending, setStatusDone } from '../helpers'
import { Authentication } from 'jolocom-lib/js/interactionTokens/authentication'

const generateAuthenticationRequest = (
identityWallet: IdentityWallet,
agent: Agent,
redis: RedisApi
) => async (req: Request, res: Response) => {
const description = req.query.desc || 'Some random action'
const callbackURL = `${serviceUrl}/auth`

try {
const authRequest = await identityWallet.create.interactionTokens.request.auth(
const authRequest = await agent.authRequestToken(
{
callbackURL,
description
},
password
}
)

const token = authRequest.encode()
Expand All @@ -32,25 +31,19 @@ const generateAuthenticationRequest = (
}

const consumeAuthenticationResponse = (
identityWallet: IdentityWallet,
agent: Agent,
redis: RedisApi
) => async (request: RequestWithInteractionTokens, response: Response) => {
const { nonce } = request.serviceRequestToken
const { description: reqDescription } = request.userResponseToken
.interactionToken as Authentication
const { description: resDescription } = request.serviceRequestToken
.interactionToken as Authentication
) => async (request: Request, response: Response) => {
try {
const authInteraction = await agent.processJWT(request.body.token!)

if (reqDescription !== resDescription) {
await setStatusDone(redis, authInteraction.id)
return response.status(200).send()
} catch (err) {
return response
.status(401)
.send('The received description does not match the requested one')
.send(err.toString())
}

// TODO @clauxx check description ??
// to confirm that the user agreed to exactly the action requested
await setStatusDone(redis, nonce)
return response.status(200).send()
}

export const authentication = {
Expand Down
66 changes: 25 additions & 41 deletions src/controllers/issuance.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { credentialOffers, password, serviceUrl } from '../config'
import { credentialOffers, serviceUrl } from '../config'
import { Request, Response } from 'express'
import { IdentityWallet } from 'jolocom-lib/js/identityWallet/identityWallet'
import { RedisApi, RequestWithInteractionTokens } from '../types'
import { keyIdToDid } from 'jolocom-lib/js/utils/helper'
import {
Expand All @@ -12,9 +11,10 @@ import {
import { JSONWebToken } from 'jolocom-lib/js/interactionTokens/JSONWebToken'
import { CredentialOfferResponse } from 'jolocom-lib/js/interactionTokens/credentialOfferResponse'
import { SignedCredential } from 'jolocom-lib/js/credentials/signedCredential/signedCredential'
import { Agent } from '@jolocom/sdk'

const generateCredentialOffer = (
identityWallet: IdentityWallet,
agent: Agent,
redis: RedisApi
) => async (req: Request, res: Response) => {
const queryTypes: string[] = req.query.types.split(',')
Expand All @@ -28,8 +28,7 @@ const generateCredentialOffer = (
return res.status(500).send({ error: 'Credential Type not found' })
}

try {
const credOffer = await identityWallet.create.interactionTokens.request.offer(
const credOffer = await agent.credOfferToken(
{
callbackURL,
offeredCredentials: queryTypes.reduce(
Expand All @@ -42,49 +41,37 @@ const generateCredentialOffer = (
],
[]
)
},
password
}
)

const token = credOffer.encode()
await setStatusPending(redis, credOffer.nonce, { request: token })
return res.send({ token, identifier: credOffer.nonce })
} catch (err) {
return res.status(500).send({ error: err.message })
}
const token = credOffer.encode()

await setStatusPending(redis, credOffer.nonce, { request: token })
return res.send({ token, identifier: credOffer.nonce })
}

const consumeCredentialOfferResponse = (
identityWallet: IdentityWallet,
agent: Agent,
redis: RedisApi
) => async (req: RequestWithInteractionTokens, res: Response) => {
const credentialOfferResponse = req.userResponseToken as JSONWebToken<
CredentialOfferResponse
>
const selectedOffers =
credentialOfferResponse.interactionToken.selectedCredentials
const claim = await getDataFromUiForms(redis, credentialOfferResponse.nonce)
const offerInteraction = await agent.processJWT(req.body.token!)

// @ts-ignore
const selectedTypes = offerInteraction.getSummary().state.selectedTypes! as string[]
const claim = await getDataFromUiForms(redis, offerInteraction.id)

const selectedTypes = selectedOffers.map(offer => offer.type)
if (!areTypesAvailable(selectedTypes, credentialOffers)) {
return res.status(500).send({ error: 'Credential Type not found' })
}

const providedCredentials = await Promise.all(
selectedOffers.reduce<Array<Promise<SignedCredential>>>((acc, offer) => {
return [
...acc,
identityWallet.create.signedCredential(
{
metadata: credentialOffers[offer.type].schema,

selectedTypes.map((typ: string) =>
agent.signedCredential({
metadata: credentialOffers[typ].schema,
claim: { ...claim, message: 'Thank you for testing the endpoint' },
subject: keyIdToDid(credentialOfferResponse.issuer)
},
password
)
]
}, [])
subject: offerInteraction.participants.responder.did
})
)
)

const invalidTypes: string[] | undefined =
Expand All @@ -98,15 +85,12 @@ const consumeCredentialOfferResponse = (
})
}

const credentialReceive = await identityWallet.create.interactionTokens.response.issue(
{
signedCredentials: providedCredentials.map(cred => cred.toJSON())
},
password,
credentialOfferResponse

const credentialReceive = await offerInteraction.createCredentialReceiveToken(
providedCredentials
)

await setStatusDone(redis, credentialOfferResponse.nonce)
await setStatusDone(redis, offerInteraction.id)
return res.json({ token: credentialReceive.encode() })
}

Expand Down
40 changes: 21 additions & 19 deletions src/controllers/registration.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
import { Response, Request } from 'express'
import { IdentityWallet } from 'jolocom-lib/js/identityWallet/identityWallet'
import { Agent } from '@jolocom/sdk'

import { RedisApi, RequestWithInteractionTokens } from '../types'
import { credentialRequirements, password, serviceUrl } from '../config'
import {
credentialRequirements,
serviceUrl
} from '../config'
import {
extractDataFromClaims,
generateRequirementsFromConfig,
Expand All @@ -15,7 +18,7 @@ import { Endpoints } from '../sockets'
import { applyValidationFunction } from '../helpers/validators'

const generateCredentialShareRequest = (
identityWallet: IdentityWallet,
agent: Agent,
redis: RedisApi
) => async (req: Request, res: Response) => {
// NOTE Credential requirement types provided by the frontend
Expand All @@ -28,39 +31,38 @@ const generateCredentialShareRequest = (
return res.status(500).send({ error: 'Credential Type not found' })
}

const credentialRequest = await identityWallet.create.interactionTokens.request.share(
const credentialRequest = await agent.credRequestToken(
{
callbackURL,
credentialRequirements: queryTypes.reduce(
(acc, credentialType) =>
credentialRequirements[credentialType]
? [
...acc,
generateRequirementsFromConfig(
credentialRequirements[credentialType]
)
]
...acc,
generateRequirementsFromConfig(
credentialRequirements[credentialType]
)
]
: acc,
[]
)
},
password
}
)

const token = credentialRequest.encode()
await setStatusPending(redis, credentialRequest.nonce, { request: token })
return res.send({ token, identifier: credentialRequest.nonce })
}

const consumeCredentialShareResponse = (redis: RedisApi) => async (
const consumeCredentialShareResponse = (agent: Agent, redis: RedisApi) => async (
req: RequestWithInteractionTokens,
res: Response
) => {
const response = req.userResponseToken.interactionToken as CredentialResponse
const { issuer, nonce } = req.serviceRequestToken

try {
const passesValidation = response.suppliedCredentials.every(
const verificationInteraction = await agent.processJWT(req.body.token!)

// @ts-ignore
const passesValidation = verificationInteraction.getSummary().state.providedCredentials.every(
applyValidationFunction
)

Expand All @@ -71,12 +73,12 @@ const consumeCredentialShareResponse = (redis: RedisApi) => async (
}

const data = {
...extractDataFromClaims(response),
...extractDataFromClaims(verificationInteraction.lastMessage.interactionToken as CredentialResponse),
...req.middlewareData,
did: issuer
did: req.userResponseToken.issuer
}

await setStatusDone(redis, nonce, data)
await setStatusDone(redis, verificationInteraction.id, data)
return res.status(200).send()
} catch (err) {
console.log(err)
Expand Down
9 changes: 5 additions & 4 deletions src/customHandlers/customMiddleware.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { RequestWithInteractionTokens, RedisApi } from 'src/types'
import { Response, NextFunction } from 'express'
import { RedisApi } from 'src/types'
import { Request, Response, NextFunction } from 'express'
import { Agent } from '@jolocom/sdk'

export const addCustomAuthnMiddleware = (redis: RedisApi) => async (
req: RequestWithInteractionTokens,
export const addCustomAuthnMiddleware = (agent: Agent, redis: RedisApi) => async (
req: Request,
res: Response,
next: NextFunction
) => {
Expand Down
Loading