OSDN Git Service

Dev wallet sa (#1238)
[bytom/bytom-spv.git] / net / http / authn / authn.go
index 923466c..8f5c3d3 100644 (file)
@@ -9,7 +9,7 @@ import (
        "sync"
        "time"
 
-       "github.com/bytom/blockchain/accesstoken"
+       "github.com/bytom/accesstoken"
        "github.com/bytom/errors"
 )
 
@@ -18,7 +18,7 @@ const tokenExpiry = time.Minute * 5
 var loopbackOn = true
 
 var (
-       //ErrInvalidToken is returned when authenticate is called with invalide token.
+       //ErrInvalidToken is returned when authenticate is called with invalid token.
        ErrInvalidToken = errors.New("invalid token")
        //ErrNoToken is returned when authenticate is called with no token.
        ErrNoToken = errors.New("no token")
@@ -35,7 +35,6 @@ type API struct {
 }
 
 type tokenResult struct {
-       valid      bool
        lastLookup time.Time
 }
 
@@ -71,6 +70,10 @@ func (a *API) Authenticate(req *http.Request) (*http.Request, error) {
        if strings.HasPrefix(req.URL.Path, "/dashboard/") || req.URL.Path == "/dashboard" {
                return req.WithContext(ctx), nil
        }
+       // Adding this workaround for Equity Playground.
+       if strings.HasPrefix(req.URL.Path, "/equity/") || req.URL.Path == "/equity" {
+               return req.WithContext(ctx), nil
+       }
        if loopbackOn && local {
                return req.WithContext(ctx), nil
        }
@@ -134,20 +137,17 @@ func (a *API) tokenAuthn(req *http.Request) (string, error) {
 
 func (a *API) cachedTokenAuthnCheck(ctx context.Context, user, pw string) error {
        a.tokenMu.Lock()
-       res, ok := a.tokenMap[user + pw]
+       res, ok := a.tokenMap[user+pw]
        a.tokenMu.Unlock()
        if !ok || time.Now().After(res.lastLookup.Add(tokenExpiry)) {
-               valid, err := a.tokens.Check(ctx, user, pw)
+               err := a.tokens.Check(user, pw)
                if err != nil {
-                       return errors.Wrap(err)
+                       return ErrInvalidToken
                }
-               res = tokenResult{valid: valid, lastLookup: time.Now()}
+               res = tokenResult{lastLookup: time.Now()}
                a.tokenMu.Lock()
-               a.tokenMap[user + pw] = res
+               a.tokenMap[user+pw] = res
                a.tokenMu.Unlock()
        }
-       if !res.valid {
-               return ErrInvalidToken
-       }
        return nil
 }