2024-08-28 15:20:38 +00:00
|
|
|
package storage
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/go-webauthn/webauthn/webauthn"
|
|
|
|
"github.com/google/uuid"
|
|
|
|
"github.com/rs/zerolog/log"
|
2024-12-18 14:24:56 +00:00
|
|
|
"git.mstar.dev/mstar/linstrom/util"
|
2024-08-28 15:20:38 +00:00
|
|
|
)
|
|
|
|
|
2024-09-12 14:57:53 +00:00
|
|
|
// Session data used during login attempts with a passkey
|
|
|
|
// Not actually used afterwards to verify a normal session
|
|
|
|
// NOTE: Doesn't contain a DeletedAt field, thus deletions are automatically hard and not reversible
|
2024-08-28 15:20:38 +00:00
|
|
|
type PasskeySession struct {
|
|
|
|
ID string `gorm:"primarykey"`
|
|
|
|
Data webauthn.SessionData `gorm:"serializer:json"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// ---- Section SessionStore
|
|
|
|
|
2024-09-12 14:57:53 +00:00
|
|
|
// Generate some id for a new session. Just returns a new uuid
|
2024-08-28 15:20:38 +00:00
|
|
|
func (s *Storage) GenSessionID() (string, error) {
|
2024-11-06 15:57:44 +00:00
|
|
|
defer util.Untrace(util.Trace(&log.Logger))
|
2024-08-28 15:20:38 +00:00
|
|
|
x := uuid.NewString()
|
|
|
|
log.Debug().Str("session-id", x).Msg("Generated new passkey session id")
|
|
|
|
return x, nil
|
|
|
|
}
|
|
|
|
|
2024-09-12 14:57:53 +00:00
|
|
|
// Look for an active session with a given id
|
|
|
|
// Returns the session if found and a bool indicating if a session was found
|
2024-08-28 15:20:38 +00:00
|
|
|
func (s *Storage) GetSession(sessionId string) (*webauthn.SessionData, bool) {
|
2024-11-06 15:57:44 +00:00
|
|
|
defer util.Untrace(util.Trace(&log.Logger))
|
2024-08-28 15:20:38 +00:00
|
|
|
log.Debug().Str("id", sessionId).Msg("Looking for passkey session")
|
|
|
|
session := PasskeySession{}
|
|
|
|
res := s.db.Where("id = ?", sessionId).First(&session)
|
|
|
|
if res.Error != nil {
|
|
|
|
return nil, false
|
|
|
|
}
|
|
|
|
log.Debug().Str("id", sessionId).Any("webauthn-data", &session).Msg("Found passkey session")
|
|
|
|
return &session.Data, true
|
|
|
|
}
|
|
|
|
|
2024-09-12 14:57:53 +00:00
|
|
|
// Save (or update) a session with the new data
|
2024-08-28 15:20:38 +00:00
|
|
|
func (s *Storage) SaveSession(token string, data *webauthn.SessionData) {
|
2024-11-06 15:57:44 +00:00
|
|
|
defer util.Untrace(util.Trace(&log.Logger))
|
2024-08-28 15:20:38 +00:00
|
|
|
log.Debug().Str("id", token).Any("webauthn-data", data).Msg("Saving passkey session")
|
|
|
|
session := PasskeySession{
|
|
|
|
ID: token,
|
|
|
|
Data: *data,
|
|
|
|
}
|
|
|
|
s.db.Save(&session)
|
|
|
|
}
|
|
|
|
|
2024-09-12 14:57:53 +00:00
|
|
|
// Delete a session
|
|
|
|
// NOTE: This is a hard delete since the session struct contains no DeletedAt field
|
2024-08-28 15:20:38 +00:00
|
|
|
func (s *Storage) DeleteSession(token string) {
|
2024-11-06 15:57:44 +00:00
|
|
|
defer util.Untrace(util.Trace(&log.Logger))
|
2024-08-28 15:20:38 +00:00
|
|
|
log.Debug().Str("id", token).Msg("Deleting passkey session (if one exists)")
|
|
|
|
s.db.Delete(&PasskeySession{ID: token})
|
|
|
|
}
|