|
@@ -1,48 +1,37 @@
|
|
|
package main
|
|
|
|
|
|
import (
|
|
|
- "crypto/sha256"
|
|
|
- "crypto/subtle"
|
|
|
+ "log"
|
|
|
"net/http"
|
|
|
-)
|
|
|
+ "os"
|
|
|
+ "time"
|
|
|
|
|
|
-func basicAuth(next http.HandlerFunc) http.HandlerFunc {
|
|
|
- return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
- username, password, ok := r.BasicAuth()
|
|
|
- if ok {
|
|
|
-
|
|
|
-
|
|
|
- usernameHash := sha256.Sum256([]byte(username))
|
|
|
- passwordHash := sha256.Sum256([]byte(password))
|
|
|
- expectedUsernameHash := sha256.Sum256([]byte("your expected username"))
|
|
|
- expectedPasswordHash := sha256.Sum256([]byte("your expected password"))
|
|
|
+ "code.osinet.fr/fgm/web_auth_demo/server"
|
|
|
+)
|
|
|
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
- usernameMatch := 1 == subtle.ConstantTimeCompare(usernameHash[:], expectedUsernameHash[:])
|
|
|
- passwordMatch := 1 == subtle.ConstantTimeCompare(passwordHash[:], expectedPasswordHash[:])
|
|
|
+func main() {
|
|
|
+ app, err := server.New(
|
|
|
+ os.Getenv("AUTH_USERNAME"),
|
|
|
+ os.Getenv("AUTH_PASSWORD"),
|
|
|
+ os.Getenv("AUTH_CERT"),
|
|
|
+ os.Getenv("AUTH_CERT_KEY"),
|
|
|
+ )
|
|
|
+ if err != nil {
|
|
|
+ log.Fatal(err)
|
|
|
+ }
|
|
|
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
- if usernameMatch && passwordMatch {
|
|
|
- next.ServeHTTP(w, r)
|
|
|
- return
|
|
|
- }
|
|
|
- }
|
|
|
+ mux := http.NewServeMux()
|
|
|
+ mux.HandleFunc("/unprotected", app.UnprotectedHandler)
|
|
|
+ mux.HandleFunc("/protected", app.BasicAuth(app.ProtectedHandler))
|
|
|
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
- w.Header().Set("WWW-Authenticate", `Basic realm="restricted", charset="UTF-8"`)
|
|
|
- http.Error(w, http.StatusText(http.StatusUnauthorized), http.StatusUnauthorized)
|
|
|
- })
|
|
|
+ srv := &http.Server{
|
|
|
+ Addr: ":4000",
|
|
|
+ Handler: mux,
|
|
|
+ ReadTimeout: 10 * time.Second,
|
|
|
+ WriteTimeout: 30 * time.Second,
|
|
|
+ IdleTimeout: time.Minute,
|
|
|
+ }
|
|
|
+ log.Printf("Starting TLS server on %s", srv.Addr)
|
|
|
+ err = srv.ListenAndServeTLS(app.Cert, app.Key)
|
|
|
+ log.Fatal(err)
|
|
|
}
|