client.go 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. // A client for Kerberos 5 authentication.
  2. package client
  3. import (
  4. "github.com/jcmturner/gokrb5/config"
  5. "github.com/jcmturner/gokrb5/credentials"
  6. "github.com/jcmturner/gokrb5/keytab"
  7. )
  8. // Client struct.
  9. type Client struct {
  10. Credentials *credentials.Credentials
  11. Config *config.Config
  12. Session *Session
  13. Cache *Cache
  14. }
  15. // Create a new client with a password credential.
  16. func NewClientWithPassword(username, realm, password string) Client {
  17. creds := credentials.NewCredentials(username, realm)
  18. return Client{
  19. Credentials: creds.WithPassword(password),
  20. Config: config.NewConfig(),
  21. Session: &Session{},
  22. Cache: NewCache(),
  23. }
  24. }
  25. // Create a new client with a keytab credential.
  26. func NewClientWithKeytab(username, realm string, kt keytab.Keytab) Client {
  27. creds := credentials.NewCredentials(username, realm)
  28. return Client{
  29. Credentials: creds.WithKeytab(kt),
  30. Config: config.NewConfig(),
  31. Session: &Session{},
  32. Cache: NewCache(),
  33. }
  34. }
  35. // Set the Kerberos configuration for the client.
  36. func (cl *Client) WithConfig(cfg *config.Config) *Client {
  37. cl.Config = cfg
  38. return cl
  39. }
  40. // Load the Kerberos configuration for the client from file path specified.
  41. func (cl *Client) LoadConfig(cfgPath string) (*Client, error) {
  42. cfg, err := config.Load(cfgPath)
  43. if err != nil {
  44. return cl, err
  45. }
  46. cl.Config = cfg
  47. return cl, nil
  48. }
  49. // Has the client got sufficient values required.
  50. func (cl *Client) IsConfigured() bool {
  51. if !cl.Credentials.HasPassword() && !cl.Credentials.HasKeytab() {
  52. return false
  53. }
  54. if cl.Credentials.Username == "" {
  55. return false
  56. }
  57. if cl.Config.LibDefaults.Default_realm == "" {
  58. return false
  59. }
  60. for _, r := range cl.Config.Realms {
  61. if r.Realm == cl.Config.LibDefaults.Default_realm {
  62. if len(r.Kdc) > 0 {
  63. return true
  64. } else {
  65. return false
  66. }
  67. }
  68. }
  69. return false
  70. }