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