This repository was archived by the owner on Feb 7, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhandlers.go
210 lines (169 loc) · 5.36 KB
/
handlers.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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
package main
import (
"net/http"
"context"
"encoding/json"
"fmt"
"io/ioutil"
"os"
"strings"
"github.com/gorilla/mux"
"github.com/joho/godotenv"
"github.com/magiclabs/magic-admin-go"
"github.com/magiclabs/magic-admin-go/client"
"github.com/magiclabs/magic-admin-go/token"
)
type httpHandlerFunc func(http.ResponseWriter, *http.Request)
type key string
const userInfoKey key = "userInfo"
const authBearer = "Bearer"
// Load .env file from given path
var err = godotenv.Load(".env")
// Get env variables
var magicSecretKey = os.Getenv("MAGIC_SECRET_KEY")
// Instantiate Magic ✨
var magicSDK = client.New(magicSecretKey, magic.NewDefaultClient())
// Handler for the server's homepage ✨
func serverHomePage(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "Welcome to our Twitter page!")
fmt.Println("Endpoint Hit: serverHomePage")
}
// Returns ALL tweets ✨
func returnAllTweets(w http.ResponseWriter, r *http.Request) {
fmt.Println("Endpoint Hit: returnAllTweets")
json.NewEncoder(w).Encode(Tweets)
}
// Returns a SINGLE tweet ✨
func returnSingleTweet(w http.ResponseWriter, r *http.Request) {
fmt.Println("Endpoint Hit: returnSingleTweet")
vars := mux.Vars(r)
key := vars["id"]
/*
Loop over all of our Tweets
If the tweet.Id equals the key we pass in
Return the tweet encoded as JSON
*/
for _, tweet := range Tweets {
if tweet.ID == key {
json.NewEncoder(w).Encode(tweet)
}
}
}
// Creates a tweet ✨
func createATweet(w http.ResponseWriter, r *http.Request) {
fmt.Println("Endpoint Hit: createATweet")
// Get the authenticated author's info from context values
userInfo := r.Context().Value(userInfoKey)
userInfoMap := userInfo.(*magic.UserInfo)
/*
Get the body of our POST request
Unmarshal this into a new Tweet struct
Add the authenticated author to the tweet
*/
reqBody, _ := ioutil.ReadAll(r.Body)
var tweet Tweet
json.Unmarshal(reqBody, &tweet)
tweet.Author = userInfoMap.Email
/*
Update our global Tweets array to include
Our new Tweet
*/
Tweets = append(Tweets, tweet)
json.NewEncoder(w).Encode(tweet)
fmt.Println("Yay! Tweet CREATED.")
}
// Deletes a tweet ✨
func deleteATweet(w http.ResponseWriter, r *http.Request) {
fmt.Println("Endpoint Hit: deleteATweet")
// Get the authenticated author's info from context values
userInfo := r.Context().Value(userInfoKey)
userInfoMap := userInfo.(*magic.UserInfo)
// Parse the path parameters
vars := mux.Vars(r)
// Extract the `id` of the tweet we wish to delete
id := vars["id"]
// Loop through all our tweets
for index, tweet := range Tweets {
/*
Checks whether or not our id and author path
parameter matches one of our tweets.
*/
if (tweet.ID == id) && (tweet.Author == userInfoMap.Email) {
// Updates our Tweets array to remove the tweet
Tweets = append(Tweets[:index], Tweets[index+1:]...)
w.Write([]byte("Yay! Tweet has been DELETED."))
return
}
}
w.Write([]byte("Ooh. You can't delete someone else's tweet."))
}
/*
Ensures the Decentralised ID Token (DIDT) sent by the client is valid
Saves the author's user info in context values ✨
*/
func checkBearerToken(next httpHandlerFunc) httpHandlerFunc {
return func(res http.ResponseWriter, req *http.Request) {
// Check whether or not DIDT exists in HTTP Header Request
if !strings.HasPrefix(req.Header.Get("Authorization"), authBearer) {
fmt.Fprintf(res, "Bearer token is required")
return
}
// Retrieve DIDT token from HTTP Header Request
didToken := req.Header.Get("Authorization")[len(authBearer)+1:]
// Create a Token instance to interact with the DID token
tk, err := token.NewToken(didToken)
if err != nil {
fmt.Fprintf(res, "Malformed DID token error: %s", err.Error())
res.Write([]byte(err.Error()))
return
}
// Validate the Token instance before using it
if err := tk.Validate(); err != nil {
fmt.Fprintf(res, "DID token failed validation: %s", err.Error())
return
}
// Get the the user's information
userInfo, err := magicSDK.User.GetMetadataByIssuer(tk.GetIssuer())
if err != nil {
fmt.Fprintf(res, "Error when calling GetMetadataByIssuer: %s", err.Error())
return
}
// Use context values to store user's info
ctx := context.WithValue(req.Context(), userInfoKey, userInfo)
req = req.WithContext(ctx)
next(res, req)
}
}
// Save authenticated user info once they login from the client side ✨
func saveUserInfo(w http.ResponseWriter, r *http.Request) {
fmt.Println("Endpoint Hit: save-user-info")
// Get the authenticated author's info from context values
userInfo := r.Context().Value(userInfoKey)
userInfoMap := userInfo.(*magic.UserInfo)
// Get body of our POST request
reqBody, _ := ioutil.ReadAll(r.Body)
var user User
// Unmarshal JSON data into a new User struct
json.Unmarshal(reqBody, &user)
/*
Marshal User struct into JSON data to
access key-value pair.
*/
json.Marshal(user)
/*
If the email sent by the client does not match
the email saved via Magic SDK, then it is an
unauthorized login.
*/
if userInfoMap.Email != user.Email {
fmt.Fprintf(w, "Unauthorized user login")
return
}
/*
If you wanted, you could call your application logic to save the user's info.
E.g.
logic.User.add(userInfoMap.Email, userInfoMap.Issuer, userInfo.PublicAddress)
*/
// Instead of saving the user's info, we'll just return it
w.Write([]byte("Yay! User was able to login / sign up. 🪄 Email: " + user.Email))
}