Skip to content

Commit

Permalink
sasl structs: add shortcuts to for sasl.Mechanism
Browse files Browse the repository at this point in the history
This will make sasl easier to use at callsites.
  • Loading branch information
twmb committed Oct 7, 2020
1 parent 394944a commit f096901
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 0 deletions.
11 changes: 11 additions & 0 deletions pkg/sasl/kerberos/kerberos.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,17 @@ type Auth struct {
PersistAfterAuth bool
}

// AsMechanism returns a sasl mechanism that will use a as credentials for all
// sasl sessions.
//
// This is a shortcut for using the Kerberos function and is useful when you do
// not need to live-rotate credentials.
func (a Auth) AsMechanism() sasl.Mechanism {
return Kerberos(func(context.Context) (Auth, error) {
return a, nil
})
}

// Kerberos returns a sasl mechanism that will call authFn whenever sasl
// authentication is needed. The returned Auth is used for a single session.
func Kerberos(authFn func(context.Context) (Auth, error)) sasl.Mechanism {
Expand Down
11 changes: 11 additions & 0 deletions pkg/sasl/oauth/oauth.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,17 @@ type Auth struct {
_internal struct{} // require explicit field initalization
}

// AsMechanism returns a sasl mechanism that will use a as credentials for all
// sasl sessions.
//
// This is a shortcut for using the Oauth function and is useful when you do
// not need to live-rotate credentials.
func (a Auth) AsMechanism() sasl.Mechanism {
return Oauth(func(context.Context) (Auth, error) {
return a, nil
})
}

// Oauth returns an OAUTHBEARER sasl mechanism that will call authFn whenever
// authentication is needed. The returned Auth is used for a single session.
func Oauth(authFn func(context.Context) (Auth, error)) sasl.Mechanism {
Expand Down
11 changes: 11 additions & 0 deletions pkg/sasl/plain/plain.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,17 @@ type Auth struct {
_internal struct{} // require explicit field initalization
}

// AsMechanism returns a sasl mechanism that will use a as credentials for all
// sasl sessions.
//
// This is a shortcut for using the Plain function and is useful when you do
// not need to live-rotate credentials.
func (a Auth) AsMechanism() sasl.Mechanism {
return Plain(func(context.Context) (Auth, error) {
return a, nil
})
}

// Plain returns a sasl mechanism that will call authFn whenever sasl
// authentication is needed. The returned Auth is used for a single session.
func Plain(authFn func(context.Context) (Auth, error)) sasl.Mechanism {
Expand Down
22 changes: 22 additions & 0 deletions pkg/sasl/scram/scram.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,28 @@ type Auth struct {
_internal struct{} // require explicit field initalization
}

// AsSha256Mechanism returns a sasl mechanism that will use a as credentials for
// all sasl sessions.
//
// This is a shortcut for using the Sha256 function and is useful when you do
// not need to live-rotate credentials.
func (a Auth) AsSha256Mechanism() sasl.Mechanism {
return Sha256(func(context.Context) (Auth, error) {
return a, nil
})
}

// AsSha512Mechanism returns a sasl mechanism that will use a as credentials for
// all sasl sessions.
//
// This is a shortcut for using the Sha512 function and is useful when you do
// not need to live-rotate credentials.
func (a Auth) AsSha512Mechanism() sasl.Mechanism {
return Sha512(func(context.Context) (Auth, error) {
return a, nil
})
}

// Sha256 returns a SCRAM-SHA-256 sasl mechanism that will call authFn
// whenever authentication is needed. The returned Auth is used for a single
// session.
Expand Down

0 comments on commit f096901

Please sign in to comment.