1
0
Fork 0

moved the constants from auth to contatnts

Dieser Commit ist enthalten in:
Sebastian Tobie 2022-11-05 08:31:12 +01:00
Ursprung 9c6feec4ea
Commit 044721ac8e
5 geänderte Dateien mit 28 neuen und 39 gelöschten Zeilen

Datei anzeigen

@ -4,6 +4,7 @@ import (
"net/http" "net/http"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
"go.sebtobie.de/httpserver/constants"
) )
// AuthenticationHandler is an interface that is used to give the account of the request. // AuthenticationHandler is an interface that is used to give the account of the request.
@ -17,8 +18,8 @@ type AuthenticationHandler interface {
// Account is an interface that gives the application access to infos about the user. // Account is an interface that gives the application access to infos about the user.
type Account interface { type Account interface {
Get(AccountConstant) interface{} Get(constants.AccountConstant) interface{}
List() []AccountConstant List() []constants.AccountConstant
Anonymous() bool Anonymous() bool
Redirect(c *gin.Context) Redirect(c *gin.Context)
} }
@ -36,16 +37,16 @@ func (*AnonAccountHandler) Account(*gin.Context) Account {
type AnonAccount struct{} type AnonAccount struct{}
// Get returns only AccountAnon = true // Get returns only AccountAnon = true
func (*AnonAccount) Get(key AccountConstant) (in interface{}) { func (*AnonAccount) Get(key constants.AccountConstant) (in interface{}) {
if key == AccountAnon { if key == constants.AccountAnon {
return true return true
} }
return return
} }
// List return only AccountAnon as the only Listitem // List return only AccountAnon as the only Listitem
func (*AnonAccount) List() []AccountConstant { func (*AnonAccount) List() []constants.AccountConstant {
return []AccountConstant{AccountAnon} return []constants.AccountConstant{constants.AccountAnon}
} }
// Anonymous is always true // Anonymous is always true

Datei anzeigen

@ -1,14 +0,0 @@
package auth
// AccountConstant is an type for easier linting.
type AccountConstant string
// These are Constants to save specific attributes in single points of use.
const (
// AccountID is the ID of the session. Prefferably it should be an UUIDv4 to mitigate security errors.
AccountID AccountConstant = "jti"
// AccountAnon is to identify Sessions as Anonymous sessions.
AccountAnon AccountConstant = "anon"
// AccountUser is an attribute that identifies the user with an string that is unique for the user, for Example the username.
AccountUser AccountConstant = "uid"
)

Datei anzeigen

@ -5,7 +5,7 @@ import (
"testing" "testing"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
"go.sebtobie.de/httpserver/auth" "go.sebtobie.de/httpserver/constants"
"go.sebtobie.de/httpserver/menus" "go.sebtobie.de/httpserver/menus"
) )
@ -17,12 +17,12 @@ func (a *account) Anonymous() bool {
return !a.auth return !a.auth
} }
func (*account) Get(auth.AccountConstant) interface{} { func (*account) Get(constants.AccountConstant) interface{} {
return nil return nil
} }
func (*account) List() []auth.AccountConstant { func (*account) List() []constants.AccountConstant {
return []auth.AccountConstant{} return []constants.AccountConstant{}
} }
func (*account) Redirect(*gin.Context) { func (*account) Redirect(*gin.Context) {

Datei anzeigen

@ -9,14 +9,15 @@ import (
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
"github.com/golang-jwt/jwt/v4" "github.com/golang-jwt/jwt/v4"
"github.com/google/uuid" "github.com/google/uuid"
"github.com/phuslu/log" "github.com/rs/zerolog/log"
"go.sebtobie.de/httpserver/auth" "go.sebtobie.de/httpserver/auth"
"go.sebtobie.de/httpserver/constants"
) )
var defaccount = &account{ var defaccount = &account{
data: map[auth.AccountConstant]interface{}{ data: map[constants.AccountConstant]interface{}{
auth.AccountID: uuid.Nil, constants.AccountID: uuid.Nil,
auth.AccountAnon: true, constants.AccountAnon: true,
}, },
} }
@ -46,7 +47,7 @@ func (s *SAML) Account(c *gin.Context) auth.Account {
if claim, ok = token.Claims.(*jwt.MapClaims); ok && token.Valid { if claim, ok = token.Claims.(*jwt.MapClaims); ok && token.Valid {
log.Debug().Interface("claim", claim).Msg("Got valid token") log.Debug().Interface("claim", claim).Msg("Got valid token")
for key, value := range *claim { for key, value := range *claim {
acc.data[auth.AccountConstant(key)] = value acc.data[constants.AccountConstant(key)] = value
} }
return acc return acc
} }
@ -63,18 +64,18 @@ func (s *SAML) signingkey(token *jwt.Token) (key interface{}, err error) {
type account struct { type account struct {
s *SAML s *SAML
data map[auth.AccountConstant]interface{} data map[constants.AccountConstant]interface{}
} }
func (a *account) Anonymous() bool { func (a *account) Anonymous() bool {
return a.data[auth.AccountAnon].(bool) return a.data[constants.AccountAnon].(bool)
} }
func (a *account) Redirect(c *gin.Context) { func (a *account) Redirect(c *gin.Context) {
id := uuid.New().String() id := uuid.New().String()
tokenstring, err := jwttoken(jwt.MapClaims{ tokenstring, err := jwttoken(jwt.MapClaims{
string(auth.AccountID): id, string(constants.AccountID): id,
string(auth.AccountAnon): true, string(constants.AccountAnon): true,
}, a.s.jwtprivatekey) }, a.s.jwtprivatekey)
if err != nil { if err != nil {
log.Error().Err(err).Msg("Failed to generate the token") log.Error().Err(err).Msg("Failed to generate the token")
@ -102,12 +103,12 @@ func (a *account) Redirect(c *gin.Context) {
c.Redirect(http.StatusSeeOther, u.String()) c.Redirect(http.StatusSeeOther, u.String())
} }
func (a *account) Get(key auth.AccountConstant) interface{} { func (a *account) Get(key constants.AccountConstant) interface{} {
return a.data[key] return a.data[key]
} }
func (a *account) List() []auth.AccountConstant { func (a *account) List() []constants.AccountConstant {
var liste []auth.AccountConstant var liste []constants.AccountConstant
for key := range a.data { for key := range a.data {
liste = append(liste, key) liste = append(liste, key)
} }

Datei anzeigen

@ -18,6 +18,7 @@ import (
"github.com/rs/zerolog/log" "github.com/rs/zerolog/log"
"go.sebtobie.de/httpserver" "go.sebtobie.de/httpserver"
"go.sebtobie.de/httpserver/auth" "go.sebtobie.de/httpserver/auth"
"go.sebtobie.de/httpserver/constants"
) )
func musturi(url *url.URL, err error) *url.URL { func musturi(url *url.URL, err error) *url.URL {
@ -186,9 +187,9 @@ func (s *SAML) acsHF(c *gin.Context) {
} }
data := attributeStatementstomap(assert.AttributeStatements) data := attributeStatementstomap(assert.AttributeStatements)
token, err := jwttoken(jwt.MapClaims{ token, err := jwttoken(jwt.MapClaims{
string(auth.AccountAnon): false, string(constants.AccountAnon): false,
string(auth.AccountID): account.Get(auth.AccountID).(string), string(constants.AccountID): account.Get(constants.AccountID).(string),
string(auth.AccountUser): data["uid"][0], string(constants.AccountUser): data["uid"][0],
}, s.jwtprivatekey) }, s.jwtprivatekey)
if err != nil { if err != nil {
c.AbortWithStatus(http.StatusInternalServerError) c.AbortWithStatus(http.StatusInternalServerError)