-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
105 lines (97 loc) · 2.8 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
97
98
99
100
101
102
103
104
105
package main
import (
"fmt"
to_do_func "go-rest-api/crud"
"go-rest-api/helpers"
"go-rest-api/login"
"go-rest-api/models"
"go-rest-api/utils"
"net/http"
"strings"
"github.com/go-chi/chi/v5"
"github.com/go-chi/jwtauth/v5"
)
func main() {
//create chi router
r := chi.NewRouter()
//token verification and authorization
utils.InitTokenAuth()
r.Use(jwtauth.Verifier(utils.TokenAuth()))
r.Post("/login", func(w http.ResponseWriter, r *http.Request) {
r.ParseForm()
uName := r.Form.Get("username")
pwd := r.Form.Get("password")
uNameCheck := helpers.IsEmpty(uName)
pwdCheck := helpers.IsEmpty(pwd)
if uNameCheck || pwdCheck {
fmt.Fprintf(w, "There is empty data.")
return
}
//login check
if login.Login(uName, pwd) {
role := "user"
if uName == "admin" {
role = "admin"
}
token, err := utils.GenerateToken(models.JwtModel{
Name: uName,
Password: pwd,
Role: role,
})
if err != nil {
http.Error(w, "Failed to generate token", http.StatusInternalServerError)
return
}
fmt.Fprintf(w, "Login Successful.\n Token:\n %s", token)
} else {
fmt.Fprintf(w, "Login failed")
}
})
//users endpoint
r.Route("/{username}", func(r chi.Router) {
r.Use(OnlyUsers)
r.Get("/todos", to_do_func.ListTodos)
r.Post("/todos", to_do_func.CreateTodo)
r.Put("/todos/{id}", to_do_func.UpdateTodo)
r.Delete("/todos/{id}", to_do_func.DeleteTodo)
})
//admin endpoint
r.Route("/admin", func(r chi.Router) {
r.Use(AdminOnly)
r.Get("/todos", to_do_func.ListAllTodos)
r.Post("/todos", to_do_func.AdminCreateOwnTodo)
r.Put("/todos/{id}", to_do_func.AdminUpdateOwnTodo)
r.Delete("/todos/{id}", to_do_func.AdminDeleteOwnTodo)
// authorized endpoint
r.Get("/users/{username}/todos", to_do_func.AdminListUserTodos)
r.Post("/users/{username}/todos", to_do_func.AdminCreateUserTodo)
r.Put("/users/{username}/todos/{id}", to_do_func.AdminUpdateUserTodo)
r.Delete("/users/{username}/todos/{id}", to_do_func.AdminDeleteUserTodo)
})
http.ListenAndServe(":8080", r)
}
// check user permissions
func OnlyUsers(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
_, claims, _ := jwtauth.FromContext(r.Context())
tokenUserName := strings.ToLower(claims["name"].(string))
urlUserName := strings.ToLower(chi.URLParam(r, "username"))
if tokenUserName != urlUserName {
http.Error(w, "Not Allowed", http.StatusForbidden)
return
}
next.ServeHTTP(w, r)
})
}
// check admin permissions
func AdminOnly(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
_, claims, _ := jwtauth.FromContext(r.Context())
role := claims["role"].(string)
if role != "admin" {
http.Error(w, "Not Allowed", http.StatusForbidden)
return
}
next.ServeHTTP(w, r)
})
}