| 1 |
package client |
| 2 |
|
| 3 |
import ( |
| 4 |
"gopkg.in/jcmturner/gokrb5.v5/messages" |
| 5 |
"gopkg.in/jcmturner/gokrb5.v5/types" |
| 6 |
"strings" |
| 7 |
"sync" |
| 8 |
"time" |
| 9 |
) |
| 10 |
|
| 11 |
|
| 12 |
type Cache struct { |
| 13 |
Entries map[string]CacheEntry |
| 14 |
mux sync.RWMutex |
| 15 |
} |
| 16 |
|
| 17 |
|
| 18 |
type CacheEntry struct { |
| 19 |
Ticket messages.Ticket |
| 20 |
AuthTime time.Time |
| 21 |
StartTime time.Time |
| 22 |
EndTime time.Time |
| 23 |
RenewTill time.Time |
| 24 |
SessionKey types.EncryptionKey |
| 25 |
} |
| 26 |
|
| 27 |
|
| 28 |
func NewCache() *Cache { |
| 29 |
return &Cache{ |
| 30 |
Entries: map[string]CacheEntry{}, |
| 31 |
} |
| 32 |
} |
| 33 |
|
| 34 |
|
| 35 |
func (c *Cache) getEntry(spn string) (CacheEntry, bool) { |
| 36 |
c.mux.RLock() |
| 37 |
defer c.mux.RUnlock() |
| 38 |
e, ok := (*c).Entries[spn] |
| 39 |
return e, ok |
| 40 |
} |
| 41 |
|
| 42 |
|
| 43 |
func (c *Cache) addEntry(tkt messages.Ticket, authTime, startTime, endTime, renewTill time.Time, sessionKey types.EncryptionKey) CacheEntry { |
| 44 |
spn := strings.Join(tkt.SName.NameString, "/") |
| 45 |
c.mux.Lock() |
| 46 |
defer c.mux.Unlock() |
| 47 |
(*c).Entries[spn] = CacheEntry{ |
| 48 |
Ticket: tkt, |
| 49 |
AuthTime: authTime, |
| 50 |
StartTime: startTime, |
| 51 |
EndTime: endTime, |
| 52 |
RenewTill: renewTill, |
| 53 |
SessionKey: sessionKey, |
| 54 |
} |
| 55 |
return c.Entries[spn] |
| 56 |
} |
| 57 |
|
| 58 |
|
| 59 |
func (c *Cache) clear() { |
| 60 |
c.mux.Lock() |
| 61 |
defer c.mux.Unlock() |
| 62 |
for k := range c.Entries { |
| 63 |
delete(c.Entries, k) |
| 64 |
} |
| 65 |
} |
| 66 |
|
| 67 |
|
| 68 |
func (c *Cache) RemoveEntry(spn string) { |
| 69 |
c.mux.Lock() |
| 70 |
defer c.mux.Unlock() |
| 71 |
delete(c.Entries, spn) |
| 72 |
} |
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
func (cl *Client) GetCachedTicket(spn string) (messages.Ticket, types.EncryptionKey, bool) { |
| 77 |
if e, ok := cl.Cache.getEntry(spn); ok { |
| 78 |
|
| 79 |
if time.Now().UTC().After(e.StartTime) && time.Now().UTC().Before(e.EndTime) { |
| 80 |
return e.Ticket, e.SessionKey, true |
| 81 |
} else if time.Now().UTC().Before(e.RenewTill) { |
| 82 |
e, err := cl.renewTicket(e) |
| 83 |
if err != nil { |
| 84 |
return e.Ticket, e.SessionKey, false |
| 85 |
} |
| 86 |
return e.Ticket, e.SessionKey, true |
| 87 |
} |
| 88 |
} |
| 89 |
var tkt messages.Ticket |
| 90 |
var key types.EncryptionKey |
| 91 |
return tkt, key, false |
| 92 |
} |
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
func (cl *Client) renewTicket(e CacheEntry) (CacheEntry, error) { |
| 97 |
spn := e.Ticket.SName |
| 98 |
_, tgsRep, err := cl.TGSExchange(spn, e.Ticket.Realm, e.Ticket, e.SessionKey, true, 0) |
| 99 |
if err != nil { |
| 100 |
return e, err |
| 101 |
} |
| 102 |
e = cl.Cache.addEntry( |
| 103 |
tgsRep.Ticket, |
| 104 |
tgsRep.DecryptedEncPart.AuthTime, |
| 105 |
tgsRep.DecryptedEncPart.StartTime, |
| 106 |
tgsRep.DecryptedEncPart.EndTime, |
| 107 |
tgsRep.DecryptedEncPart.RenewTill, |
| 108 |
tgsRep.DecryptedEncPart.Key, |
| 109 |
) |
| 110 |
return e, nil |
| 111 |
} |