2021-01-09 20:39:05 +00:00
|
|
|
package auth
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
|
|
|
|
|
|
|
"github.com/gin-gonic/gin"
|
2022-11-05 07:31:12 +00:00
|
|
|
"go.sebtobie.de/httpserver/constants"
|
2021-01-09 20:39:05 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// 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 {
|
2022-11-06 09:52:24 +00:00
|
|
|
Get(constants.AccountConstant) any
|
2022-11-05 07:31:12 +00:00
|
|
|
List() []constants.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
|
2022-11-06 09:52:24 +00:00
|
|
|
func (*AnonAccount) Get(key constants.AccountConstant) (in any) {
|
2022-11-05 07:31:12 +00:00
|
|
|
if key == constants.AccountAnon {
|
2021-01-09 20:39:05 +00:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// List return only AccountAnon as the only Listitem
|
2022-11-05 07:31:12 +00:00
|
|
|
func (*AnonAccount) List() []constants.AccountConstant {
|
|
|
|
return []constants.AccountConstant{constants.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)
|
|
|
|
}
|