(handler http.Handler, credential string)
| 24 | } |
| 25 | |
| 26 | func (server *Server) wrapBasicAuth(handler http.Handler, credential string) http.Handler { |
| 27 | return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 28 | token := strings.SplitN(r.Header.Get("Authorization"), " ", 2) |
| 29 | |
| 30 | if len(token) != 2 || strings.ToLower(token[0]) != "basic" { |
| 31 | w.Header().Set("WWW-Authenticate", `Basic realm="GoTTY"`) |
| 32 | http.Error(w, "Bad Request", http.StatusUnauthorized) |
| 33 | return |
| 34 | } |
| 35 | |
| 36 | payload, err := base64.StdEncoding.DecodeString(token[1]) |
| 37 | if err != nil { |
| 38 | http.Error(w, "Internal Server Error", http.StatusInternalServerError) |
| 39 | return |
| 40 | } |
| 41 | |
| 42 | if credential != string(payload) { |
| 43 | w.Header().Set("WWW-Authenticate", `Basic realm="GoTTY"`) |
| 44 | http.Error(w, "authorization failed", http.StatusUnauthorized) |
| 45 | return |
| 46 | } |
| 47 | |
| 48 | log.Printf("Basic Authentication Succeeded: %s", r.RemoteAddr) |
| 49 | handler.ServeHTTP(w, r) |
| 50 | }) |
| 51 | } |
no test coverage detected