-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
96 lines (82 loc) · 2.02 KB
/
main.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
package main
import (
"encoding/json"
"io/ioutil"
"log"
"net/http"
"os"
"github.com/eschudt/jwt-auth/auth"
"github.com/eschudt/jwt-auth/verifier"
)
type JwtToken struct {
Token string
}
var (
logger *log.Logger
authService *auth.Service
verifyService *verifier.Service
credentials []auth.LoginDetail
)
func main() {
listenPort := os.Getenv("NOMAD_PORT_http")
logger = log.New(os.Stderr, "", 0)
login1 := auth.LoginDetail{
Username: "user1",
Password: "password1",
AccountID: "1",
}
login2 := auth.LoginDetail{
Username: "user2",
Password: "password2",
AccountID: "2",
}
credentials = append(credentials, login1)
credentials = append(credentials, login2)
authService = auth.New(credentials)
verifyService = verifier.New()
mux := http.NewServeMux()
mux.HandleFunc("/", helloHandler)
mux.HandleFunc("/login", loginHandler)
mux.HandleFunc("/verify", verifyHandler)
http.ListenAndServe(":"+listenPort, mux)
}
func helloHandler(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
}
func loginHandler(w http.ResponseWriter, r *http.Request) {
body, err := ioutil.ReadAll(r.Body)
if err != nil {
logger.Fatal(err)
}
var t auth.LoginDetail
err = json.Unmarshal(body, &t)
if err != nil {
logger.Fatal(err)
}
jwtToken := authService.Login(t.Username, t.Password)
if jwtToken == "" {
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusUnauthorized)
return
}
data := JwtToken{jwtToken}
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
resp, err := json.Marshal(data)
if err != nil {
logger.Fatal(err)
}
w.Write(resp)
}
func verifyHandler(w http.ResponseWriter, r *http.Request) {
token := r.Header.Get("Authorization")
requesterDetails := verifyService.Verify(token)
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
resp, err := json.Marshal(requesterDetails)
if err != nil {
logger.Fatal(err)
}
w.Write(resp)
}