Skip to content

Commit

Permalink
return bool from verification function
Browse files Browse the repository at this point in the history
  • Loading branch information
Eslam-Nawara committed Oct 30, 2024
1 parent b79f1ec commit 9495238
Showing 1 changed file with 28 additions and 18 deletions.
46 changes: 28 additions & 18 deletions pkg/provision/engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,20 @@ import (
"encoding/hex"
"encoding/json"
"fmt"
"io"
"net/http"
"net/url"
"os"
"path/filepath"
"sort"
"time"

"github.com/cenkalti/backoff/v3"
"github.com/joncrlsn/dque"
"github.com/pkg/errors"
"github.com/rs/zerolog/log"
substrate "github.com/threefoldtech/tfchain/clients/tfchain-client-go"
"github.com/threefoldtech/zos4/pkg"
"github.com/threefoldtech/zos4/pkg/environment"
"github.com/threefoldtech/zos4/pkg/gridtypes"
"github.com/threefoldtech/zos4/pkg/gridtypes/zos"
"github.com/threefoldtech/zos4/pkg/stubs"
Expand Down Expand Up @@ -1022,8 +1024,15 @@ func (n *NativeEngine) CreateOrUpdate(twin uint32, deployment gridtypes.Deployme
}

// make sure the account used is verified
if getTwinVerificationState(twin) != "VERIFIED" {
return fmt.Errorf("user is not verified")
check := func() error {
if !isTwinVerified(twin) {
return fmt.Errorf("user is not verified")
}
return nil
}

if err := backoff.Retry(check, backoff.WithMaxRetries(backoff.NewExponentialBackOff(), 5)); err != nil {
return err
}

if err := deployment.Verify(n.twins); err != nil {
Expand Down Expand Up @@ -1180,18 +1189,23 @@ func (e *NativeEngine) GetWorkloadStatus(id string) (gridtypes.ResultState, bool
return wl.Result.State, true, nil
}

// getTwinVerificationState make sure the account used is verified we have the user public key in bytes(pkBytes)
func getTwinVerificationState(twinID uint32) (status string) {
verificationServiceURL := "https://kyc1.gent01.dev.grid.tf/api/v1/status"
status = "FAILED"
// isTwinVerified make sure the account used is verified
func isTwinVerified(twinID uint32) (verified bool) {
const verifiedStatus = "VERIFIED"
env := environment.MustGet()

verificationServiceURL, err := url.JoinPath(env.KycURL, "/api/v1/status")
if err != nil {
return
}

request, err := http.NewRequest(http.MethodGet, verificationServiceURL, nil)
if err != nil {
return
}

q := request.URL.Query()
q.Set("twinID", fmt.Sprint(twinID))
q.Set("twin_id", fmt.Sprint(twinID))
request.URL.RawQuery = q.Encode()

cl := &http.Client{
Expand All @@ -1204,21 +1218,17 @@ func getTwinVerificationState(twinID uint32) (status string) {
}
defer response.Body.Close()

body, err := io.ReadAll(response.Body)
if err != nil {
if response.StatusCode != http.StatusOK {
log.Error().Msg("failed to get user status")
return
}

bodyMap := map[string]string{}
err = json.Unmarshal(body, &bodyMap)
if err != nil {
return
}
var result struct{ Result struct{ Status string } }

if response.StatusCode != http.StatusOK {
log.Error().Msgf("failed to verify user status: %s", bodyMap["error"])
err = json.NewDecoder(response.Body).Decode(&result)
if err != nil {
return
}

return bodyMap["status"]
return result.Result.Status == verifiedStatus
}

0 comments on commit 9495238

Please sign in to comment.