-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfunction_for_frontend_with_netlify_jwt.go
118 lines (112 loc) · 2.83 KB
/
function_for_frontend_with_netlify_jwt.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
106
107
108
109
110
111
112
113
114
115
116
117
118
package p
import (
"errors"
"fmt"
"log"
"net/http"
"os"
"time"
"github.com/asendia/legacy-api/api"
"github.com/asendia/legacy-api/data"
"github.com/asendia/legacy-api/secure"
)
// Google Cloud Function
func CloudFunctionForFrontendWithNetlifyJWT(w http.ResponseWriter, r *http.Request) {
corsStatus, err := api.VerifyCORS(w, r)
if err != nil || corsStatus != http.StatusAccepted {
return
}
// Verifying auth
jwtRes, err := VerifyNetlifyJWT(r)
if err != nil {
log.Println(err.Error())
w.Header().Set("Content-Type", "application/json")
http.Error(w, `{"err":"Decode and verify auth failed"}`, http.StatusForbidden)
return
}
// Establishing connection to database
ctx := r.Context()
conn, err := data.ConnectDB(ctx, data.LoadDBURLConfig())
if err != nil {
log.Printf("Cannot connect to the database: %v\n", err.Error())
http.Error(w, "Internal server error", http.StatusInternalServerError)
return
}
defer conn.Close()
tx, err := conn.Begin(ctx)
if err != nil {
log.Printf("Cannot begin database transaction: %v\n", err.Error())
http.Error(w, "Internal server error", http.StatusInternalServerError)
return
}
defer tx.Rollback(ctx)
// Init API controller
a := api.APIForFrontend{
Context: ctx,
Tx: tx,
}
var res api.APIResponse
action := r.URL.Query().Get("action")
switch action {
case "insert-message":
p, errP := api.ParseReqInsertMessage(r)
if errP != nil {
err = errP
break
}
res, err = a.InsertMessage(jwtRes, p)
break
case "select-messages":
res, err = a.SelectMessagesByEmailCreator(jwtRes)
break
case "update-message":
p, errP := api.ParseReqUpdateMessage(r)
if errP != nil {
err = errP
break
}
res, err = a.UpdateMessage(jwtRes, p)
break
case "delete-message":
p, errP := api.ParseReqDeleteMessage(r)
if errP != nil {
err = errP
break
}
res, err = a.DeleteMessage(jwtRes, p.ID)
break
default:
err = errors.New("Invalid Action")
res.StatusCode = http.StatusNotFound
break
}
w.Header().Set("Content-Type", "application/json")
// Handle controller error
if err != nil {
log.Println(err.Error())
http.Error(w, `{"err":"`+err.Error()+`"}`, res.GetValidStatusCode())
return
}
// Generate response
resStr, err := res.ToString()
if err != nil {
log.Println(err.Error())
http.Error(w, `{"err":"Cannot generate a response"}`, http.StatusInternalServerError)
return
}
tx.Commit(ctx)
fmt.Fprint(w, resStr)
}
func VerifyNetlifyJWT(r *http.Request) (jwtRes secure.JWTResponse, err error) {
// Always login with this email during test or cmd
jwtRes = secure.JWTResponse{Email: "test@sejiwo.com"}
// Verify JWT token on prod env only
if os.Getenv("ENVIRONMENT") == "prod" {
client := &http.Client{Timeout: time.Second * 10}
jwtRes, err = secure.VerifyNetlifyJWT(client, r.Header.Get("authorization"))
if err != nil {
return
}
}
return
}