Skip to content

Commit

Permalink
Splitting up route handlers to controllers
Browse files Browse the repository at this point in the history
  • Loading branch information
Exulansis committed Dec 11, 2018
1 parent b563b2d commit a0ee0be
Show file tree
Hide file tree
Showing 9 changed files with 132 additions and 10 deletions.
9 changes: 3 additions & 6 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,11 @@
# production
/build

# editors
.idea

# misc
.DS_Store
.env.local
.env.development.local
.env.test.local
.env.production.local
dump.rdb

npm-debug.log*
yarn-debug.log*
yarn-error.log*
2 changes: 2 additions & 0 deletions config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,5 @@ export const credentialRequirements = [
constraints: [] as IConstraint[]
}
]

export const credentialOffer = [{}]
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
"@types/redis": "^2.8.6",
"@types/socket.io": "^1.4.38",
"@types/socket.io-client": "^1.4.32",
"prettier": "^1.15.3",
"typescript": "^3.0.1"
},
"jest": {
Expand Down
Empty file added src/app.ts
Empty file.
39 changes: 39 additions & 0 deletions src/controllers/issuance.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import {password} from '../../config'
import {ControllerArguments} from './registration'
import {Response, Request} from 'express'

const generateCredentialOffer = async (
options: ControllerArguments,
req: Request,
res: Response
) => {
const {identityWallet} = options

try {
const credOffer = await identityWallet.create.interactionTokens.request.offer(
{
callbackURL: '/receive/',
requestedInput: {},
instant: true
},
password
)

return res.status(200).send({token: credOffer.encode()})
} catch (err) {
return res.status(500).send({error: err.message})
}
}

const consumeCredentialOfferResponse = async (
options: ControllerArguments,
req: Request,
res: Response
) => {
console.log('YES')
}

export const issuance = {
generateCredentialOffer,
consumeCredentialOfferResponse
}
79 changes: 79 additions & 0 deletions src/controllers/registration.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
import {credentialRequirements, password} from '../../config'
import {Express, Request, Response} from 'express'
import {extractDataFromClaims} from '../helpers/'
import {IdentityWallet} from 'jolocom-lib/js/identityWallet/identityWallet'
import {JolocomLib} from 'jolocom-lib'

export interface ControllerArguments {
app: Express;
identityWallet: IdentityWallet;
password: string;
}

const generateCredentialShareRequest = async (
options: ControllerArguments,
req: Request,
res: Response
) => {
const {identityWallet} = options
const callbackURL = '/authentication'

const credentialRequest = await identityWallet.create.interactionTokens.request.share(
{
callbackURL,
credentialRequirements
}, password
)

res.send({token: credentialRequest.encode()})
}

const consumeCredentialShareResponse = async (
options: ControllerArguments,
req: Request,
res: Response
) => {
const {token} = req.body
const {identityWallet} = options

try {
const credentialResponse = await JolocomLib.parse.interactionToken.fromJWT(token)

if (!(await JolocomLib.util.validateDigestable(credentialResponse))) {
return res.status(401).send('Invalid signature on interaction token')
}

// TODO include | parse this from request
const credentialRequest = await identityWallet.create.interactionTokens.request.share(
{
callbackURL: '/authenticate',
credentialRequirements
}, password
)

if (!credentialResponse.interactionToken.satisfiesRequest(credentialRequest.interactionToken)) {
return res.status(401).send('The supplied credentials do not match the types of the requested credentials')
}

const userData = {
...extractDataFromClaims(credentialResponse.interactionToken),
did: credentialResponse.issuer,
status: 'success'
}
console.log(userData)

// await setAsync(
// identifier,
// JSON.stringify({status: 'success', data: userData})
// )

return res.status(200)
} catch (err) {
return res.status(401).send(err.message)
}
}

export const registration = {
generateCredentialShareRequest,
consumeCredentialShareResponse
}
File renamed without changes.
Empty file added src/index.ts
Empty file.
12 changes: 8 additions & 4 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -824,13 +824,13 @@ big.js@^3.1.3:
version "3.2.0"
resolved "https://registry.yarnpkg.com/big.js/-/big.js-3.2.0.tgz#a5fc298b81b9e0dca2e458824784b65c52ba588e"

"bignumber.js@git+https://github.com/debris/bignumber.js.git#94d7146671b9719e00a09c29b01a691bc85048c2":
"bignumber.js@git+https://github.com/debris/bignumber.js#master":
version "2.0.7"
resolved "git+https://github.com/debris/bignumber.js.git#94d7146671b9719e00a09c29b01a691bc85048c2"
resolved "git+https://github.com/debris/bignumber.js#c7a38de919ed75e6fb6ba38051986e294b328df9"

"bignumber.js@git+https://github.com/debris/bignumber.js.git#master":
"bignumber.js@git+https://github.com/debris/bignumber.js.git#94d7146671b9719e00a09c29b01a691bc85048c2":
version "2.0.7"
resolved "git+https://github.com/debris/bignumber.js.git#c7a38de919ed75e6fb6ba38051986e294b328df9"
resolved "git+https://github.com/debris/bignumber.js.git#94d7146671b9719e00a09c29b01a691bc85048c2"

bindings@^1.2.1, bindings@^1.3.0:
version "1.3.1"
Expand Down Expand Up @@ -3368,6 +3368,10 @@ prepend-http@^1.0.1:
version "1.0.4"
resolved "https://registry.yarnpkg.com/prepend-http/-/prepend-http-1.0.4.tgz#d4f4562b0ce3696e41ac52d0e002e57a635dc6dc"

prettier@^1.15.3:
version "1.15.3"
resolved "https://registry.yarnpkg.com/prettier/-/prettier-1.15.3.tgz#1feaac5bdd181237b54dbe65d874e02a1472786a"

private@^0.1.6:
version "0.1.8"
resolved "https://registry.yarnpkg.com/private/-/private-0.1.8.tgz#2381edb3689f7a53d653190060fcf822d2f368ff"
Expand Down

0 comments on commit a0ee0be

Please sign in to comment.