Ver Fonte

use bool rather than err to indicate if not in cache

Jonathan Turner há 8 anos atrás
pai
commit
7d566b50b6
3 ficheiros alterados com 11 adições e 11 exclusões
  1. 5 4
      client/client.go
  2. 3 4
      credentials/ccache.go
  3. 3 3
      credentials/ccache_test.go

+ 5 - 4
client/client.go

@@ -2,6 +2,7 @@
 package client
 
 import (
+	"errors"
 	"fmt"
 	"github.com/jcmturner/gokrb5/config"
 	"github.com/jcmturner/gokrb5/credentials"
@@ -66,12 +67,12 @@ func NewClientFromCCache(c credentials.CCache) (Client, error) {
 		NameType:   nametype.KRB_NT_SRV_INST,
 		NameString: []string{"krbtgt", c.DefaultPrincipal.Realm},
 	}
-	cred, err := c.GetEntry(spn)
-	if err != nil {
-		return cl, err
+	cred, ok := c.GetEntry(spn)
+	if !ok {
+		return cl, errors.New("TGT not found in CCache")
 	}
 	var tgt messages.Ticket
-	err = tgt.Unmarshal(cred.Ticket)
+	err := tgt.Unmarshal(cred.Ticket)
 	if err != nil {
 		return cl, fmt.Errorf("TGT bytes in cache are not valid: %v", err)
 	}

+ 3 - 4
credentials/ccache.go

@@ -4,7 +4,6 @@ import (
 	"bytes"
 	"encoding/binary"
 	"errors"
-	"fmt"
 	"github.com/jcmturner/asn1"
 	"github.com/jcmturner/gokrb5/types"
 	"io/ioutil"
@@ -230,7 +229,7 @@ func (c *CCache) Contains(p types.PrincipalName) bool {
 }
 
 // GetEntry returns a specific credential for the PrincipalName provided.
-func (c *CCache) GetEntry(p types.PrincipalName) (credential, error) {
+func (c *CCache) GetEntry(p types.PrincipalName) (credential, bool) {
 	var cred credential
 	var found bool
 	for i := range c.Credentials {
@@ -241,9 +240,9 @@ func (c *CCache) GetEntry(p types.PrincipalName) (credential, error) {
 		}
 	}
 	if !found {
-		return cred, fmt.Errorf("Cache does not contain an entry for %s", p.GetPrincipalNameString())
+		return cred, false
 	}
-	return cred, nil
+	return cred, true
 }
 
 // GetEntries filters out configuration entries an returns a slice of credentials.

+ 3 - 3
credentials/ccache_test.go

@@ -82,9 +82,9 @@ func TestCCache_GetEntry(t *testing.T) {
 		NameType:   nametype.KRB_NT_PRINCIPAL,
 		NameString: []string{"HTTP", "host.test.gokrb5"},
 	}
-	cred, err := c.GetEntry(httppn)
-	if err != nil {
-		t.Fatalf("Could not get entry from CCache: %v", err)
+	cred, ok := c.GetEntry(httppn)
+	if !ok {
+		t.Fatal("Could not get entry from CCache as not found")
 	}
 	assert.Equal(t, httppn, cred.Server.PrincipalName, "Credential does not have the right server principal name")
 }