Skip to content

Commit

Permalink
add: method to prepend prefix with tag and content length; update: mo…
Browse files Browse the repository at this point in the history
…ve ctx to the api to have access to ctx from requests folder too
  • Loading branch information
mhrynenko committed May 21, 2024
1 parent 671c522 commit ea4b1f6
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 22 deletions.
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
package handlers
package api

import (
"context"
"net/http"

"github.com/ethereum/go-ethereum/ethclient"
stateabi "github.com/iden3/contracts-abi/state/go/abi"
"github.com/rarimo/passport-identity-provider/internal/config"
"github.com/rarimo/passport-identity-provider/internal/data"
"github.com/rarimo/passport-identity-provider/internal/service/issuer"
"github.com/rarimo/passport-identity-provider/internal/service/vault"
"gitlab.com/distributed_lab/logan/v3"
"net/http"
)

type ctxKey int
Expand Down
15 changes: 8 additions & 7 deletions internal/service/api/handlers/create_identity.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
"github.com/rarimo/certificate-transparency-go/x509"
"github.com/rarimo/passport-identity-provider/internal/config"
"github.com/rarimo/passport-identity-provider/internal/data"
"github.com/rarimo/passport-identity-provider/internal/service/api"
"github.com/rarimo/passport-identity-provider/internal/service/api/requests"
"github.com/rarimo/passport-identity-provider/resources"
"gitlab.com/distributed_lab/ape"
Expand Down Expand Up @@ -58,18 +59,18 @@ var algorithmsListMap = map[string]map[string]string{
func CreateIdentity(w http.ResponseWriter, r *http.Request) {
req, err := requests.NewCreateIdentityRequest(r)
if err != nil {
Log(r).WithError(err).Error("failed to create new create identity request")
api.Log(r).WithError(err).Error("failed to create new create identity request")
ape.RenderErr(w, problems.BadRequest(err)...)
return
}

rawReqData, err := json.Marshal(req.Data)
if err != nil {
Log(r).WithError(err).Error("failed to marshal create identity request")
api.Log(r).WithError(err).Error("failed to marshal create identity request")
ape.RenderErr(w, problems.InternalError())
return
}
log := Log(r).WithFields(logan.F{
log := api.Log(r).WithFields(logan.F{
"user-agent": r.Header.Get("User-Agent"),
"request_data": string(rawReqData),
})
Expand Down Expand Up @@ -116,7 +117,7 @@ func CreateIdentity(w http.ResponseWriter, r *http.Request) {
return
}

cfg := VerifierConfig(r)
cfg := api.VerifierConfig(r)

switch algorithm {
case SHA1withECDSA:
Expand Down Expand Up @@ -170,7 +171,7 @@ func CreateIdentity(w http.ResponseWriter, r *http.Request) {
return
}

masterQ := MasterQ(r)
masterQ := api.MasterQ(r)

claim, err := masterQ.Claim().ResetFilter().
FilterBy("user_did", req.Data.ID.String()).
Expand Down Expand Up @@ -213,8 +214,8 @@ func CreateIdentity(w http.ResponseWriter, r *http.Request) {
}

var claimID string
iss := Issuer(r)
vaultClient := VaultClient(r)
iss := api.Issuer(r)
vaultClient := api.VaultClient(r)

blinder, err := vaultClient.Blinder()
if err != nil {
Expand Down
9 changes: 5 additions & 4 deletions internal/service/api/handlers/get_gist_data.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"github.com/iden3/contracts-abi/state/go/abi"
core "github.com/iden3/go-iden3-core/v2"
"github.com/iden3/go-iden3-core/v2/w3c"
"github.com/rarimo/passport-identity-provider/internal/service/api"
"github.com/rarimo/passport-identity-provider/internal/service/api/requests"
"github.com/rarimo/passport-identity-provider/resources"
"gitlab.com/distributed_lab/ape"
Expand All @@ -21,12 +22,12 @@ import (
func GetGistData(w http.ResponseWriter, r *http.Request) {
req, err := requests.NewGetGistDataRequest(r)
if err != nil {
Log(r).WithError(err).Error("failed to parse get gist data request")
api.Log(r).WithError(err).Error("failed to parse get gist data request")
ape.RenderErr(w, problems.BadRequest(err)...)
return
}

log := Log(r).WithFields(logan.F{
log := api.Log(r).WithFields(logan.F{
"user-agent": r.Header.Get("User-Agent"),
"user_did": req.UserDID,
"block_number": req.BlockNumber,
Expand All @@ -46,7 +47,7 @@ func GetGistData(w http.ResponseWriter, r *http.Request) {
return
}

blockNum, err := EthClient(r).BlockNumber(context.Background())
blockNum, err := api.EthClient(r).BlockNumber(context.Background())
if err != nil {
log.WithError(err).Error("failed to get block number")
ape.RenderErr(w, problems.InternalError())
Expand All @@ -67,7 +68,7 @@ func GetGistData(w http.ResponseWriter, r *http.Request) {
blockNum = req.BlockNumber
}

stateContract := StateContract(r)
stateContract := api.StateContract(r)

gistProof, err := stateContract.GetGISTProof(&bind.CallOpts{
BlockNumber: new(big.Int).SetUint64(blockNum),
Expand Down
51 changes: 49 additions & 2 deletions internal/service/api/requests/create_identity.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,13 @@ package requests
import (
"encoding/json"
"net/http"
"strconv"
"strings"

"github.com/iden3/go-iden3-core/v2/w3c"
snarkTypes "github.com/iden3/go-rapidsnark/types"
"github.com/rarimo/passport-identity-provider/internal/service/api"
"gitlab.com/distributed_lab/logan/v3"
"gitlab.com/distributed_lab/logan/v3/errors"
)

Expand Down Expand Up @@ -33,9 +37,52 @@ func NewCreateIdentityRequest(r *http.Request) (CreateIdentityRequest, error) {
return request, errors.Wrap(err, "failed to unmarshal")
}

if request.Data.DocumentSOD.EncapsulatedContent[0:2] != "30" {
request.Data.DocumentSOD.EncapsulatedContent = "30" + request.Data.DocumentSOD.EncapsulatedContent
encapsulatedContent := PrependPrefix(request.Data.DocumentSOD.EncapsulatedContent)
if strings.Compare(encapsulatedContent, request.Data.DocumentSOD.EncapsulatedContent) != 0 {
api.Log(r).WithFields(logan.F{
"encapsulated_content_new": encapsulatedContent,
"encapsulated_content_old": request.Data.DocumentSOD.EncapsulatedContent,
}).Info("encapsulated content update")
request.Data.DocumentSOD.EncapsulatedContent = encapsulatedContent
}

return request, nil
}

// PrependPrefix - сrunch before Android fix
func PrependPrefix(data string) string {
// Parse by VERSION field
subs := strings.Split(data, "0201")

dataLength := subs[0]

// recreate the rest of the string without length
rest := "0201" + strings.Join(subs[1:], "0201")

restByteLen := int64(len(rest) / 2)

actualLength := toHex(restByteLen)

if restByteLen > 128 && restByteLen < 256 {
actualLength = "81" + actualLength
}
if restByteLen > 256 {
actualLength = "82" + actualLength
}

data = "30" + dataLength + rest
if strings.Compare(dataLength, actualLength) != 0 {
data = "30" + actualLength + rest
}

return data
}

func toHex(number int64) string {
hexStr := strconv.FormatInt(number, 16)
if len(hexStr)%2 != 0 {
hexStr = "0" + hexStr
}

return hexStr
}
15 changes: 8 additions & 7 deletions internal/service/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"github.com/go-chi/chi"
stateabi "github.com/iden3/contracts-abi/state/go/abi"
"github.com/rarimo/passport-identity-provider/internal/data/pg"
"github.com/rarimo/passport-identity-provider/internal/service/api"
"github.com/rarimo/passport-identity-provider/internal/service/api/handlers"
"github.com/rarimo/passport-identity-provider/internal/service/issuer"
"github.com/rarimo/passport-identity-provider/internal/service/vault"
Expand Down Expand Up @@ -39,17 +40,17 @@ func (s *service) router() chi.Router {
ape.RecoverMiddleware(s.log),
ape.LoganMiddleware(s.log),
ape.CtxMiddleware(
handlers.CtxLog(s.log),
handlers.CtxMasterQ(pg.NewMasterQ(s.cfg.DB())),
handlers.CtxVerifierConfig(s.cfg.VerifierConfig()),
handlers.CtxStateContract(stateContract),
handlers.CtxIssuer(issuer.New(
api.CtxLog(s.log),
api.CtxMasterQ(pg.NewMasterQ(s.cfg.DB())),
api.CtxVerifierConfig(s.cfg.VerifierConfig()),
api.CtxStateContract(stateContract),
api.CtxIssuer(issuer.New(
s.cfg.Log().WithField("service", "issuer"),
s.cfg.IssuerConfig(),
issuerLogin, issuerPassword,
)),
handlers.CtxVaultClient(vaultClient),
handlers.CtxEthClient(ethCli),
api.CtxVaultClient(vaultClient),
api.CtxEthClient(ethCli),
),
)
r.Route("/integrations/identity-provider-service", func(r chi.Router) {
Expand Down

0 comments on commit ea4b1f6

Please sign in to comment.