gitee.com/h79/goutils@v1.22.10/auth/route.go (about)

     1  package auth
     2  
     3  import (
     4  	"gitee.com/h79/goutils/auth/token"
     5  	"gitee.com/h79/goutils/common/result"
     6  )
     7  
     8  type authMap map[string]token.Authenticate
     9  
    10  var (
    11  	_ token.Route = (*route)(nil)
    12  )
    13  
    14  type route struct {
    15  	auths authMap
    16  }
    17  
    18  func (auth *route) HasAuth(method string) bool {
    19  	_, ok := auth.auths[method]
    20  	return ok
    21  }
    22  
    23  func (auth *route) Use(a token.Authenticate) token.Route {
    24  	auth.auths[a.Type()] = a
    25  	return auth
    26  }
    27  
    28  func (auth *route) Auth(method string) (token.Authenticate, error) {
    29  	if len(auth.auths) <= 0 {
    30  		return nil, result.Error(result.ErrNil, "Not exist auth")
    31  	}
    32  	item, ok := auth.auths[method]
    33  	if !ok {
    34  		return nil, result.RErrNotFound
    35  	}
    36  	return item, nil
    37  }