You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

discovery_cache.go 1.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. package openid
  2. import (
  3. "sync"
  4. )
  5. type DiscoveredInfo interface {
  6. OpEndpoint() string
  7. OpLocalID() string
  8. ClaimedID() string
  9. // ProtocolVersion: it's always openId 2.
  10. }
  11. type DiscoveryCache interface {
  12. Put(id string, info DiscoveredInfo)
  13. // Return a discovered info, or nil.
  14. Get(id string) DiscoveredInfo
  15. }
  16. type SimpleDiscoveredInfo struct {
  17. opEndpoint string
  18. opLocalID string
  19. claimedID string
  20. }
  21. func (s *SimpleDiscoveredInfo) OpEndpoint() string {
  22. return s.opEndpoint
  23. }
  24. func (s *SimpleDiscoveredInfo) OpLocalID() string {
  25. return s.opLocalID
  26. }
  27. func (s *SimpleDiscoveredInfo) ClaimedID() string {
  28. return s.claimedID
  29. }
  30. type SimpleDiscoveryCache struct {
  31. cache map[string]DiscoveredInfo
  32. mutex *sync.Mutex
  33. }
  34. func NewSimpleDiscoveryCache() *SimpleDiscoveryCache {
  35. return &SimpleDiscoveryCache{cache: map[string]DiscoveredInfo{}, mutex: &sync.Mutex{}}
  36. }
  37. func (s *SimpleDiscoveryCache) Put(id string, info DiscoveredInfo) {
  38. s.mutex.Lock()
  39. defer s.mutex.Unlock()
  40. s.cache[id] = info
  41. }
  42. func (s *SimpleDiscoveryCache) Get(id string) DiscoveredInfo {
  43. s.mutex.Lock()
  44. defer s.mutex.Unlock()
  45. if info, has := s.cache[id]; has {
  46. return info
  47. }
  48. return nil
  49. }
  50. func compareDiscoveredInfo(a DiscoveredInfo, opEndpoint, opLocalID, claimedID string) bool {
  51. return a != nil &&
  52. a.OpEndpoint() == opEndpoint &&
  53. a.OpLocalID() == opLocalID &&
  54. a.ClaimedID() == claimedID
  55. }