cache.go 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. package client
  2. import (
  3. "errors"
  4. "fmt"
  5. "github.com/jcmturner/gokrb5/types"
  6. "strings"
  7. "time"
  8. )
  9. // Client ticket cache
  10. type Cache struct {
  11. Entries map[string]CacheEntry
  12. }
  13. // Ticket cache entry
  14. type CacheEntry struct {
  15. Ticket types.Ticket
  16. AuthTime time.Time
  17. EndTime time.Time
  18. RenewTill time.Time
  19. AutoRenew bool
  20. }
  21. // Create a new client ticket cache.
  22. func NewCache() *Cache {
  23. return &Cache{
  24. Entries: map[string]CacheEntry{},
  25. }
  26. }
  27. // Get a cache entry that matches the SPN.
  28. func (c *Cache) GetEntry(spn string) (CacheEntry, bool) {
  29. e, ok := (*c).Entries[spn]
  30. return e, ok
  31. }
  32. // Get a ticket from the cache for the SPN.
  33. // Only a ticket that is currently valid will be returned.
  34. func (c *Cache) GetTicket(spn string) (types.Ticket, bool) {
  35. if e, ok := c.GetEntry(spn); ok {
  36. //If within time window of ticket return it
  37. if time.Now().After(e.AuthTime) && time.Now().Before(e.EndTime) {
  38. return e.Ticket, true
  39. }
  40. }
  41. var tkt types.Ticket
  42. return tkt, false
  43. }
  44. // Renew a ticket in the cache for the specified SPN.
  45. func (c *Cache) RenewEntry(spn string) error {
  46. if e, ok := c.GetEntry(spn); ok {
  47. return e.Renew()
  48. }
  49. return fmt.Errorf("No entry for this SPN: %s", spn)
  50. }
  51. // Add a ticket to the cache
  52. func (c *Cache) AddEntry(tkt types.Ticket, authTime, endTime, renewTill time.Time) {
  53. (*c).Entries[strings.Join(tkt.SName.NameString, "/")] = CacheEntry{
  54. Ticket: tkt,
  55. AuthTime: authTime,
  56. EndTime: endTime,
  57. RenewTill: renewTill,
  58. }
  59. }
  60. // Remove the cache entry for the defined SPN
  61. func (c *Cache) RemoveEntry(spn string) {
  62. delete(c.Entries, spn)
  63. }
  64. // Enable background auto renew of the ticket for the specified SPN
  65. func (c *Cache) EnableAutoRenew(spn string) error {
  66. return nil
  67. }
  68. // Disable background auto renew of the ticket for the specified SPN
  69. func (c *Cache) DisableAutoRenew(spn string) error {
  70. return nil
  71. }
  72. // Renew the cache entry
  73. func (e *CacheEntry) Renew() error {
  74. if time.Now().After(e.RenewTill) {
  75. return errors.New("Past renew till time. Cannot renew.")
  76. }
  77. //TODO put renew action here
  78. return nil
  79. }