github.com/wrgl/wrgl@v0.14.0/pkg/auth/test/authz.go (about)

     1  // SPDX-License-Identifier: Apache-2.0
     2  // Copyright © 2022 Wrangle Ltd
     3  
     4  package authtest
     5  
     6  import (
     7  	"net/http"
     8  )
     9  
    10  type AuthzStore struct {
    11  	m map[string][]string
    12  }
    13  
    14  func NewAuthzStore() *AuthzStore {
    15  	return &AuthzStore{
    16  		m: map[string][]string{},
    17  	}
    18  }
    19  
    20  func (s *AuthzStore) AddPolicy(email, scope string) error {
    21  	s.m[email] = append(s.m[email], scope)
    22  	return nil
    23  }
    24  
    25  func (s *AuthzStore) RemovePolicy(email, scope string) error {
    26  	for i, v := range s.m[email] {
    27  		if v == scope {
    28  			if i < len(s.m[email])-1 {
    29  				s.m[email] = append(s.m[email][:i], s.m[email][i+1:]...)
    30  			} else {
    31  				s.m[email] = s.m[email][:i]
    32  			}
    33  			break
    34  		}
    35  	}
    36  	return nil
    37  }
    38  
    39  func (s *AuthzStore) Authorized(r *http.Request, email, scope string) (bool, error) {
    40  	if sl, ok := s.m[email]; ok {
    41  		for _, v := range sl {
    42  			if v == scope {
    43  				return true, nil
    44  			}
    45  		}
    46  	}
    47  	return false, nil
    48  }
    49  
    50  func (s *AuthzStore) Flush() error {
    51  	return nil
    52  }
    53  
    54  func (s *AuthzStore) ListPolicies(email string) (scopes []string, err error) {
    55  	if sl, ok := s.m[email]; ok {
    56  		return sl, nil
    57  	}
    58  	return nil, nil
    59  }