Documentation ¶
Overview ¶
Package basic provides plug and play, generic, secure, easy to use, customizable, and painless Basic Authentication for Go's HTTP handlers. This package tries its best to implement all specifications in a customizable way as specified in [RFC 7617](https://datatracker.ietf.org/doc/html/rfc7617), the newest version of Basic Authentication which obsoletes [RFC 2617](https://datatracker.ietf.org/doc/html/rfc2617).
Basic Authentication itself is a simple and secure way to protect your API endpoints. However, for it to be completely secure, you have to augment the authentication by using SSL/TLS. You may use hashes / encryption in advance, but I think it's not necessary. SSL/TLS provides excellent security as long as you can trust your Certificate Authority and can ensure your connections are end-to-end encrypted, no sniffers or spoofers whatsoever.
In order to use this package, you have to instantiate a new `BasicAuth` instance before hooking it to a handler. You can set various configurations, such as the authenticator function, charset (if using non-standard, other than UTF-8 charset), invalid credentials response, invalid scheme response, custom realm, and static users if applicable. If you want to do anything with the `*http.Request` struct, it is recommended for you to process it in a previous custom middleware before implementing your authentication with this library. This package tries its best to be as generic as possible, so you can definitely use any web framework or customized handlers as long as it conforms to the main interface (`http.Handler`).
As a note about the `BasicAuth` attributes, you may use the authenticator function in order to perform a more sophisticated authentication logic, such as pulling your user based on their username from the database. Another thing to note is that you can pass `nil` or `make(map[string]string)` to the `Users` attribute if you do not need static credentials. Finally, the `WWW-Authenticate` header is only sent if both `Charset` and `Realm` are set. `Users` attribute is a 1-to-1 mapping of username and password.
See example in `example/main.go`.
Index ¶
- func CompareInputs(input, expected string) bool
- type BasicAuth
- func (a *BasicAuth) Authenticate(next http.HandlerFunc) http.HandlerFunc
- func (a *BasicAuth) SendInvalidCredentialsResponse(w http.ResponseWriter, r *http.Request)
- func (a *BasicAuth) SendInvalidSchemeResponse(w http.ResponseWriter, r *http.Request)
- func (a *BasicAuth) SetWWWAuthenticate(w http.ResponseWriter)
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func CompareInputs ¶
CompareInputs is to safe compare two inputs (prevents timing attacks).
Types ¶
type BasicAuth ¶
type BasicAuth struct { Authenticator func(username, password string) bool // Custom callback to find out the validity of a user's authentication process. This can be implemented in any implementation detail (for example: DB calls). Charset string // Custom charset to be passed in the `WWW-Authenticate` header. According to RFC 7617, this has to be 'UTF-8'. InvalidCredentialsResponse http.Handler // Callback to be invoked after receiving an InvalidCredentials error. InvalidSchemeResponse http.Handler // Callback to be invoked after receiving an InvalidScheme error. Realm string // Specific realm for an authorization endpoint. This can be an arbitrary string. Users map[string]string // Static credentials for all users. Can be `nil` if need be. }
BasicAuth is used to configure all the library options.
func NewCustomBasicAuth ¶
func NewCustomBasicAuth( authenticator func(username, password string) bool, charset string, invalidCredentialsResponse http.Handler, invalidSchemeResponse http.Handler, realm string, users map[string]string, ) *BasicAuth
NewCustomBasicAuth is used to set up Basic Auth options with customizable configurations.
func NewDefaultBasicAuth ¶
NewDefaultBasicAuth is used to set up Basic Auth options with default configurations.
func (*BasicAuth) Authenticate ¶
func (a *BasicAuth) Authenticate(next http.HandlerFunc) http.HandlerFunc
Authenticate is a middleware to safeguard a route with the updated version of Basic Authentication (RFC 7617).
func (*BasicAuth) SendInvalidCredentialsResponse ¶
func (a *BasicAuth) SendInvalidCredentialsResponse(w http.ResponseWriter, r *http.Request)
SendInvalidCredentialsResponse is used to send back an invalid response if the Basic Authorization credentials are invalid.
func (*BasicAuth) SendInvalidSchemeResponse ¶
func (a *BasicAuth) SendInvalidSchemeResponse(w http.ResponseWriter, r *http.Request)
SendInvalidSchemeResponse is used to send back invalid response if the Basic Authorization header is not in the proper format.
func (*BasicAuth) SetWWWAuthenticate ¶
func (a *BasicAuth) SetWWWAuthenticate(w http.ResponseWriter)
SetWWWAuthenticate sets the `WWW-Authenticate` network header on the API response payload. If the charset and realm are both empty, we do not set the `WWW-Authenticate` header.