Sign session cookie to avoid tapping
Signed-off-by: gearnode <bryan@frimin.fr>
This commit is contained in:
@@ -176,8 +176,15 @@ func LogoutHandler(usrmgrSvc *usrmgr.Service, authCfg AuthConfig) http.HandlerFu
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Verify the cookie signature
|
||||||
|
originalValue, err := verifyCookieValue(cookie.Value, authCfg.CookieSecret)
|
||||||
|
if err != nil {
|
||||||
|
http.Error(w, "Invalid session cookie", http.StatusBadRequest)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
// Parse the session ID
|
// Parse the session ID
|
||||||
sessionID, err := gid.ParseGID(cookie.Value)
|
sessionID, err := gid.ParseGID(originalValue)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
http.Error(w, "Invalid session ID", http.StatusBadRequest)
|
http.Error(w, "Invalid session ID", http.StatusBadRequest)
|
||||||
return
|
return
|
||||||
|
|||||||
@@ -44,6 +44,7 @@ type (
|
|||||||
CookieDomain string
|
CookieDomain string
|
||||||
CookiePath string
|
CookiePath string
|
||||||
SessionDuration time.Duration
|
SessionDuration time.Duration
|
||||||
|
CookieSecret string
|
||||||
}
|
}
|
||||||
|
|
||||||
Resolver struct {
|
Resolver struct {
|
||||||
@@ -143,20 +144,24 @@ func graphqlHandler(proboSvc *probo.Service, usrmgrSvc *usrmgr.Service, authCfg
|
|||||||
// Extract session from cookie
|
// Extract session from cookie
|
||||||
cookie, err := r.Cookie(authCfg.CookieName)
|
cookie, err := r.Cookie(authCfg.CookieName)
|
||||||
if err == nil && cookie.Value != "" {
|
if err == nil && cookie.Value != "" {
|
||||||
// Parse the session ID
|
// Verify the cookie signature
|
||||||
sessionID, err := gid.ParseGID(cookie.Value)
|
originalValue, err := verifyCookieValue(cookie.Value, authCfg.CookieSecret)
|
||||||
if err == nil {
|
if err == nil {
|
||||||
// Get the session
|
// Parse the session ID
|
||||||
session, err := usrmgrSvc.GetSession(r.Context(), sessionID)
|
sessionID, err := gid.ParseGID(originalValue)
|
||||||
if err == nil {
|
if err == nil {
|
||||||
// Add session to context
|
// Get the session
|
||||||
ctx = context.WithValue(ctx, sessionContextKey, session)
|
session, err := usrmgrSvc.GetSession(r.Context(), sessionID)
|
||||||
|
|
||||||
// Get the user
|
|
||||||
user, err := usrmgrSvc.GetUserBySession(r.Context(), sessionID)
|
|
||||||
if err == nil {
|
if err == nil {
|
||||||
// Add user to context
|
// Add session to context
|
||||||
ctx = context.WithValue(ctx, userContextKey, user)
|
ctx = context.WithValue(ctx, sessionContextKey, session)
|
||||||
|
|
||||||
|
// Get the user
|
||||||
|
user, err := usrmgrSvc.GetUserBySession(r.Context(), sessionID)
|
||||||
|
if err == nil {
|
||||||
|
// Add user to context
|
||||||
|
ctx = context.WithValue(ctx, userContextKey, user)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -30,16 +30,15 @@ type (
|
|||||||
CookieHTTPOnly bool `json:"cookie-http-only"`
|
CookieHTTPOnly bool `json:"cookie-http-only"`
|
||||||
CookieDomain string `json:"cookie-domain"`
|
CookieDomain string `json:"cookie-domain"`
|
||||||
CookiePath string `json:"cookie-path"`
|
CookiePath string `json:"cookie-path"`
|
||||||
|
CookieSecret string `json:"cookie-secret"`
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
// GetPepperBytes returns the pepper as a byte array
|
|
||||||
func (c authConfig) GetPepperBytes() ([]byte, error) {
|
func (c authConfig) GetPepperBytes() ([]byte, error) {
|
||||||
if c.Pepper == "" {
|
if c.Pepper == "" {
|
||||||
return nil, fmt.Errorf("pepper cannot be empty")
|
return nil, fmt.Errorf("pepper cannot be empty")
|
||||||
}
|
}
|
||||||
|
|
||||||
// If the pepper is base64 encoded, decode it
|
|
||||||
if decoded, err := base64.StdEncoding.DecodeString(c.Pepper); err == nil {
|
if decoded, err := base64.StdEncoding.DecodeString(c.Pepper); err == nil {
|
||||||
if len(decoded) < 32 {
|
if len(decoded) < 32 {
|
||||||
return nil, fmt.Errorf("decoded pepper must be at least 32 bytes long")
|
return nil, fmt.Errorf("decoded pepper must be at least 32 bytes long")
|
||||||
@@ -47,10 +46,28 @@ func (c authConfig) GetPepperBytes() ([]byte, error) {
|
|||||||
return decoded, nil
|
return decoded, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// Otherwise use the raw string as the pepper
|
|
||||||
if len(c.Pepper) < 32 {
|
if len(c.Pepper) < 32 {
|
||||||
return nil, fmt.Errorf("pepper must be at least 32 bytes long")
|
return nil, fmt.Errorf("pepper must be at least 32 bytes long")
|
||||||
}
|
}
|
||||||
|
|
||||||
return []byte(c.Pepper), nil
|
return []byte(c.Pepper), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (c authConfig) GetCookieSecretBytes() ([]byte, error) {
|
||||||
|
if c.CookieSecret == "" {
|
||||||
|
return nil, fmt.Errorf("cookie secret cannot be empty")
|
||||||
|
}
|
||||||
|
|
||||||
|
if decoded, err := base64.StdEncoding.DecodeString(c.CookieSecret); err == nil {
|
||||||
|
if len(decoded) < 32 {
|
||||||
|
return nil, fmt.Errorf("decoded cookie secret must be at least 32 bytes long")
|
||||||
|
}
|
||||||
|
return decoded, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(c.CookieSecret) < 32 {
|
||||||
|
return nil, fmt.Errorf("cookie secret must be at least 32 bytes long")
|
||||||
|
}
|
||||||
|
|
||||||
|
return []byte(c.CookieSecret), nil
|
||||||
|
}
|
||||||
|
|||||||
@@ -80,6 +80,7 @@ func New() *Implm {
|
|||||||
CookieHTTPOnly: true,
|
CookieHTTPOnly: true,
|
||||||
CookieDomain: "localhost",
|
CookieDomain: "localhost",
|
||||||
CookiePath: "/",
|
CookiePath: "/",
|
||||||
|
CookieSecret: "this-is-a-secure-secret-for-cookie-signing-at-least-32-bytes",
|
||||||
},
|
},
|
||||||
AWS: awsConfig{
|
AWS: awsConfig{
|
||||||
Region: "us-east-1",
|
Region: "us-east-1",
|
||||||
@@ -122,6 +123,12 @@ func (impl *Implm) Run(
|
|||||||
return fmt.Errorf("cannot get pepper bytes: %w", err)
|
return fmt.Errorf("cannot get pepper bytes: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Validate cookie secret
|
||||||
|
_, err = impl.cfg.Auth.GetCookieSecretBytes()
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("cannot get cookie secret bytes: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
awsConfig := awsconfig.NewConfig(
|
awsConfig := awsconfig.NewConfig(
|
||||||
l,
|
l,
|
||||||
httpclient.DefaultPooledClient(
|
httpclient.DefaultPooledClient(
|
||||||
@@ -162,6 +169,7 @@ func (impl *Implm) Run(
|
|||||||
CookieDomain: impl.cfg.Auth.CookieDomain,
|
CookieDomain: impl.cfg.Auth.CookieDomain,
|
||||||
CookiePath: impl.cfg.Auth.CookiePath,
|
CookiePath: impl.cfg.Auth.CookiePath,
|
||||||
SessionDuration: time.Duration(impl.cfg.Auth.SessionDuration) * time.Hour,
|
SessionDuration: time.Duration(impl.cfg.Auth.SessionDuration) * time.Hour,
|
||||||
|
CookieSecret: impl.cfg.Auth.CookieSecret,
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
)
|
)
|
||||||
|
|||||||
Reference in New Issue
Block a user