-
Notifications
You must be signed in to change notification settings - Fork 0
/
libhttp.go
57 lines (48 loc) · 1.43 KB
/
libhttp.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
// Package libhttp provides http related library functions.
package libhttp
import (
"encoding/base64"
"encoding/json"
"fmt"
"net/http"
"strings"
)
// BasicRealm is used when setting the WWW-Authenticate response header.
var BasicRealm = "Authorization Required"
// ParseBasicAuth parses an HTTP Basic Authentication string.
// "Basic QWxhZGRpbjpvcGVuIHNlc2FtZQ==" returns ("Aladdin", "open sesame", true).
func ParseBasicAuth(auth string) (username, password string, ok bool) {
if !strings.HasPrefix(auth, "Basic ") {
return
}
c, err := base64.StdEncoding.DecodeString(strings.TrimPrefix(auth, "Basic "))
if err != nil {
return
}
cs := string(c)
s := strings.IndexByte(cs, ':')
if s < 0 {
return
}
return cs[:s], cs[s+1:], true
}
// BasicAuthUnauthorized denies authentication.
func BasicAuthUnauthorized(w http.ResponseWriter, err error) {
message := "Not Authorized."
if err != nil {
message += fmt.Sprintf(" Error: %v", err)
}
w.Header().Set("WWW-Authenticate", "Basic realm=\""+BasicRealm+"\"")
http.Error(w, message, http.StatusUnauthorized)
}
// HandleErrorJson wraps error in JSON structure.
func HandleErrorJson(w http.ResponseWriter, err error) {
var errMap map[string]string
if err == nil {
errMap = map[string]string{"Error": "Error struct is nil."}
} else {
errMap = map[string]string{"Error": err.Error()}
}
errJson, _ := json.Marshal(errMap)
http.Error(w, string(errJson), http.StatusInternalServerError)
}