gitee.com/sasukebo/go-micro/v4@v4.7.1/auth/noop.go (about)

     1  package auth
     2  
     3  import (
     4  	"github.com/google/uuid"
     5  )
     6  
     7  var (
     8  	DefaultAuth = NewAuth()
     9  )
    10  
    11  func NewAuth(opts ...Option) Auth {
    12  	options := Options{}
    13  
    14  	for _, o := range opts {
    15  		o(&options)
    16  	}
    17  
    18  	return &noop{
    19  		opts: options,
    20  	}
    21  }
    22  
    23  func NewRules() Rules {
    24  	return new(noopRules)
    25  }
    26  
    27  type noop struct {
    28  	opts Options
    29  }
    30  
    31  type noopRules struct{}
    32  
    33  // String returns the name of the implementation
    34  func (n *noop) String() string {
    35  	return "noop"
    36  }
    37  
    38  // Init the auth
    39  func (n *noop) Init(opts ...Option) {
    40  	for _, o := range opts {
    41  		o(&n.opts)
    42  	}
    43  }
    44  
    45  // Options set for auth
    46  func (n *noop) Options() Options {
    47  	return n.opts
    48  }
    49  
    50  // Generate a new account
    51  func (n *noop) Generate(id string, opts ...GenerateOption) (*Account, error) {
    52  	options := NewGenerateOptions(opts...)
    53  
    54  	return &Account{
    55  		ID:       id,
    56  		Secret:   options.Secret,
    57  		Metadata: options.Metadata,
    58  		Scopes:   options.Scopes,
    59  		Issuer:   n.Options().Namespace,
    60  	}, nil
    61  }
    62  
    63  // Grant access to a resource
    64  func (n *noopRules) Grant(rule *Rule) error {
    65  	return nil
    66  }
    67  
    68  // Revoke access to a resource
    69  func (n *noopRules) Revoke(rule *Rule) error {
    70  	return nil
    71  }
    72  
    73  // Rules used to verify requests
    74  // Verify an account has access to a resource
    75  func (n *noopRules) Verify(acc *Account, res *Resource, opts ...VerifyOption) error {
    76  	return nil
    77  }
    78  
    79  func (n *noopRules) List(opts ...ListOption) ([]*Rule, error) {
    80  	return []*Rule{}, nil
    81  }
    82  
    83  // Inspect a token
    84  func (n *noop) Inspect(token string) (*Account, error) {
    85  	return &Account{ID: uuid.New().String(), Issuer: n.Options().Namespace}, nil
    86  }
    87  
    88  // Token generation using an account id and secret
    89  func (n *noop) Token(opts ...TokenOption) (*Token, error) {
    90  	return &Token{}, nil
    91  }