Jelajahi Sumber

client destroy methods

Jonathan Turner 7 tahun lalu
induk
melakukan
ece543f967
3 mengubah file dengan 35 tambahan dan 1 penghapusan
  1. 9 0
      client/cache.go
  2. 8 0
      client/client.go
  3. 18 1
      client/session.go

+ 9 - 0
client/cache.go

@@ -55,6 +55,15 @@ func (c *Cache) addEntry(tkt messages.Ticket, authTime, startTime, endTime, rene
 	return c.Entries[spn]
 }
 
+// Clear deletes all the cache entries
+func (c *Cache) clear() {
+	c.mux.Lock()
+	defer c.mux.Unlock()
+	for k := range c.Entries {
+		delete(c.Entries, k)
+	}
+}
+
 // RemoveEntry removes the cache entry for the defined SPN.
 func (c *Cache) RemoveEntry(spn string) {
 	c.mux.Lock()

+ 8 - 0
client/client.go

@@ -4,6 +4,7 @@ package client
 import (
 	"errors"
 	"fmt"
+	"time"
 
 	"gopkg.in/jcmturner/gokrb5.v5/config"
 	"gopkg.in/jcmturner/gokrb5.v5/credentials"
@@ -213,3 +214,10 @@ func (cl *Client) Login() error {
 	cl.AddSession(ASRep.Ticket, ASRep.DecryptedEncPart)
 	return nil
 }
+
+// Destroy stops the auto-renewal of all sessions and removes the sessions and cache entries from the client.
+func (cl *Client) Destroy() {
+	cl.sessions.destroy()
+	cl.Cache.clear()
+	cl.Credentials.ValidUntil = time.Now().UTC()
+}

+ 18 - 1
client/session.go

@@ -17,6 +17,15 @@ type sessions struct {
 	mux     sync.RWMutex
 }
 
+func (s *sessions) destroy() {
+	s.mux.Lock()
+	defer s.mux.Unlock()
+	for k, e := range s.Entries {
+		e.destroy()
+		delete(s.Entries, k)
+	}
+}
+
 // Client session struct.
 type session struct {
 	Realm                string
@@ -34,7 +43,6 @@ func (s *session) update(tkt messages.Ticket, dep messages.EncKDCRepPart) {
 	s.mux.Lock()
 	defer s.mux.Unlock()
 	s.AuthTime = dep.AuthTime
-	s.AuthTime = dep.AuthTime
 	s.EndTime = dep.EndTime
 	s.RenewTill = dep.RenewTill
 	s.TGT = tkt
@@ -42,6 +50,15 @@ func (s *session) update(tkt messages.Ticket, dep messages.EncKDCRepPart) {
 	s.SessionKeyExpiration = dep.KeyExpiration
 }
 
+func (s *session) destroy() {
+	s.mux.Lock()
+	defer s.mux.Unlock()
+	s.cancel <- true
+	s.EndTime = time.Now().UTC()
+	s.RenewTill = s.EndTime
+	s.SessionKeyExpiration = s.EndTime
+}
+
 // AddSession adds a session for a realm with a TGT to the client's session cache.
 // A goroutine is started to automatically renew the TGT before expiry.
 func (cl *Client) AddSession(tkt messages.Ticket, dep messages.EncKDCRepPart) {