Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix/auth binding #1166

Merged
merged 4 commits into from Aug 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions core/sys/sign.go
Expand Up @@ -12,3 +12,5 @@ type SignFunc func(hash string, signatureScheme string, keys []KeyPair) (string,
type VerifyFunc func(signature string, msg string) (bool, error)

type VerifyWithFunc func(pk, signature string, msg string) (bool, error)

type AuthorizeFunc func(msg string) (string, error)
2 changes: 2 additions & 0 deletions core/sys/vars.go
Expand Up @@ -21,4 +21,6 @@ var (

// Verify verify method. it should be initialized on different platform.
VerifyWith VerifyWithFunc

Authorize AuthorizeFunc
)
18 changes: 17 additions & 1 deletion mobilesdk/sdk/sdk.go
Expand Up @@ -7,11 +7,13 @@ import (
"encoding/base64"
"encoding/json"
"fmt"
"github.com/pkg/errors"
"math"
"strconv"
"strings"

"github.com/0chain/gosdk/core/sys"
"github.com/pkg/errors"

"github.com/0chain/gosdk/core/util"
"github.com/0chain/gosdk/core/version"
"github.com/0chain/gosdk/zboxcore/client"
Expand All @@ -25,6 +27,10 @@ import (

var nonce = int64(0)

type Autorizer interface {
Auth(msg string) (string, error)
}

// ChainConfig - blockchain config
type ChainConfig struct {
ChainID string `json:"chain_id,omitempty"`
Expand Down Expand Up @@ -414,3 +420,13 @@ func decodeTicket(ticket string) (string, string, uint64, error) {
s, _ := strconv.ParseFloat(string(fmt.Sprintf("%v", lock)), 64)
return string(recipientPublicKey), string(markerStr), zcncore.ConvertTokenToSAS(s), nil
}

// Client can extend interface and fass implementation to this register like this
// public class Autorizer extends Pkg.Autorizer {
// public void Auth() {
// // do something here
// }
// }
func RegisterAuthorizer(auth Autorizer) {
sys.Authorize = auth.Auth
}
25 changes: 10 additions & 15 deletions zcncore/transactionauth_base.go
Expand Up @@ -2,13 +2,10 @@ package zcncore

import (
"encoding/json"
"fmt"
"net/http"
"strconv"

"github.com/0chain/errors"
"github.com/0chain/gosdk/core/sys"
"github.com/0chain/gosdk/core/transaction"
"github.com/0chain/gosdk/core/util"
"github.com/0chain/gosdk/core/zcncrypto"
)

Expand Down Expand Up @@ -38,23 +35,21 @@ func (ta *TransactionWithAuth) getAuthorize() (*transaction.Transaction, error)
return nil, errors.Wrap(err, "signing error.")
}

req, err := util.NewHTTPPostRequest(_config.authUrl+"/transaction", ta.t.txn)
jsonByte, err := json.Marshal(ta.t.txn)
if err != nil {
return nil, errors.Wrap(err, "new post request failed for auth")
return nil, err
}
res, err := req.Post()

if sys.Authorize == nil {
return nil, errors.New("not_initialized", "no authorize func is set, define it in native code and set in sys")
}
authorize, err := sys.Authorize(string(jsonByte))
if err != nil {
return nil, errNetwork
return nil, err
}

if res.StatusCode != http.StatusOK {
if res.StatusCode == http.StatusUnauthorized {
return nil, errUserRejected
}
return nil, errors.New(strconv.Itoa(res.StatusCode), fmt.Sprintf("auth error: %v. %v", res.Status, res.Body))
}
var txnResp transaction.Transaction
err = json.Unmarshal([]byte(res.Body), &txnResp)
err = json.Unmarshal([]byte(authorize), &txnResp)
if err != nil {
return nil, errors.Wrap(err, "invalid json on auth response.")
}
Expand Down