forked from gogf/gf
/
ghttp_request_auth.go
65 lines (59 loc) · 1.71 KB
/
ghttp_request_auth.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
58
59
60
61
62
63
64
65
// Copyright GoFrame Author(https://goframe.org). All Rights Reserved.
//
// This Source Code Form is subject to the terms of the MIT License.
// If a copy of the MIT was not distributed with this file,
// You can obtain one at https://github.com/gogf/gf.
package ghttp
import (
"fmt"
"net/http"
"strings"
"github.com/gogf/gf/encoding/gbase64"
)
// BasicAuth enables the http basic authentication feature with given passport and password
// and asks client for authentication. It returns true if authentication success, else returns
// false if failure.
func (r *Request) BasicAuth(user, pass string, tips ...string) bool {
auth := r.Header.Get("Authorization")
if auth == "" {
r.setBasicAuth(tips...)
return false
}
authArray := strings.SplitN(auth, " ", 2)
if len(authArray) != 2 {
r.Response.WriteStatus(http.StatusForbidden)
return false
}
switch authArray[0] {
case "Basic":
authBytes, err := gbase64.DecodeString(authArray[1])
if err != nil {
r.Response.WriteStatus(http.StatusForbidden, err.Error())
return false
}
authArray := strings.SplitN(string(authBytes), ":", 2)
if len(authArray) != 2 {
r.Response.WriteStatus(http.StatusForbidden)
return false
}
if authArray[0] != user || authArray[1] != pass {
r.setBasicAuth(tips...)
return false
}
return true
default:
r.Response.WriteStatus(http.StatusForbidden)
return false
}
}
// setBasicAuth sets the http basic authentication tips.
func (r *Request) setBasicAuth(tips ...string) {
realm := ""
if len(tips) > 0 && tips[0] != "" {
realm = tips[0]
} else {
realm = "Need Login"
}
r.Response.Header().Set("WWW-Authenticate", fmt.Sprintf(`Basic realm="%s"`, realm))
r.Response.WriteHeader(http.StatusUnauthorized)
}