You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

62 regels
2.5 KiB

  1. // Package auth package provides authentication-related interfaces and types.
  2. // It also includes a basic implementation of credentials using username and password.
  3. package auth
  4. // StreamingCredentialsProvider is an interface that defines the methods for a streaming credentials provider.
  5. // It is used to provide credentials for authentication.
  6. // The CredentialsListener is used to receive updates when the credentials change.
  7. type StreamingCredentialsProvider interface {
  8. // Subscribe subscribes to the credentials provider for updates.
  9. // It returns the current credentials, a cancel function to unsubscribe from the provider,
  10. // and an error if any.
  11. // TODO(ndyakov): Should we add context to the Subscribe method?
  12. Subscribe(listener CredentialsListener) (Credentials, UnsubscribeFunc, error)
  13. }
  14. // UnsubscribeFunc is a function that is used to cancel the subscription to the credentials provider.
  15. // It is used to unsubscribe from the provider when the credentials are no longer needed.
  16. type UnsubscribeFunc func() error
  17. // CredentialsListener is an interface that defines the methods for a credentials listener.
  18. // It is used to receive updates when the credentials change.
  19. // The OnNext method is called when the credentials change.
  20. // The OnError method is called when an error occurs while requesting the credentials.
  21. type CredentialsListener interface {
  22. OnNext(credentials Credentials)
  23. OnError(err error)
  24. }
  25. // Credentials is an interface that defines the methods for credentials.
  26. // It is used to provide the credentials for authentication.
  27. type Credentials interface {
  28. // BasicAuth returns the username and password for basic authentication.
  29. BasicAuth() (username string, password string)
  30. // RawCredentials returns the raw credentials as a string.
  31. // This can be used to extract the username and password from the raw credentials or
  32. // additional information if present in the token.
  33. RawCredentials() string
  34. }
  35. type basicAuth struct {
  36. username string
  37. password string
  38. }
  39. // RawCredentials returns the raw credentials as a string.
  40. func (b *basicAuth) RawCredentials() string {
  41. return b.username + ":" + b.password
  42. }
  43. // BasicAuth returns the username and password for basic authentication.
  44. func (b *basicAuth) BasicAuth() (username string, password string) {
  45. return b.username, b.password
  46. }
  47. // NewBasicCredentials creates a new Credentials object from the given username and password.
  48. func NewBasicCredentials(username, password string) Credentials {
  49. return &basicAuth{
  50. username: username,
  51. password: password,
  52. }
  53. }