Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@viggy28
Last active March 21, 2019 02:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save viggy28/67249b54c60463faa9117fff9297d739 to your computer and use it in GitHub Desktop.
Save viggy28/67249b54c60463faa9117fff9297d739 to your computer and use it in GitHub Desktop.
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