session_secure.go 787 B

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. package login
  2. import (
  3. "crypto/rand"
  4. "encoding/hex"
  5. "fmt"
  6. "gorm.io/gorm"
  7. )
  8. type SessionSecurer interface {
  9. UpdateSecure(db *gorm.DB, model interface{}, id string) error
  10. GetSecure() string
  11. }
  12. type SessionSecure struct {
  13. SessionSecure string `gorm:"size:32"`
  14. }
  15. var _ SessionSecurer = (*SessionSecure)(nil)
  16. func (ss *SessionSecure) UpdateSecure(db *gorm.DB, model interface{}, id string) error {
  17. b := make([]byte, 16)
  18. rand.Read(b)
  19. secure := hex.EncodeToString(b)
  20. err := db.Model(model).
  21. Where(fmt.Sprintf("%s = ?", snakePrimaryField(model)), id).
  22. Updates(map[string]interface{}{
  23. "session_secure": secure,
  24. }).
  25. Error
  26. if err != nil {
  27. return err
  28. }
  29. ss.SessionSecure = secure
  30. return nil
  31. }
  32. func (ss *SessionSecure) GetSecure() string {
  33. return ss.SessionSecure
  34. }