1
0
Fork 0
httpserver/auth/auth.go

61 Zeilen
1.7 KiB
Go

package auth
import (
"net/http"
"github.com/gin-gonic/gin"
"go.sebtobie.de/httpserver/constants"
)
// AuthenticationHandler is an interface that is used to give the account of the request.
// it is set into the context using gin.Context.Set.
// The context must be reused for the redirect.
// Account NEVER returns nil.
// Not loggedin Users have return Anonymous() = true and get() = nil and List() = []string{}
type AuthenticationHandler interface {
Account(*gin.Context) Account
}
// Account is an interface that gives the application access to infos about the user.
type Account interface {
Get(constants.AccountConstant) any
List() []constants.AccountConstant
Anonymous() bool
Redirect(c *gin.Context)
}
// AnonAccountHandler is an simple struct that fullfills the AuthenticationHandler Interface
type AnonAccountHandler struct{}
// Account is an simple method that returns an Account that is always anonymous.
func (*AnonAccountHandler) Account(*gin.Context) Account {
return &AnonAccount{}
}
// AnonAccount is an simple Account-interface implementation.
// It is always Anonymous
type AnonAccount struct{}
// Get returns only AccountAnon = true
func (*AnonAccount) Get(key constants.AccountConstant) (in any) {
if key == constants.AccountAnon {
return true
}
return
}
// List return only AccountAnon as the only Listitem
func (*AnonAccount) List() []constants.AccountConstant {
return []constants.AccountConstant{constants.AccountAnon}
}
// Anonymous is always true
func (*AnonAccount) Anonymous() bool {
return true
}
// Redirect should point to an login, but since its not possible for this handler it sends an 401 Page
func (*AnonAccount) Redirect(c *gin.Context) {
c.AbortWithStatus(http.StatusForbidden)
}