Skip to content

Commit

Permalink
add create connection endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
volodymyr-basiuk committed Aug 1, 2024
1 parent 0c7facd commit c732366
Show file tree
Hide file tree
Showing 5 changed files with 242 additions and 0 deletions.
40 changes: 40 additions & 0 deletions api/api.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -489,6 +489,33 @@ paths:
$ref: '#/components/responses/400'
'500':
$ref: '#/components/responses/500'
post:
summary: Create Connection
operationId: createConnection
description: create connection
tags:
- Connection
security:
- basicAuth: [ ]
parameters:
- $ref: '#/components/parameters/pathIdentifier'
requestBody:
required: true
content:
application/json:
schema:
$ref: '#/components/schemas/CreateConnectionRequest'
responses:
'201':
description: Connection Created
content:
application/json:
schema:
$ref: '#/components/schemas/GenericMessage'
'400':
$ref: '#/components/responses/400'
'500':
$ref: '#/components/responses/500'

/v1/{identifier}/connections/{id}/credentials/revoke:
post:
Expand Down Expand Up @@ -2223,6 +2250,19 @@ components:
items:
type: string
example: [ "BJJSignature2021" ]
CreateConnectionRequest:
type: object
required: [ userDID, userDoc, issuerDoc ]
properties:
userDID:
type: string
example: did:polygonid:polygon:amoy:2qMZrfBsXuGFTwSqkqYki78zF3pe1vtXoqH4yRLsfs
userDoc:
type: object
format: byte
issuerDoc:
type: object
format: byte

parameters:
credentialStatusType:
Expand Down
122 changes: 122 additions & 0 deletions internal/api/api.gen.go

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

72 changes: 72 additions & 0 deletions internal/api/connections.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,17 @@ package api

import (
"context"
"encoding/json"
"errors"
"strings"
"time"

uuid "github.com/google/uuid"
"github.com/iden3/go-iden3-core/v2/w3c"
jsonSuite "github.com/iden3/go-schema-processor/v2/json"
"github.com/iden3/go-schema-processor/verifiable"

"github.com/polygonid/sh-id-platform/internal/core/domain"
"github.com/polygonid/sh-id-platform/internal/core/ports"
"github.com/polygonid/sh-id-platform/internal/core/services"
"github.com/polygonid/sh-id-platform/internal/log"
Expand Down Expand Up @@ -41,6 +47,68 @@ func (s *Server) GetConnections(ctx context.Context, request GetConnectionsReque
return GetConnections200JSONResponse(resp), nil
}

// CreateConnection creates a connection
func (s *Server) CreateConnection(ctx context.Context, request CreateConnectionRequestObject) (CreateConnectionResponseObject, error) {
// validate did document
issuerDoc, err := json.Marshal(request.Body.IssuerDoc)
if err != nil {
log.Debug(ctx, "invalid issuer did document object", "err", err, "req", request)
return CreateConnection400JSONResponse{N400JSONResponse{"invalid issuer did document"}}, nil
}

userDoc, err := json.Marshal(request.Body.UserDoc)
if err != nil {
log.Debug(ctx, "invalid user did document object", "err", err, "req", request)
return CreateConnection400JSONResponse{N400JSONResponse{"invalid user did document"}}, nil
}
validator := jsonSuite.Validator{}
if checkJSONIsNotNull(issuerDoc) {
err := validator.ValidateData(issuerDoc, []byte(verifiable.DIDDocumentJSONSchema))
if err != nil {
log.Debug(ctx, "invalid issuer did document", "err", err, "req", request)
return CreateConnection400JSONResponse{N400JSONResponse{"invalid issuer did document"}}, nil
}
}

if checkJSONIsNotNull(userDoc) {
err := validator.ValidateData(userDoc, []byte(verifiable.DIDDocumentJSONSchema))
if err != nil {
log.Debug(ctx, "invalid user did document", "err", err, "req", request)
return CreateConnection400JSONResponse{N400JSONResponse{"invalid user did document"}}, nil
}
}

issuerDID, err := w3c.ParseDID(request.Identifier)
if err != nil {
log.Debug(ctx, "invalid issuer did", "err", err, "req", request)
return CreateConnection400JSONResponse{N400JSONResponse{"invalid issuer did"}}, nil
}

userDID, err := w3c.ParseDID(request.Body.UserDID)
if err != nil {
log.Debug(ctx, "invalid user did", "err", err, "req", request)
return CreateConnection400JSONResponse{N400JSONResponse{"invalid user did"}}, nil
}

conn := &domain.Connection{
ID: uuid.New(),
IssuerDID: *issuerDID,
UserDID: *userDID,
IssuerDoc: issuerDoc,
UserDoc: userDoc,
CreatedAt: time.Now(),
ModifiedAt: time.Now(),
}

err = s.connectionsService.Create(ctx, conn)
if err != nil {
log.Debug(ctx, "create connection error", "err", err, "req", request)
return CreateConnection500JSONResponse{N500JSONResponse{"There was an error creating the connection"}}, nil
}

return CreateConnection201JSONResponse{}, nil
}

// DeleteConnection deletes a connection
func (s *Server) DeleteConnection(ctx context.Context, request DeleteConnectionRequestObject) (DeleteConnectionResponseObject, error) {
issuerDID, err := w3c.ParseDID(request.Identifier)
Expand Down Expand Up @@ -158,3 +226,7 @@ func getConnectionsFilter(req GetConnectionsRequestObject) (*ports.NewGetAllConn
}
return ports.NewGetAllRequest(req.Params.Credentials, req.Params.Query, req.Params.Page, req.Params.MaxResults, orderBy), nil
}

func checkJSONIsNotNull(message []byte) bool {
return message != nil && string(message) != "null"
}
1 change: 1 addition & 0 deletions internal/core/ports/connections_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ func NewDeleteRequest(connID uuid.UUID, deleteCredentials *bool, revokeCredentia

// ConnectionsService is the interface implemented by the Connections service
type ConnectionsService interface {
Create(ctx context.Context, conn *domain.Connection) error
Delete(ctx context.Context, id uuid.UUID, deleteCredentials bool, issuerDID w3c.DID) error
DeleteCredentials(ctx context.Context, id uuid.UUID, issuerID w3c.DID) error
GetByIDAndIssuerID(ctx context.Context, id uuid.UUID, issuerDID w3c.DID) (*domain.Connection, error)
Expand Down
7 changes: 7 additions & 0 deletions internal/core/services/connections.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,13 @@ func NewConnection(connRepo ports.ConnectionsRepository, claimsRepo ports.Claims
}
}

func (c *connection) Create(ctx context.Context, connection *domain.Connection) error {
return c.storage.Pgx.BeginFunc(ctx, func(tx pgx.Tx) error {
_, err := c.connRepo.Save(ctx, c.storage.Pgx, connection)
return err
})
}

func (c *connection) Delete(ctx context.Context, id uuid.UUID, deleteCredentials bool, issuerDID w3c.DID) error {
return c.storage.Pgx.BeginFunc(ctx,
func(tx pgx.Tx) error {
Expand Down

0 comments on commit c732366

Please sign in to comment.