Skip to content

Commit

Permalink
added create, delete, get API + db operations for pip data
Browse files Browse the repository at this point in the history
  • Loading branch information
woutslakhorst committed May 22, 2024
1 parent e05676a commit 9fc51e5
Show file tree
Hide file tree
Showing 7 changed files with 261 additions and 20 deletions.
55 changes: 50 additions & 5 deletions api/pip/api.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,57 @@
package pip

import "context"
import (
"context"
"encoding/json"
"github.com/nuts-foundation/nuts-pxp/db"
)

var _ StrictServerInterface = (*Wrapper)(nil)

type Wrapper struct{}
type Wrapper struct {
DB db.DB
}

func (w Wrapper) CreateData(_ context.Context, request CreateDataRequestObject) (CreateDataResponseObject, error) {
// serialize authInput for storage
authInput, _ := json.Marshal(request.Body.AuthInput)
data := db.SQLData{
Id: request.Id,
Client: request.Body.ClientId,
Scope: request.Body.Scope,
Verifier: request.Body.VerifierId,
AuthInput: string(authInput),
}
err := w.DB.Create(data)
if err != nil {
return nil, err
}
return CreateData204Response{}, nil
}

func (w Wrapper) GetData(ctx context.Context, request GetDataRequestObject) (GetDataResponseObject, error) {
data, err := w.DB.Get(request.Id)
if err != nil {
return nil, err
}
// turn data into map[string]interface{}
var authInput map[string]interface{}
err = json.Unmarshal([]byte(data.AuthInput), &authInput)
if err != nil {
return nil, err
}
return GetData200JSONResponse{
ClientId: data.Client,
Scope: data.Scope,
VerifierId: data.Verifier,
AuthInput: authInput,
}, nil
}

func (w Wrapper) CreateData(ctx context.Context, request CreateDataRequestObject) (CreateDataResponseObject, error) {
//TODO implement me
panic("implement me")
func (w Wrapper) DeleteData(ctx context.Context, request DeleteDataRequestObject) (DeleteDataResponseObject, error) {
err := w.DB.Delete(request.Id)
if err != nil {
return nil, err
}
return DeleteData204Response{}, nil
}
124 changes: 124 additions & 0 deletions api/pip/generated.go

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

38 changes: 34 additions & 4 deletions db/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ package db
import (
"context"
"errors"
"os"
"strings"

"github.com/glebarez/sqlite"
"github.com/nuts-foundation/nuts-pxp/config"
sql_migrations "github.com/nuts-foundation/nuts-pxp/db/migrations"
Expand All @@ -11,10 +14,10 @@ import (
"gorm.io/driver/postgres"
"gorm.io/driver/sqlserver"
"gorm.io/gorm"
"os"
"strings"
)

var _ DB = (*SqlDB)(nil)

type SqlDB struct {
sqlDB *gorm.DB
}
Expand Down Expand Up @@ -96,10 +99,37 @@ func New(config config.Config) (*SqlDB, error) {
return e, nil
}

func (e *SqlDB) Close() error {
sqlDB, err := e.sqlDB.DB()
func (db *SqlDB) Close() error {
sqlDB, err := db.sqlDB.DB()
if err != nil {
return err
}
return sqlDB.Close()
}

func (db *SqlDB) Create(data SQLData) error {
return db.sqlDB.Create(data).Error
}

func (db *SqlDB) Delete(id string) error {
return db.sqlDB.Where("id = ?", id).Delete(&SQLData{}).Error
}

func (db *SqlDB) Get(id string) (SQLData, error) {
var record SQLData
err := db.sqlDB.Model(&SQLData{}).Where("id = ?", id).First(&record).Error
if err != nil {
return SQLData{}, err
}
return record, nil
}

func (db *SqlDB) Query(scope string, verifier string, client string) (string, error) {
var record SQLData
err := db.sqlDB.Model(&SQLData{}).Where("scope = ? AND verifier = ? AND client = ?", scope, verifier, client).
First(&record).Error
if err != nil {
return "", err
}
return record.AuthInput, nil
}
5 changes: 5 additions & 0 deletions db/interface.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
package db

type DB interface {
Close() error
Create(data SQLData) error
Delete(id string) error
Get(id string) (SQLData, error)
Query(scope string, verifier string, client string) (string, error)
}
17 changes: 17 additions & 0 deletions db/types.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package db

import "gorm.io/gorm/schema"

var _ schema.Tabler = (*SQLData)(nil)

type SQLData struct {
Id string `gorm:"primaryKey"`
Client string
Scope string
Verifier string
AuthInput string `gorm:"column:auth_input"`
}

func (s SQLData) TableName() string {
return "data"
}
2 changes: 1 addition & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ func main() {
echoServer.HidePort = true

// init API & register routes
pipController := &pip.Wrapper{}
pipController := &pip.Wrapper{DB: sqlDb}
opaController := &opa.Wrapper{}
pip.RegisterHandlers(echoServer, pip.NewStrictHandler(pipController, []pip.StrictMiddlewareFunc{}))
opa.RegisterHandlers(echoServer, opa.NewStrictHandler(opaController, []opa.StrictMiddlewareFunc{}))
Expand Down
40 changes: 30 additions & 10 deletions oas/pip.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,23 +7,35 @@ servers:
description: Default endpoint
paths:
/pip/{id}:
parameters:
- name: id
in: path
required: true
description: An identifier for the data, used for deletion and updates
content:
plain/text:
schema:
type: string
example: 1111-2222-3333-4444
get:
operationId: getData
summary: Get pip data for given ide
tags:
- pip
responses:
'200':
description: Data known for id
content:
application/json:
schema:
$ref: '#/components/schemas/Data'
post:
operationId: createData
summary: Add authorization data used for OPA evaluation
description: |
Add data to the PIP.
tags:
- pip
parameters:
- name: id
in: path
required: true
description: An identifier for the data, used for deletion and updates
content:
plain/text:
schema:
type: string
example: 1111-2222-3333-4444
requestBody:
required: true
content:
Expand All @@ -33,6 +45,14 @@ paths:
responses:
'204':
description: Successful request. No content.
delete:
operationId: deleteData
summary: Delete data for given id
tags:
- pip
responses:
'204':
description: Successful request. No content.
components:
schemas:
Data:
Expand Down

0 comments on commit 9fc51e5

Please sign in to comment.