OSDN Git Service

fix accesstoken check api
authorYongfeng LI <wliyongfeng@gmail.com>
Mon, 15 Jan 2018 06:39:26 +0000 (14:39 +0800)
committerYongfeng LI <wliyongfeng@gmail.com>
Mon, 15 Jan 2018 06:39:26 +0000 (14:39 +0800)
blockchain/accesstoken/accesstoken.go
blockchain/accesstoken/accesstoken_test.go
blockchain/accesstokens.go
net/http/authn/authn.go

index 9929c6f..ef12e7f 100644 (file)
@@ -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.
index c925e69..f789393 100644 (file)
@@ -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)
index ab7af0f..153e559 100644 (file)
@@ -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)
 }
index 0a6d9e6..77790d6 100644 (file)
@@ -3,7 +3,6 @@ package authn
 import (
        "context"
        "crypto/x509"
-       "encoding/hex"
        "net"
        "net/http"
        "strings"