1
0
Fork 0
httpserver/auth/auth.go

60 Zeilen
1.6 KiB
Go

2021-01-09 20:39:05 +00:00
package auth
import (
"net/http"
"github.com/gin-gonic/gin"
)
// 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(AccountConstant) interface{}
List() []AccountConstant
2021-01-09 20:39:05 +00:00
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 AccountConstant) (in interface{}) {
2021-01-09 20:39:05 +00:00
if key == AccountAnon {
return true
}
return
}
// List return only AccountAnon as the only Listitem
func (*AnonAccount) List() []AccountConstant {
return []AccountConstant{AccountAnon}
2021-01-09 20:39:05 +00:00
}
// 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)
}