cookie.go 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. package dao
  2. import (
  3. "context"
  4. "database/sql"
  5. "fmt"
  6. "time"
  7. "go-common/app/job/main/passport-auth/model"
  8. "go-common/library/log"
  9. )
  10. const (
  11. _addCookieSQL = "INSERT IGNORE INTO user_cookie_%s (mid,session,csrf,type,expires) VALUES (?,?,?,?,?)"
  12. _delCookieBySessionSQL = "DELETE FROM user_cookie_%s where session = ?"
  13. _addCookieDeletedSQL = "INSERT IGNORE INTO user_cookie_deleted_%s (mid,session,csrf,type,expires,ctime) VALUES (?,?,?,?,?,?)"
  14. )
  15. // AddCookie save cookie
  16. func (d *Dao) AddCookie(c context.Context, cookie *model.Cookie, session, csrf []byte, ct time.Time) (affected int64, err error) {
  17. row, err := d.db.Exec(c, fmt.Sprintf(_addCookieSQL, formatSuffix(ct)), cookie.Mid, session, csrf, cookie.Type, cookie.Expires)
  18. if err != nil {
  19. log.Error("dao.db.Exec(%v) err(%v)", cookie, err)
  20. return
  21. }
  22. return row.RowsAffected()
  23. }
  24. // DelCookie del cookie by session
  25. func (d *Dao) DelCookie(c context.Context, session []byte, ct time.Time) (affected int64, err error) {
  26. var res sql.Result
  27. if res, err = d.db.Exec(c, fmt.Sprintf(_delCookieBySessionSQL, formatSuffix(ct)), session); err != nil {
  28. log.Error("del cookie by session , dao.db.Exec(%s) error(%v)", session, err)
  29. return
  30. }
  31. return res.RowsAffected()
  32. }
  33. // AddCookieDeleted save cookie deleted
  34. func (d *Dao) AddCookieDeleted(c context.Context, cookie *model.Cookie, session, csrf []byte, ct time.Time) (affected int64, err error) {
  35. row, err := d.db.Exec(c, fmt.Sprintf(_addCookieDeletedSQL, formatSuffix(ct)), cookie.Mid, session, csrf, cookie.Type, cookie.Expires, cookie.Ctime)
  36. if err != nil {
  37. log.Error("fail to add cookie deleted, cookie(%+v), tx.Exec() error(%+v)", cookie, err)
  38. return
  39. }
  40. return row.RowsAffected()
  41. }