Re-organize packages and add basic auth test

This commit is contained in:
Bob Shannon
2018-11-19 13:15:18 -05:00
parent dcc48860b8
commit 16ff8a182b
4 changed files with 82 additions and 6 deletions

19
pkg/api/basic_auth.go Normal file
View File

@ -0,0 +1,19 @@
package api
import (
"crypto/subtle"
macaron "gopkg.in/macaron.v1"
)
// BasicAuthenticatedRequest parses the provided HTTP request for basic authentication credentials
// 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()
if !ok || subtle.ConstantTimeCompare([]byte(user), []byte(expectedUser)) != 1 || subtle.ConstantTimeCompare([]byte(pass), []byte(expectedPass)) != 1 {
return false
}
return true
}