github.com/annwntech/go-micro/v2@v2.9.5/auth/default.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  type noop struct {
    24  	opts Options
    25  }
    26  
    27  // String returns the name of the implementation
    28  func (n *noop) String() string {
    29  	return "noop"
    30  }
    31  
    32  // Init the auth
    33  func (n *noop) Init(opts ...Option) {
    34  	for _, o := range opts {
    35  		o(&n.opts)
    36  	}
    37  }
    38  
    39  // Options set for auth
    40  func (n *noop) Options() Options {
    41  	return n.opts
    42  }
    43  
    44  // Generate a new account
    45  func (n *noop) Generate(id string, opts ...GenerateOption) (*Account, error) {
    46  	options := NewGenerateOptions(opts...)
    47  
    48  	return &Account{
    49  		ID:       id,
    50  		Secret:   options.Secret,
    51  		Metadata: options.Metadata,
    52  		Scopes:   options.Scopes,
    53  		Issuer:   n.Options().Issuer,
    54  	}, nil
    55  }
    56  
    57  // Grant access to a resource
    58  func (n *noop) Grant(rule *Rule) error {
    59  	return nil
    60  }
    61  
    62  // Revoke access to a resource
    63  func (n *noop) Revoke(rule *Rule) error {
    64  	return nil
    65  }
    66  
    67  // Rules used to verify requests
    68  func (n *noop) Rules(opts ...RulesOption) ([]*Rule, error) {
    69  	return []*Rule{}, nil
    70  }
    71  
    72  // Verify an account has access to a resource
    73  func (n *noop) Verify(acc *Account, res *Resource, opts ...VerifyOption) error {
    74  	return nil
    75  }
    76  
    77  // Inspect a token
    78  func (n *noop) Inspect(token string) (*Account, error) {
    79  	return &Account{ID: uuid.New().String(), Issuer: n.Options().Issuer}, nil
    80  }
    81  
    82  // Token generation using an account id and secret
    83  func (n *noop) Token(opts ...TokenOption) (*AuthToken, error) {
    84  	return &AuthToken{}, nil
    85  }