60 Zeilen
1.6 KiB
Go
60 Zeilen
1.6 KiB
Go
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
|
|
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{}) {
|
|
if key == AccountAnon {
|
|
return true
|
|
}
|
|
return
|
|
}
|
|
|
|
// List return only AccountAnon as the only Listitem
|
|
func (*AnonAccount) List() []AccountConstant {
|
|
return []AccountConstant{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)
|
|
}
|