Skip to content

Commit

Permalink
add proof operator (#8)
Browse files Browse the repository at this point in the history
* add proof operator

* post-merge fix
  • Loading branch information
freigeistig authored Jan 24, 2024
1 parent a5b6a39 commit 2148115
Show file tree
Hide file tree
Showing 14 changed files with 105 additions and 20 deletions.
5 changes: 5 additions & 0 deletions docs/spec/components/schemas/Proof.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ allOf:
- proof_type
- org_id
- schema_url
- operator
properties:
creator:
type: string
Expand All @@ -35,3 +36,7 @@ allOf:
schema_url:
type: string
description: The schema URL of the claim the proof was created based on
operator:
type: string
description: The operator that will be used to check the proof
example: "$eq"
4 changes: 4 additions & 0 deletions docs/spec/components/schemas/ProofCreate.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ required:
- org_id
- user_did
- schema_url
- operator
properties:
user_did:
type: string
Expand All @@ -22,3 +23,6 @@ properties:
schema_url:
type: string
description: The schema URL of the claim the proof was created based on
operator:
type: string
description: The operator that will be used to check the proof
7 changes: 7 additions & 0 deletions internal/assets/migrations/002_proof_operator.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
-- +migrate Up
create type proof_operator as enum('$noop', '$eq', '$lt', '$gt', '$in', '$nin', '$ne');
alter table proofs add column operator proof_operator not null;

-- +migrate Down
alter table proofs drop column operator;
drop type proof_operator;
1 change: 1 addition & 0 deletions internal/data/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package data

import (
"context"

"github.com/google/uuid"
)

Expand Down
24 changes: 12 additions & 12 deletions internal/data/pg/schema.xo.go

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

36 changes: 36 additions & 0 deletions internal/data/proofs.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package data

type ProofOperator string

const (
Noop ProofOperator = "$noop"
Eq ProofOperator = "$eq"
Lt ProofOperator = "$lt"
Gt ProofOperator = "$gt"
In ProofOperator = "$in"
Nin ProofOperator = "$nin"
Ne ProofOperator = "$ne"
)

func (o ProofOperator) String() string {
return string(o)
}

var proofOperatorStrConv = map[string]ProofOperator{
"$noop": Noop,
"$eq": Eq,
"$lt": Lt,
"$gt": Gt,
"$in": In,
"$nin": Nin,
"$ne": Ne,
}

func ProofOperatorFromString(data string) (ProofOperator, bool) {
res, ok := proofOperatorStrConv[data]
return res, ok
}

func MustProofOperatorFromString(data string) ProofOperator {
return proofOperatorStrConv[data]
}
15 changes: 8 additions & 7 deletions internal/data/schema.xo.go

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

8 changes: 8 additions & 0 deletions internal/services/api/handlers/create_proof.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,12 @@ func newProofCreateRequest(r *http.Request) (*proofCreateRequest, error) {
}
}

if err := validation.Validate(req.Data.Operator, ValidationOperator); err != nil {
return nil, validation.Errors{
"data/operator": errors.Wrap(err, "invalid operator value"),
}
}

return &req, nil
}

Expand Down Expand Up @@ -67,6 +73,7 @@ func CreateProof(w http.ResponseWriter, r *http.Request) {
Type: req.Data.ProofType,
OrgID: orgID,
SchemaURL: req.Data.SchemaUrl,
Operator: data.MustProofOperatorFromString(req.Data.Operator),
}

err = Storage(r).ProofQ().Insert(&proof)
Expand All @@ -89,6 +96,7 @@ func CreateProof(w http.ResponseWriter, r *http.Request) {
ProofType: proof.Type,
OrgId: proof.OrgID.String(),
SchemaUrl: req.Data.SchemaUrl,
Operator: proof.Operator.String(),
},
},
Included: resources.Included{},
Expand Down
1 change: 1 addition & 0 deletions internal/services/api/handlers/get_proof.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ func ProofByID(w http.ResponseWriter, r *http.Request) {
ProofType: proof.Type,
OrgId: proof.OrgID.String(),
SchemaUrl: proof.SchemaURL,
Operator: proof.Operator.String(),
},
},
Included: resources.Included{},
Expand Down
1 change: 1 addition & 0 deletions internal/services/api/handlers/get_proofs_by_link.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ func GetLinkByID(w http.ResponseWriter, r *http.Request) {
ProofType: proof.Type,
OrgId: proof.OrgID.String(),
SchemaUrl: proof.SchemaURL,
Operator: proof.Operator.String(),
},
})
}
Expand Down
16 changes: 16 additions & 0 deletions internal/services/api/handlers/validation.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package handlers

import (
validation "github.com/go-ozzo/ozzo-validation/v4"
"github.com/rarimo/rarime-link-svc/internal/data"
)

var (
ErrOperator = validation.NewError("validation_is_operator", "must be a valid operator")
ValidationOperator = validation.NewStringRuleWithError(isOperator, ErrOperator)
)

func isOperator(op string) bool {
_, ok := data.ProofOperatorFromString(op)
return ok
}
3 changes: 2 additions & 1 deletion internal/services/proofs_cleaner/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@ package proofs_cleaner

import (
"context"
"time"

"github.com/rarimo/rarime-link-svc/internal/config"
"github.com/rarimo/rarime-link-svc/internal/data"
"gitlab.com/distributed_lab/logan/v3"
"gitlab.com/distributed_lab/running"
"time"
)

type ProofsCleaner struct {
Expand Down
2 changes: 2 additions & 0 deletions resources/model_proof_attributes.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ type ProofAttributes struct {
CreatedAt time.Time `json:"created_at"`
// The ID of the user who created the proof
Creator string `json:"creator"`
// The operator that will be used to check the proof
Operator string `json:"operator"`
// The ID of the organization that issued the proof's claim
OrgId string `json:"org_id"`
// The proof object in JSON string format
Expand Down
2 changes: 2 additions & 0 deletions resources/model_proof_create.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
package resources

type ProofCreate struct {
// The operator that will be used to check the proof
Operator string `json:"operator"`
// The ID of the organization that issued the proof's claim
OrgId string `json:"org_id"`
// The proof object in JSON string format
Expand Down

0 comments on commit 2148115

Please sign in to comment.