1
0

store.go 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. package captcha
  2. import (
  3. "github.com/mojocn/base64Captcha"
  4. "gogs.baozhida.cn/zoie/OAuth-core/storage"
  5. )
  6. type cacheStore struct {
  7. cache storage.AdapterCache
  8. expiration int
  9. }
  10. // NewCacheStore returns a new standard memory store for captchas with the
  11. // given collection threshold and expiration time (duration). The returned
  12. // store must be registered with SetCustomStore to replace the default one.
  13. func NewCacheStore(cache storage.AdapterCache, expiration int) base64Captcha.Store {
  14. s := new(cacheStore)
  15. s.cache = cache
  16. s.expiration = expiration
  17. return s
  18. }
  19. // Set sets the digits for the captcha id.
  20. func (e *cacheStore) Set(id string, value string) {
  21. _ = e.cache.Set(id, value, e.expiration)
  22. }
  23. // Get returns stored digits for the captcha id. Clear indicates
  24. // whether the captcha must be deleted from the store.
  25. func (e *cacheStore) Get(id string, clear bool) string {
  26. v, err := e.cache.Get(id)
  27. if err == nil {
  28. if clear {
  29. _ = e.cache.Del(id)
  30. }
  31. return v
  32. }
  33. return ""
  34. }
  35. // Verify captcha's answer directly
  36. func (e *cacheStore) Verify(id, answer string, clear bool) bool {
  37. return e.Get(id, clear) == answer
  38. }