github.com/germla/auth@v0.0.0-20230505173936-bc9a4159cf17/authority/authority.go (about)

     1  package authority
     2  
     3  import (
     4  	"html/template"
     5  	"net/http"
     6  
     7  	"github.com/germla/auth"
     8  	"github.com/qor/middlewares"
     9  	"github.com/qor/roles"
    10  	"github.com/qor/session"
    11  )
    12  
    13  var (
    14  	// AccessDeniedFlashMessage access denied message
    15  	AccessDeniedFlashMessage = template.HTML("Access Denied!")
    16  )
    17  
    18  // Authority authority struct
    19  type Authority struct {
    20  	*Config
    21  }
    22  
    23  // AuthInterface auth interface
    24  type AuthInterface interface {
    25  	auth.SessionStorerInterface
    26  	GetCurrentUser(req *http.Request) interface{}
    27  }
    28  
    29  // Config authority config
    30  type Config struct {
    31  	Auth                AuthInterface
    32  	Role                *roles.Role
    33  	AccessDeniedHandler func(w http.ResponseWriter, req *http.Request)
    34  }
    35  
    36  // New initialize Authority
    37  func New(config *Config) *Authority {
    38  	if config == nil {
    39  		config = &Config{}
    40  	}
    41  
    42  	if config.Auth == nil {
    43  		panic("Auth should not be nil for Authority")
    44  	}
    45  
    46  	if config.Role == nil {
    47  		config.Role = roles.Global
    48  	}
    49  
    50  	if config.AccessDeniedHandler == nil {
    51  		config.AccessDeniedHandler = NewAccessDeniedHandler(config.Auth, "/")
    52  	}
    53  
    54  	authority := &Authority{Config: config}
    55  
    56  	middlewares.Use(middlewares.Middleware{
    57  		Name:        "authority",
    58  		InsertAfter: []string{"session"},
    59  		Handler: func(handler http.Handler) http.Handler {
    60  			return authority.Middleware(handler)
    61  		},
    62  	})
    63  	return authority
    64  }
    65  
    66  // Authorize authorize specfied roles or authenticated user to access wrapped handler
    67  func (authority *Authority) Authorize(roles ...string) func(http.Handler) http.Handler {
    68  	return func(handler http.Handler) http.Handler {
    69  		return http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
    70  			var currentUser interface{}
    71  
    72  			// Get current user from request
    73  			currentUser = authority.Auth.GetCurrentUser(req)
    74  
    75  			if (len(roles) == 0 && currentUser != nil) || authority.Role.HasRole(req, currentUser, roles...) {
    76  				handler.ServeHTTP(w, req)
    77  				return
    78  			}
    79  
    80  			authority.AccessDeniedHandler(w, req)
    81  		})
    82  	}
    83  }
    84  
    85  // NewAccessDeniedHandler new access denied handler
    86  func NewAccessDeniedHandler(Auth AuthInterface, redirectPath string) func(http.ResponseWriter, *http.Request) {
    87  	return func(w http.ResponseWriter, req *http.Request) {
    88  		Auth.Flash(w, req, session.Message{Message: AccessDeniedFlashMessage})
    89  		http.Redirect(w, req, redirectPath, http.StatusSeeOther)
    90  	}
    91  }