6
6
"log"
7
7
"strings"
8
8
9
- "github.com/fasthttp/router"
10
9
"github.com/elithrar/simple-scrypt"
10
+ "github.com/fasthttp/router"
11
11
"github.com/valyala/fasthttp"
12
12
)
13
13
@@ -46,34 +46,34 @@ func BasicAuth(h fasthttp.RequestHandler, requiredUser string, requiredPasswordH
46
46
return fasthttp .RequestHandler (func (ctx * fasthttp.RequestCtx ) {
47
47
// Get the Basic Authentication credentials
48
48
user , password , hasAuth := basicAuth (ctx )
49
-
50
- // WARNING:
49
+
50
+ // WARNING:
51
51
// DO NOT use plain-text passwords for real apps.
52
52
// A simple string comparison using == is vulnerable to a timing attack.
53
53
// Instead, use the hash comparison function found in your hash library.
54
54
// This example uses scrypt, which is a solid choice for secure hashing:
55
55
// go get -u github.com/elithrar/simple-scrypt
56
-
56
+
57
57
if hasAuth && user == requiredUser {
58
-
58
+
59
59
// Uses the parameters from the existing derived key. Return an error if they don't match.
60
60
err := scrypt .CompareHashAndPassword (requiredPasswordHash , []byte (password ))
61
61
62
- if err != nil {
63
-
62
+ if err != nil {
63
+
64
64
// log error and request Basic Authentication again below.
65
65
log .Fatal (err )
66
-
67
- } else {
68
-
66
+
67
+ } else {
68
+
69
69
// Delegate request to the given handle
70
70
h (ctx )
71
71
return
72
-
73
- }
74
-
72
+
73
+ }
74
+
75
75
}
76
-
76
+
77
77
// Request Basic Authentication otherwise
78
78
ctx .Error (fasthttp .StatusMessage (fasthttp .StatusUnauthorized ), fasthttp .StatusUnauthorized )
79
79
ctx .Response .Header .Set ("WWW-Authenticate" , "Basic realm=Restricted" )
@@ -93,12 +93,12 @@ func Protected(ctx *fasthttp.RequestCtx) {
93
93
func main () {
94
94
user := "gordon"
95
95
pass := "secret!"
96
-
96
+
97
97
// generate a hashed password from the password above:
98
98
hashedPassword , err := scrypt .GenerateFromPassword ([]byte (pass ), scrypt .DefaultParams )
99
- if err != nil {
100
- log .Fatal (err )
101
- }
99
+ if err != nil {
100
+ log .Fatal (err )
101
+ }
102
102
103
103
r := router .New ()
104
104
r .GET ("/" , Index )
0 commit comments