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"
"github.com/gin-gonic/gin"
"go.sebtobie.de/httpserver/constants"
)
// 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.
type Account interface {
Get(AccountConstant) interface{}
List() []AccountConstant
Get(constants.AccountConstant) interface{}
List() []constants.AccountConstant
Anonymous() bool
Redirect(c *gin.Context)
}
@ -36,16 +37,16 @@ func (*AnonAccountHandler) Account(*gin.Context) Account {
type AnonAccount struct{}
// Get returns only AccountAnon = true
func (*AnonAccount) Get(key AccountConstant) (in interface{}) {
if key == AccountAnon {
func (*AnonAccount) Get(key constants.AccountConstant) (in interface{}) {
if key == constants.AccountAnon {
return true
}
return
}
// List return only AccountAnon as the only Listitem
func (*AnonAccount) List() []AccountConstant {
return []AccountConstant{AccountAnon}
func (*AnonAccount) List() []constants.AccountConstant {
return []constants.AccountConstant{constants.AccountAnon}
}
// 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"
"github.com/gin-gonic/gin"
"go.sebtobie.de/httpserver/auth"
"go.sebtobie.de/httpserver/constants"
"go.sebtobie.de/httpserver/menus"
)
@ -17,12 +17,12 @@ func (a *account) Anonymous() bool {
return !a.auth
}
func (*account) Get(auth.AccountConstant) interface{} {
func (*account) Get(constants.AccountConstant) interface{} {
return nil
}
func (*account) List() []auth.AccountConstant {
return []auth.AccountConstant{}
func (*account) List() []constants.AccountConstant {
return []constants.AccountConstant{}
}
func (*account) Redirect(*gin.Context) {

Datei anzeigen

@ -9,14 +9,15 @@ import (
"github.com/gin-gonic/gin"
"github.com/golang-jwt/jwt/v4"
"github.com/google/uuid"
"github.com/phuslu/log"
"github.com/rs/zerolog/log"
"go.sebtobie.de/httpserver/auth"
"go.sebtobie.de/httpserver/constants"
)
var defaccount = &account{
data: map[auth.AccountConstant]interface{}{
auth.AccountID: uuid.Nil,
auth.AccountAnon: true,
data: map[constants.AccountConstant]interface{}{
constants.AccountID: uuid.Nil,
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 {
log.Debug().Interface("claim", claim).Msg("Got valid token")
for key, value := range *claim {
acc.data[auth.AccountConstant(key)] = value
acc.data[constants.AccountConstant(key)] = value
}
return acc
}
@ -63,18 +64,18 @@ func (s *SAML) signingkey(token *jwt.Token) (key interface{}, err error) {
type account struct {
s *SAML
data map[auth.AccountConstant]interface{}
data map[constants.AccountConstant]interface{}
}
func (a *account) Anonymous() bool {
return a.data[auth.AccountAnon].(bool)
return a.data[constants.AccountAnon].(bool)
}
func (a *account) Redirect(c *gin.Context) {
id := uuid.New().String()
tokenstring, err := jwttoken(jwt.MapClaims{
string(auth.AccountID): id,
string(auth.AccountAnon): true,
string(constants.AccountID): id,
string(constants.AccountAnon): true,
}, a.s.jwtprivatekey)
if err != nil {
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())
}
func (a *account) Get(key auth.AccountConstant) interface{} {
func (a *account) Get(key constants.AccountConstant) interface{} {
return a.data[key]
}
func (a *account) List() []auth.AccountConstant {
var liste []auth.AccountConstant
func (a *account) List() []constants.AccountConstant {
var liste []constants.AccountConstant
for key := range a.data {
liste = append(liste, key)
}

Datei anzeigen

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