Skip to content

Commit

Permalink
Merge pull request #482 from 0xPolygonID/PID-1304-issuer-node-auth-qr…
Browse files Browse the repository at this point in the history
…-code-callback

chore: add create claim when the user authenticates
  • Loading branch information
martinsaporiti authored Aug 30, 2023
2 parents 14b3e3b + 1183ebb commit cb0371e
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 1 deletion.
8 changes: 8 additions & 0 deletions api_ui/api.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,8 @@ paths:
description: ok
'400':
$ref: '#/components/responses/400'
'409':
$ref: '#/components/responses/409'
'500':
$ref: '#/components/responses/500'

Expand Down Expand Up @@ -1649,6 +1651,12 @@ components:
application/json:
schema:
$ref: '#/components/schemas/GenericErrorMessage'
'409':
description: 'Conflict'
content:
application/json:
schema:
$ref: '#/components/schemas/GenericErrorMessage'
'422':
description: 'Unprocessable Content'
content:
Expand Down
5 changes: 5 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@ github.com/cenkalti/backoff/v3 v3.2.2 h1:cfUAAO3yvKMYKPrvhDuHSwQnhZNk/RMHKdZqKTx
github.com/cenkalti/backoff/v3 v3.2.2/go.mod h1:cIeZDE3IrqwwJl6VUwCN6trj1oXrTS4rc0ij+ULvLYs=
github.com/census-instrumentation/opencensus-proto v0.2.1/go.mod h1:f6KPmirojxKA12rnyqOA5BBL4O983OfeGPqjHWSTneU=
github.com/cespare/cp v0.1.0 h1:SE+dxFebS7Iik5LK0tsi1k9ZCxEaFX4AjQmoyA+1dJk=
github.com/cespare/xxhash v1.1.0/go.mod h1:XrSqR1VqqWfGrhpAt58auRo0WTKS1nRRg3ghfAqPWnc=
github.com/cespare/xxhash/v2 v2.1.1/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs=
github.com/cespare/xxhash/v2 v2.1.2/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs=
github.com/cespare/xxhash/v2 v2.2.0 h1:DC2CZ1Ep5Y4k3ZQ899DldepgrayRUGE6BBZ/cd9Cj44=
Expand Down Expand Up @@ -163,6 +164,7 @@ github.com/cockroachdb/pebble v0.0.0-20230309163202-51422ae2d449 h1:eSpFD9gBOKKQ
github.com/cockroachdb/pebble v0.0.0-20230309163202-51422ae2d449/go.mod h1:9lRMC4XN3/BLPtIp6kAKwIaHu369NOf2rMucPzipz50=
github.com/cockroachdb/redact v1.1.3 h1:AKZds10rFSIj7qADf0g46UixK8NNLwWTNdCIGS5wfSQ=
github.com/cockroachdb/redact v1.1.3/go.mod h1:BVNblN9mBWFyMyqK1k3AAiSxhvhfK2oOZZ2lK+dpvRg=
github.com/cockroachdb/sentry-go v0.6.1-cockroachdb.2/go.mod h1:8BT+cPK6xvFOcRlk0R8eg+OTkcqI6baNH4xAkpiYVvQ=
github.com/codegangsta/inject v0.0.0-20150114235600-33e0aa1cb7c0/go.mod h1:4Zcjuz89kmFXt9morQgcfYZAYZ5n8WHjt81YYWIwtTM=
github.com/coreos/etcd v3.3.10+incompatible/go.mod h1:uF7uidLiAD3TWHmW31ZFd/JWoc32PjwdhPthX9715RE=
github.com/coreos/go-etcd v2.0.0+incompatible/go.mod h1:Jez6KQU2B/sWsbdaef3ED8NzMklzPG4d5KIOhIy30Tk=
Expand Down Expand Up @@ -273,6 +275,7 @@ github.com/go-kit/log v0.1.0/go.mod h1:zbhenjAZHb184qTLMA9ZjW7ThYL0H2mk7Q6pNt4vb
github.com/go-logfmt/logfmt v0.3.0/go.mod h1:Qt1PoO58o5twSAckw1HlFXLmHsOX5/0LbT9GBnD5lWE=
github.com/go-logfmt/logfmt v0.4.0/go.mod h1:3RMwSq7FuexP4Kalkev3ejPJsZTpXXBr9+V4qmtdjCk=
github.com/go-logfmt/logfmt v0.5.0/go.mod h1:wCYkCAKZfumFQihp8CzCvQ3paCTfi41vtzG1KdI/P7A=
github.com/go-logfmt/logfmt v0.5.1/go.mod h1:WYhtIu8zTZfxdn5+rREduYbwxfcBr/Vr6KEVveWlfTs=
github.com/go-logr/logr v1.2.4 h1:g01GSCwiDw2xSZfjJ2/T9M+S6pFdcNtFYsp+Y43HYDQ=
github.com/go-martini/martini v0.0.0-20170121215854-22fa46961aab/go.mod h1:/P9AEU963A2AYjv4d1V5eVL1CQbEJq6aCNHDDjibzu8=
github.com/go-ole/go-ole v1.2.6 h1:/Fpf6oFPoeFik9ty7siob0G6Ke8QvQEuVcuChpwXzpY=
Expand Down Expand Up @@ -774,6 +777,7 @@ github.com/mediocregopher/radix/v3 v3.4.2/go.mod h1:8FL3F6UQRXHXIBSPUs5h0RybMF8i
github.com/mgechev/revive v1.3.1 h1:OlQkcH40IB2cGuprTPcjB0iIUddgVZgGmDX3IAMR8D4=
github.com/mgechev/revive v1.3.1/go.mod h1:YlD6TTWl2B8A103R9KWJSPVI9DrEf+oqr15q21Ld+5I=
github.com/microcosm-cc/bluemonday v1.0.2/go.mod h1:iVP4YcDBq+n/5fb23BhYFvIMq/leAFZyRl6bYmGDlGc=
github.com/minio/blake2b-simd v0.0.0-20160723061019-3f5f724cb5b1/go.mod h1:pD8RvIylQ358TN4wwqatJ8rNavkEINozVn9DtGI3dfQ=
github.com/minio/sha256-simd v1.0.1 h1:6kaan5IFmwTNynnKKpDHe6FWHohJOHhCPchzK49dzMM=
github.com/minio/sha256-simd v1.0.1/go.mod h1:Pz6AKMiUdngCLpeTL/RJY1M9rUuPMYujV5xJjtbRSN8=
github.com/mitchellh/cli v1.0.0/go.mod h1:hNIlj7HEI86fIcpObd7a0FcrxTWetlwJDGcceTlRvqc=
Expand Down Expand Up @@ -1610,6 +1614,7 @@ gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk=
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q=
gopkg.in/errgo.v2 v2.1.0/go.mod h1:hNsd1EY+bozCKY1Ytp96fpM3vjJbqLJn88ws8XvfDNI=
gopkg.in/fsnotify.v1 v1.4.7/go.mod h1:Tz8NjZHkW78fSQdbUxIjBTcgA1z1m8ZHf0WmKUhAMys=
gopkg.in/go-jose/go-jose.v2 v2.6.1 h1:qEzJlIDmG9q5VO0M/o8tGS65QMHMS1w01TQJB1VPJ4U=
Expand Down
14 changes: 14 additions & 0 deletions internal/api_ui/api.gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

52 changes: 51 additions & 1 deletion internal/api_ui/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,15 +140,65 @@ func (s *Server) AuthCallback(ctx context.Context, request AuthCallbackRequestOb
return AuthCallback400JSONResponse{N400JSONResponse{"Cannot proceed with empty body"}}, nil
}

_, err := s.identityService.Authenticate(ctx, *request.Body, request.Params.SessionID, s.cfg.APIUI.ServerURL, s.cfg.APIUI.IssuerDID)
arm, err := s.identityService.Authenticate(ctx, *request.Body, request.Params.SessionID, s.cfg.APIUI.ServerURL, s.cfg.APIUI.IssuerDID)
if err != nil {
log.Debug(ctx, "error authenticating", err.Error())
return AuthCallback500JSONResponse{}, nil
}

created, err := s.createCampaignClaim(ctx, s.cfg.APIUI.IssuerDID, arm.From)
if err != nil {
log.Error(ctx, "error creating campaign claim", err.Error())
return AuthCallback500JSONResponse{}, nil
}

if !created {
log.Info(ctx, "claim already exists")
return AuthCallback409JSONResponse{N409JSONResponse{"claim already issued"}}, nil
}

return AuthCallback200Response{}, nil
}

func (s *Server) createCampaignClaim(ctx context.Context, issuerDID core.DID, userDID string) (bool, error) {
credentialSchema := "https://raw.githubusercontent.com/iden3/claim-schema-vocab/main/schemas/json/KYCAgeCredential-v3.json"
credentialType := "KYCAgeCredential"
//nolint:all
credentialSubject := map[string]interface{}{
"id": userDID,
"birthday": 19960426,
"documentType": 11,
}
//nolint:all
credentialExpiration := time.Now().Add(time.Hour * 24 * 365 * 10)

claims, err := s.claimService.GetAll(ctx, issuerDID, &ports.ClaimsFilter{
Subject: userDID,
SchemaType: credentialType,
})
if err != nil {
log.Error(ctx, "error getting claims", "err", err)
return false, err
}

if len(claims) > 0 {
log.Info(ctx, "claim already exists")
return false, nil
}

credentialRequest := ports.NewCreateClaimRequest(&issuerDID, credentialSchema, credentialSubject, &credentialExpiration, credentialType,
nil, nil, nil, common.ToPointer(true), common.ToPointer(false),
nil, true)

_, err = s.claimService.Save(ctx, credentialRequest)
if err != nil {
log.Error(ctx, "error saving claim", "err", err)
return false, err
}

return true, nil
}

// AuthQRCode returns the qr code for authenticating a user
func (s *Server) AuthQRCode(ctx context.Context, _ AuthQRCodeRequestObject) (AuthQRCodeResponseObject, error) {
qrCode, err := s.identityService.CreateAuthenticationQRCode(ctx, s.cfg.APIUI.ServerURL, s.cfg.APIUI.IssuerDID)
Expand Down

0 comments on commit cb0371e

Please sign in to comment.