-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
74 lines (58 loc) · 1.84 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
package main
import (
"errors"
"log"
"net/http"
"os"
"strings"
"github.com/dgrijalva/jwt-go"
"github.com/gorilla/mux"
"github.com/lufanbb/Golang_REST_API_JWT_Auth/internal/driver"
"github.com/lufanbb/Golang_REST_API_JWT_Auth/internal/model"
"github.com/lufanbb/Golang_REST_API_JWT_Auth/internal/service"
"github.com/subosito/gotenv"
)
func init() {
gotenv.Load()
}
func main() {
db := driver.ConnectDB()
router := mux.NewRouter()
router.HandleFunc("/signup", service.Signup(db)).Methods("POST")
router.HandleFunc("/login", service.Login(db)).Methods("POST")
router.HandleFunc("/protected", TokenVerifyMiddleWare(service.ProtectedEndpoint)).Methods("GET")
log.Println("Listening on port 8000...")
log.Fatal(http.ListenAndServe(":8000", router))
}
// TokenVerifyMiddleWare verify the authorization token before it forwards the request to the route
func TokenVerifyMiddleWare(next http.HandlerFunc) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
var err model.Error
authHeader := r.Header.Get("Authorization")
bearerToken := strings.Split(authHeader, " ")
if len(bearerToken) == 2 {
authToken := bearerToken[1]
token, error := jwt.Parse(authToken, func(token *jwt.Token) (interface{}, error) {
if _, ok := token.Method.(*jwt.SigningMethodHMAC); !ok {
return nil, errors.New("The signin Method cannot be verified")
}
return []byte(os.Getenv("SECRET")), nil
})
if error != nil {
err.Message = error.Error()
service.RespondWithError(w, http.StatusUnauthorized, err)
return
}
if token.Valid {
next.ServeHTTP(w, r)
} else {
err.Message = "Token is not valid"
service.RespondWithError(w, http.StatusUnauthorized, err)
return
}
} else {
err.Message = "Token format is not correct"
service.RespondWithError(w, http.StatusUnauthorized, err)
}
}
}