2018-11-19 12:15:18 -06:00
|
|
|
package api
|
2018-11-14 14:42:47 -06:00
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/subtle"
|
|
|
|
macaron "gopkg.in/macaron.v1"
|
|
|
|
)
|
|
|
|
|
2018-11-19 12:15:18 -06:00
|
|
|
// BasicAuthenticatedRequest parses the provided HTTP request for basic authentication credentials
|
2018-11-14 14:42:47 -06:00
|
|
|
// and returns true if the provided credentials match the expected username and password.
|
|
|
|
// Returns false if the request is unauthenticated.
|
|
|
|
// Uses constant-time comparison in order to mitigate timing attacks.
|
|
|
|
func BasicAuthenticatedRequest(req macaron.Request, expectedUser, expectedPass string) bool {
|
|
|
|
user, pass, ok := req.BasicAuth()
|
2018-11-14 16:37:32 -06:00
|
|
|
if !ok || subtle.ConstantTimeCompare([]byte(user), []byte(expectedUser)) != 1 || subtle.ConstantTimeCompare([]byte(pass), []byte(expectedPass)) != 1 {
|
2018-11-14 14:42:47 -06:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
return true
|
|
|
|
}
|