Skip to content

Commit

Permalink
feat: Multiple audience dids
Browse files Browse the repository at this point in the history
BREAKING CHANGE: DB Schema change
  • Loading branch information
simonas-notcat committed Apr 30, 2020
1 parent 9e54a45 commit eaa1a40
Show file tree
Hide file tree
Showing 8 changed files with 56 additions and 35 deletions.
20 changes: 10 additions & 10 deletions packages/daf-cli/src/credential.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ program
type: 'input',
name: 'sub',
message: 'Subject DID',
default: identities[0].did
default: identities[0].did,
},
{
type: 'input',
Expand Down Expand Up @@ -62,7 +62,7 @@ program
issuer: answers.iss,
'@context': ['https://www.w3.org/2018/credentials/v1'],
type: ['VerifiableCredential'],
credentialSubject
credentialSubject,
}

if (answers.addStatus) {
Expand All @@ -83,14 +83,14 @@ program

data['credentialStatus'] = {
type: statusAnswers.type,
id: statusAnswers.id
id: statusAnswers.id,
}
}

const signAction: W3c.ActionSignW3cVc = {
type: W3c.ActionTypes.signCredentialJwt,
save: true,
data
data,
}

const credential: Daf.Credential = await (await agent).handleAction(signAction)
Expand Down Expand Up @@ -141,7 +141,7 @@ program
},
]
for (const id of ids) {
const name = await id.getLatestClaimValue((await agent).dbConnection, { type: 'name'})
const name = await id.getLatestClaimValue((await agent).dbConnection, { type: 'name' })
identities.push({
value: id.did,
name: `${id.did} - ${name}`,
Expand Down Expand Up @@ -182,9 +182,9 @@ program
aud = answers.aud
}

const credentials = await Daf.Credential.find({
where: { subject: answers.iss},
relations: ['claims']
const credentials = await Daf.Credential.find({
where: { subject: answers.iss },
relations: ['claims'],
})
const list: any = []
if (credentials.length > 0) {
Expand Down Expand Up @@ -230,11 +230,11 @@ program
save: true,
data: {
issuer: answers.iss,
audience: aud,
audience: [aud],
tag: answers.tag,
'@context': ['https://www.w3.org/2018/credentials/v1'],
type: ['VerifiablePresentation'],
verifiableCredential
verifiableCredential,
},
}

Expand Down
3 changes: 2 additions & 1 deletion packages/daf-core/src/entities/identity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
PrimaryColumn,
BaseEntity,
OneToMany,
ManyToMany,
CreateDateColumn,
UpdateDateColumn,
} from 'typeorm'
Expand Down Expand Up @@ -55,7 +56,7 @@ export class Identity extends BaseEntity {
)
issuedPresentations: Presentation[]

@OneToMany(
@ManyToMany(
type => Presentation,
presentation => presentation.audience,
)
Expand Down
5 changes: 3 additions & 2 deletions packages/daf-core/src/entities/presentation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,15 +40,16 @@ export class Presentation extends BaseEntity {
)
issuer: Identity

@ManyToOne(
@ManyToMany(
type => Identity,
identity => identity.receivedPresentations,
{
cascade: ['insert'],
eager: true,
},
)
audience: Identity
@JoinTable()
audience: Identity[]

@Column({ nullable: true })
id?: String
Expand Down
45 changes: 30 additions & 15 deletions packages/daf-core/src/graphql/graphql-core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,10 @@ function decorateQB(

if (input?.order) {
for (const item of input.order) {
qb = qb.orderBy(`${qb.connection.driver.escape(tablename)}.${qb.connection.driver.escape(item.column)}`, item.direction)
qb = qb.orderBy(
`${qb.connection.driver.escape(tablename)}.${qb.connection.driver.escape(item.column)}`,
item.direction,
)
}
}
return qb
Expand Down Expand Up @@ -234,9 +237,12 @@ const credentials = async (_: any, args: FindArgs, ctx: Context) => {
if (ctx.authenticatedDid) {
qb = qb.andWhere(
new Brackets(qb => {
qb.where('credential.subject = :ident', { ident: ctx.authenticatedDid }).orWhere('credential.issuer = :ident', {
ident: ctx.authenticatedDid,
})
qb.where('credential.subject = :ident', { ident: ctx.authenticatedDid }).orWhere(
'credential.issuer = :ident',
{
ident: ctx.authenticatedDid,
},
)
}),
)
}
Expand All @@ -255,9 +261,12 @@ const credentialsCount = async (_: any, args: FindArgs, ctx: Context) => {
if (ctx.authenticatedDid) {
qb = qb.andWhere(
new Brackets(qb => {
qb.where('credential.subject = :ident', { ident: ctx.authenticatedDid }).orWhere('credential.issuer = :ident', {
ident: ctx.authenticatedDid,
})
qb.where('credential.subject = :ident', { ident: ctx.authenticatedDid }).orWhere(
'credential.issuer = :ident',
{
ident: ctx.authenticatedDid,
},
)
}),
)
}
Expand Down Expand Up @@ -321,7 +330,7 @@ export const resolvers = {

Query: {
identity: async (_: any, { did }, ctx: Context) =>
(checkAuthIdentity(did, ctx.authenticatedDid)) &&
checkAuthIdentity(did, ctx.authenticatedDid) &&
(await ctx.agent.dbConnection).getRepository(Identity).findOne(did),
identities: async (_: any, { input }, ctx: Context) => {
if (ctx.authenticatedDid) {
Expand All @@ -339,9 +348,12 @@ export const resolvers = {
if (ctx.authenticatedDid) {
qb = qb.andWhere(
new Brackets(qb => {
qb.where('message.to = :ident', { ident: ctx.authenticatedDid }).orWhere('message.from = :ident', {
ident: ctx.authenticatedDid,
})
qb.where('message.to = :ident', { ident: ctx.authenticatedDid }).orWhere(
'message.from = :ident',
{
ident: ctx.authenticatedDid,
},
)
}),
)
}
Expand Down Expand Up @@ -400,9 +412,12 @@ export const resolvers = {
if (ctx.authenticatedDid) {
qb = qb.andWhere(
new Brackets(qb => {
qb.where('claim.subject = :ident', { ident: ctx.authenticatedDid }).orWhere('claim.issuer = :ident', {
ident: ctx.authenticatedDid,
})
qb.where('claim.subject = :ident', { ident: ctx.authenticatedDid }).orWhere(
'claim.issuer = :ident',
{
ident: ctx.authenticatedDid,
},
)
}),
)
}
Expand Down Expand Up @@ -715,7 +730,7 @@ export const typeDefs = `
id: String
raw: String!
issuer: Identity!
audience: Identity!
audience: [Identity]!
issuanceDate: Date!
expirationDate: Date
context: [String]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ describe('daf-selective-disclosure-helper', () => {

const presentation = new Presentation()
presentation.issuer = identity
presentation.audience = identity
presentation.audience = [identity]
presentation.context = ['https://www.w3.org/2018/credentials/v1']
presentation.type = ['VerifiablePresentation']
presentation.credentials = [credential1, credential3]
Expand Down Expand Up @@ -120,7 +120,7 @@ describe('daf-selective-disclosure-helper', () => {

const presentation = new Presentation()
presentation.issuer = identity
presentation.audience = identity
presentation.audience = [identity]
presentation.context = ['https://www.w3.org/2018/credentials/v1']
presentation.type = ['VerifiablePresentation']
presentation.credentials = [credential1]
Expand Down
2 changes: 1 addition & 1 deletion packages/daf-w3c/src/action-handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ export interface PresentationInput {
type: string[]
id?: string
issuer: string
audience: string
audience: string[]
tag?: string
expirationDate?: string
verifiableCredential: string[]
Expand Down
2 changes: 1 addition & 1 deletion packages/daf-w3c/src/graphql.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ export const typeDefs = `
expirationDate: Date
tag: String
issuer: String!
audience: String!
audience: [String]!
context: [String]!
type: [String]!
verifiableCredential: [String]!
Expand Down
10 changes: 7 additions & 3 deletions packages/daf-w3c/src/message-handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,6 @@ export function createCredential(payload: VerifiableCredentialPayload, jwt: stri
vc.expirationDate = timestampToDate(payload.exp)
}


if (payload.vc.credentialStatus) {
vc.credentialStatus = payload.vc.credentialStatus
}
Expand All @@ -129,8 +128,13 @@ export function createPresentation(
vp.issuer = new Identity()
vp.issuer.did = payload.iss

vp.audience = new Identity()
vp.audience.did = payload.aud
const audArray = Array.isArray(payload.aud) ? (payload.aud as string[]) : [payload.aud]

vp.audience = audArray.map((did: string) => {
const id = new Identity()
id.did = did
return id
})

vp.raw = jwt

Expand Down

0 comments on commit eaa1a40

Please sign in to comment.