From: Yongfeng LI Date: Mon, 15 Jan 2018 06:39:26 +0000 (+0800) Subject: fix accesstoken check api X-Git-Tag: v1.0.5~360^2~1 X-Git-Url: http://git.osdn.net/view?a=commitdiff_plain;h=2d6c7a228aa5b1f7a1261461f2b04c3b1906fac6;p=bytom%2Fbytom.git fix accesstoken check api --- diff --git a/blockchain/accesstoken/accesstoken.go b/blockchain/accesstoken/accesstoken.go index 9929c6fe..ef12e7f3 100644 --- a/blockchain/accesstoken/accesstoken.go +++ b/blockchain/accesstoken/accesstoken.go @@ -55,7 +55,7 @@ func NewStore(db dbm.DB) *CredentialStore { } // Create generates a new access token with the given ID. -func (cs *CredentialStore) Create(ctx context.Context, id, typ string) (*string, error) { +func (cs *CredentialStore) Create(ctx context.Context, id, typ string) (*Token, error) { if !validIDRegexp.MatchString(id) { return nil, errors.WithDetailf(ErrBadID, "invalid id %q", id) } @@ -86,7 +86,7 @@ func (cs *CredentialStore) Create(ctx context.Context, id, typ string) (*string, } cs.DB.Set(key, value) - return &token.Token, nil + return token, nil } // Check returns whether or not an id-secret pair is a valid access token. diff --git a/blockchain/accesstoken/accesstoken_test.go b/blockchain/accesstoken/accesstoken_test.go index c925e690..f7893930 100644 --- a/blockchain/accesstoken/accesstoken_test.go +++ b/blockchain/accesstoken/accesstoken_test.go @@ -42,7 +42,7 @@ func TestList(t *testing.T) { defer os.RemoveAll("temp") cs := NewStore(testDB) - tokenMap := make(map[string]*string) + tokenMap := make(map[string]*Token) tokenMap["ab"] = mustCreateToken(ctx, t, cs, "ab", "test") tokenMap["bc"] = mustCreateToken(ctx, t, cs, "bc", "test") tokenMap["cd"] = mustCreateToken(ctx, t, cs, "cd", "test") @@ -56,7 +56,7 @@ func TestList(t *testing.T) { t.Error("List errored: get invalid length") } for _, v := range got { - if m := strings.Compare(v.Token, *tokenMap[v.ID]); m != 0 { + if m := strings.Compare(v.Token, tokenMap[v.ID].Token); m != 0 { t.Errorf("List error: ID: %s, expected token: %s, DB token: %s", v.ID, *tokenMap[v.ID], v.Token) } continue @@ -70,7 +70,7 @@ func TestCheck(t *testing.T) { cs := NewStore(testDB) token := mustCreateToken(ctx, t, cs, "x", "client") - tokenParts := strings.Split(*token, ":") + tokenParts := strings.Split(token.Token, ":") valid, err := cs.Check(ctx, tokenParts[0], tokenParts[1]) if err != nil { @@ -121,7 +121,7 @@ func TestDeleteWithInvalidId(t *testing.T) { } } -func mustCreateToken(ctx context.Context, t *testing.T, cs *CredentialStore, id, typ string) *string { +func mustCreateToken(ctx context.Context, t *testing.T, cs *CredentialStore, id, typ string) *Token { token, err := cs.Create(ctx, id, typ) if err != nil { t.Fatal(err) diff --git a/blockchain/accesstokens.go b/blockchain/accesstokens.go index ab7af0f2..153e5597 100644 --- a/blockchain/accesstokens.go +++ b/blockchain/accesstokens.go @@ -2,7 +2,6 @@ package blockchain import ( "context" - "encoding/hex" log "github.com/sirupsen/logrus" @@ -17,20 +16,19 @@ func (bcr *BlockchainReactor) createAccessToken(ctx context.Context, x struct { }) Response { token, err := bcr.accessTokens.Create(ctx, x.ID, x.Type) if err != nil { - return resWrapper(nil, err) + return NewErrorResponse(err) } - data := map[string]*string{"accessToken": token} - return resWrapper(data) + return NewSuccessResponse(token) } func (bcr *BlockchainReactor) listAccessTokens(ctx context.Context) Response { tokens, err := bcr.accessTokens.List(ctx) if err != nil { log.Errorf("listAccessTokens: %v", err) - return resWrapper(nil, err) + return NewErrorResponse(err) } - return resWrapper(tokens) + return NewSuccessResponse(tokens) } func (bcr *BlockchainReactor) deleteAccessToken(ctx context.Context, x struct { @@ -48,14 +46,9 @@ func (bcr *BlockchainReactor) checkAccessToken(ctx context.Context, x struct { ID string `json:"id"` Secret string `json:"secret"` }) Response { - secret, err := hex.DecodeString(x.Secret) - if err != nil { - return resWrapper(nil, err) - } - _, err = bcr.accessTokens.Check(ctx, x.ID, secret) - if err != nil { - return resWrapper(nil, err) + if _, err := bcr.accessTokens.Check(ctx, x.ID, x.Secret); err != nil { + return NewErrorResponse(err) } - return resWrapper(nil) + return NewSuccessResponse(nil) } diff --git a/net/http/authn/authn.go b/net/http/authn/authn.go index 0a6d9e62..77790d6f 100644 --- a/net/http/authn/authn.go +++ b/net/http/authn/authn.go @@ -3,7 +3,6 @@ package authn import ( "context" "crypto/x509" - "encoding/hex" "net" "net/http" "strings"