cookie.go 828 B

12345678910111213141516171819202122232425262728293031323334
  1. package dao
  2. import (
  3. "context"
  4. "encoding/hex"
  5. "fmt"
  6. "time"
  7. "go-common/app/service/main/passport-auth/model"
  8. xsql "go-common/library/database/sql"
  9. "go-common/library/log"
  10. )
  11. const (
  12. _getCookieSessionSQL = "SELECT mid,session,csrf,type,expires FROM user_cookie_%s where session = ? limit 1"
  13. )
  14. // Cookie get cookie by session
  15. func (d *Dao) Cookie(c context.Context, sd []byte, ct time.Time) (res *model.Cookie, session []byte, err error) {
  16. row := d.db.QueryRow(c, fmt.Sprintf(_getCookieSessionSQL, formatSuffix(ct)), sd)
  17. res = new(model.Cookie)
  18. var csrf []byte
  19. if err = row.Scan(&res.Mid, &session, &csrf, &res.Type, &res.Expires); err != nil {
  20. if err == xsql.ErrNoRows {
  21. res = nil
  22. err = nil
  23. } else {
  24. log.Error("row.Scan() error(%v)", err)
  25. }
  26. return
  27. }
  28. res.CSRF = hex.EncodeToString(csrf)
  29. return
  30. }