Last active
March 21, 2019 02:12
-
-
Save viggy28/67249b54c60463faa9117fff9297d739 to your computer and use it in GitHub Desktop.
Performs basic authentication check in go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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