-
-
Notifications
You must be signed in to change notification settings - Fork 6
/
x3.go
102 lines (94 loc) · 2.78 KB
/
x3.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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
package main
import (
"fmt"
"io"
"net/http"
"net/url"
"golang.org/x/crypto/bcrypt"
)
type application struct {
config struct {
port string
path string
cert string
key string
}
user struct {
name string
hash string
}
}
func main() {
// Instantiate app with some application data
app := new(application)
// Setup the config data
app.config.port = "8003"
app.config.path = "./www"
app.config.cert = "localhost.crt"
app.config.key = "localhost.key"
// Setup the user data
app.user.name = "admin"
app.user.hash = "$2a$14$A51GrX.lqVNioLKUVQSZoulowhdFq2mrFYmc/A4Um6CuUpwRREZlO" // 1234
// Setup some routes
http.HandleFunc("/", app.fileHandler)
http.HandleFunc("/hello", app.helloHandler)
http.HandleFunc("/hash", app.hashHandler)
// Start a server
fmt.Printf("Server started on port %s\n", app.config.port)
if err := http.ListenAndServeTLS(":"+app.config.port, app.config.cert, app.config.key, nil); err != nil {
fmt.Println(err)
}
}
// A function that authenticates the user and returns true or false
func (app *application) auth(w http.ResponseWriter, r *http.Request) bool {
// Grab the username, password, and a verification that they were formatted ok in the request
user, pass, ok := r.BasicAuth()
// If the header includes credentials
if ok {
// If the username is correct
if user == app.user.name {
// If the passwords match
if bcrypt.CompareHashAndPassword([]byte(app.user.hash), []byte(pass)) == nil {
return true
}
}
}
// Set a header reporting unauthorized and requesting credentials
w.Header().Set("WWW-Authenticate", `Basic realm="restricted", charset="UTF-8"`)
http.Error(w, "Unauthorized", http.StatusUnauthorized)
return false
}
func (app *application) fileHandler(w http.ResponseWriter, r *http.Request) {
// Authorize the user and return on failure
if !app.auth(w, r) {
return
}
// Parse the url for the path
u, err := url.ParseRequestURI(r.RequestURI)
if err != nil {
fmt.Println("Unable to parse url.")
http.Error(w, "Bad request", http.StatusBadRequest)
return
}
fmt.Println("Serving " + app.config.path + u.Path)
// Serve a file from the default directory
http.ServeFile(w, r, app.config.path+u.Path)
}
// A handler that authorizes the user and says hello
func (app *application) helloHandler(w http.ResponseWriter, r *http.Request) {
// Authorize the user and return on failure
if !app.auth(w, r) {
return
}
// Say hello
w.Header().Add("content-type", "text/html")
io.WriteString(w, "Hello World.\n")
}
// An example of how to hash a password and store it
func (app *application) hashHandler(w http.ResponseWriter, r *http.Request) {
pass := r.URL.Query().Get("pass")
bytes, _ := bcrypt.GenerateFromPassword([]byte(pass), 14)
hash := string(bytes)
w.Header().Add("content-type", "text/html")
io.WriteString(w, hash)
}