client.go 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. package client
  2. import (
  3. "github.com/jcmturner/gokrb5/config"
  4. "github.com/jcmturner/gokrb5/credentials"
  5. "github.com/jcmturner/gokrb5/keytab"
  6. )
  7. type Client struct {
  8. Credentials *credentials.Credentials
  9. Config *config.Config
  10. Session *Session
  11. }
  12. func NewClientWithPassword(username, password string) Client {
  13. creds := credentials.NewCredentials(username)
  14. return Client{
  15. Credentials: creds.WithPassword(password),
  16. Config: config.NewConfig(),
  17. }
  18. }
  19. func NewClientWithKeytab(username string, kt keytab.Keytab) Client {
  20. creds := credentials.NewCredentials(username)
  21. return Client{
  22. Credentials: creds.WithKeytab(kt),
  23. Config: config.NewConfig(),
  24. }
  25. }
  26. func (cl *Client) WithConfig(cfg *config.Config) *Client {
  27. cl.Config = cfg
  28. return cl
  29. }
  30. func (cl *Client) LoadConfig(cfgPath string) (*Client, error) {
  31. cfg, err := config.Load(cfgPath)
  32. if err != nil {
  33. return cl, err
  34. }
  35. cl.Config = cfg
  36. return cl, nil
  37. }
  38. func (cl *Client) IsConfigured() bool {
  39. if !cl.Credentials.HasPassword() && !cl.Credentials.HasKeytab() {
  40. return false
  41. }
  42. if cl.Credentials.Username == "" {
  43. return false
  44. }
  45. if cl.Config.LibDefaults.Default_realm == "" {
  46. return false
  47. }
  48. for _, r := range cl.Config.Realms {
  49. if r.Realm == cl.Config.LibDefaults.Default_realm {
  50. if len(r.Kdc) > 0 {
  51. return true
  52. } else {
  53. return false
  54. }
  55. }
  56. }
  57. return false
  58. }