credentials.go 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. // Credentials for Kerberos 5 authentication.
  2. package credentials
  3. import (
  4. "github.com/jcmturner/gokrb5/iana/nametype"
  5. "github.com/jcmturner/gokrb5/keytab"
  6. "github.com/jcmturner/gokrb5/types"
  7. )
  8. // Credentials struct for a user.
  9. // Contains either a keytab, password or both.
  10. // Keytabs are used over passwords if both are defined.
  11. type Credentials struct {
  12. Username string
  13. Realm string
  14. CName types.PrincipalName
  15. Keytab keytab.Keytab
  16. Password string
  17. }
  18. // Create a new Credentials struct.
  19. func NewCredentials(username string, realm string) Credentials {
  20. return Credentials{
  21. Username: username,
  22. Realm: realm,
  23. CName: types.PrincipalName{
  24. NameType: nametype.KRB_NT_PRINCIPAL,
  25. NameString: []string{username},
  26. },
  27. Keytab: keytab.NewKeytab(),
  28. }
  29. }
  30. // Set the Keytab in the Credentials struct.
  31. func (c *Credentials) WithKeytab(kt keytab.Keytab) *Credentials {
  32. c.Keytab = kt
  33. return c
  34. }
  35. // Set the password in the Credentials struct.
  36. func (c *Credentials) WithPassword(password string) *Credentials {
  37. c.Password = password
  38. return c
  39. }
  40. // Query if the Credentials has a keytab defined.
  41. func (c *Credentials) HasKeytab() bool {
  42. if len(c.Keytab.Entries) > 0 {
  43. return true
  44. }
  45. return false
  46. }
  47. // Query if the Credentials has a password defined.
  48. func (c *Credentials) HasPassword() bool {
  49. if c.Password != "" {
  50. return true
  51. }
  52. return false
  53. }