Created
January 21, 2020 18:54
-
-
Save nanom1t/460251efe1ec40ccba4f42ecade98a3f to your computer and use it in GitHub Desktop.
Golang basic auth example
This file contains hidden or 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
package main | |
import ( | |
"encoding/base64" | |
"net/http" | |
"strings" | |
) | |
type handler func(w http.ResponseWriter, r *http.Request) | |
func basicAuth(pass handler) handler { | |
return func(w http.ResponseWriter, r *http.Request) { | |
auth := strings.SplitN(r.Header.Get("Authorization"), " ", 2) | |
if len(auth) != 2 || auth[0] != "Basic" { | |
http.Error(w, "authorization failed", http.StatusUnauthorized) | |
return | |
} | |
payload, _ := base64.StdEncoding.DecodeString(auth[1]) | |
pair := strings.SplitN(string(payload), ":", 2) | |
if len(pair) != 2 || !validate(pair[0], pair[1]) { | |
http.Error(w, "authorization failed", http.StatusUnauthorized) | |
return | |
} | |
pass(w, r) | |
} | |
} | |
func validate(username, password string) bool { | |
if username == "test" && password == "test" { | |
return true | |
} | |
return false | |
} |
This file contains hidden or 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
package main | |
import ( | |
"log" | |
"net/http" | |
) | |
func main() { | |
// public views | |
http.HandleFunc("/", HandleIndex) | |
// private views | |
http.HandleFunc("/post", PostOnly(BasicAuth(HandlePost))) | |
http.HandleFunc("/json", GetOnly(BasicAuth(HandleJSON))) | |
log.Fatal(http.ListenAndServe(":8080", nil)) | |
} |
This file contains hidden or 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
package main | |
import ( | |
"encoding/json" | |
"io" | |
"log" | |
"net/http" | |
) | |
func HandleIndex(w http.ResponseWriter, r *http.Request) { | |
io.WriteString(w, "hello, world\n") | |
} | |
func HandlePost(w http.ResponseWriter, r *http.Request) { | |
r.ParseForm() | |
log.Println(r.PostForm) | |
io.WriteString(w, "post\n") | |
} | |
type Result struct { | |
FirstName string `json:"first"` | |
LastName string `json:"last"` | |
} | |
func HandleJSON(w http.ResponseWriter, r *http.Request) { | |
w.Header().Set("Content-Type", "application/json") | |
result, _ := json.Marshal(Result{"tee", "dub"}) | |
io.WriteString(w, string(result)) | |
} |
This file contains hidden or 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
curl http://0.0.0.0:8080 | |
curl -u username:password -d "param1=value1¶m2=value2" http://0.0.0.0:8080/post | |
curl -u username:password http://0.0.0.0:8080/json |
This file contains hidden or 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
package main | |
import ( | |
"net/http" | |
) | |
func GetOnly(h handler) handler { | |
return func(w http.ResponseWriter, r *http.Request) { | |
if r.Method == "GET" { | |
h(w, r) | |
return | |
} | |
http.Error(w, "get only", http.StatusMethodNotAllowed) | |
} | |
} | |
func PostOnly(h handler) handler { | |
return func(w http.ResponseWriter, r *http.Request) { | |
if r.Method == "POST" { | |
h(w, r) | |
return | |
} | |
http.Error(w, "post only", http.StatusMethodNotAllowed) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment