Skip to content

Instantly share code, notes, and snippets.

@viggy28 viggy28/verifyauth.go
Last active Mar 21, 2019

Embed
What would you like to do?
Performs basic authentication check in go
// VerifyAuth verifies the basic authentication
func VerifyAuth(r *http.Request) (bool, error) {
username, password, ok := r.BasicAuth()
if !ok {
return false, errors.New("couldn't invoke BasicAuth()")
}
APIUSERNAME := os.Getenv("APIUSERNAME")
APIPASSWORD := os.Getenv("APIPASSWORD")
if APIUSERNAME == "" || APIPASSWORD == "" {
return false, errors.New("APIUSERNAME or APIPASSWORD is not set")
}
if username != APIUSERNAME || password != APIPASSWORD {
return false, errors.New("invalid credentials")
}
return true, nil
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.