-
Notifications
You must be signed in to change notification settings - Fork 3
/
basic_auth.go
80 lines (70 loc) · 2.17 KB
/
basic_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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
// Copyright 2017 King Qiu. All rights reserved.
// Use of this source code is governed by a MIT style
// license that can be found in the LICENSE file.
// https://github.com/qjw/kelly
// https://github.com/martini-contrib/auth
package middleware
import (
"github.com/qjw/kelly"
"crypto/sha256"
"crypto/subtle"
"encoding/base64"
"net/http"
"strings"
)
// SecureCompare performs a constant time compare of two strings to limit timing attacks.
func secureCompare(given string, actual string) bool {
givenSha := sha256.Sum256([]byte(given))
actualSha := sha256.Sum256([]byte(actual))
return subtle.ConstantTimeCompare(givenSha[:], actualSha[:]) == 1
}
// BasicRealm is used when setting the WWW-Authenticate response header.
var BasicRealm = "Authorization Required"
// Basic returns a Handler that authenticates via Basic Auth. Writes a http.StatusUnauthorized
// if authentication fails.
func Basic(username string, password string) kelly.HandlerFunc {
var siteAuth = base64.StdEncoding.EncodeToString([]byte(username + ":" + password))
return func(c *kelly.Context) {
if auth,err := c.GetHeader("Authorization");err == nil{
if !secureCompare(auth, "Basic "+siteAuth) {
unauthorized(c)
return
}
c.Set("user",username)
c.InvokeNext()
}else{
unauthorized(c)
}
}
}
// BasicFunc returns a Handler that authenticates via Basic Auth using the provided function.
// The function should return true for a valid username/password combination.
func BasicFunc(authfn func(string, string) bool) kelly.HandlerFunc {
return func(c *kelly.Context) {
auth,err := c.GetHeader("Authorization")
if err != nil{
unauthorized(c)
return
}
if len(auth) < 6 || auth[:6] != "Basic " {
unauthorized(c)
return
}
b, err := base64.StdEncoding.DecodeString(auth[6:])
if err != nil {
unauthorized(c)
return
}
tokens := strings.SplitN(string(b), ":", 2)
if len(tokens) != 2 || !authfn(tokens[0], tokens[1]) {
unauthorized(c)
return
}
c.Set("user",tokens[0])
c.InvokeNext()
}
}
func unauthorized(res http.ResponseWriter) {
res.Header().Set("WWW-Authenticate", "Basic realm=\""+BasicRealm+"\"")
http.Error(res, "Not Authorized", http.StatusUnauthorized)
}