/
gateway.go
308 lines (266 loc) · 9.11 KB
/
gateway.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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
package controllers
import (
"encoding/json"
"io/ioutil"
"log"
"net/http"
"net/url"
"strconv"
"strings"
"time"
"github.com/google/uuid"
"golang.org/x/oauth2"
"github.com/bartmika/osin-example/internal/idos"
"github.com/bartmika/osin-example/internal/models"
"github.com/bartmika/osin-example/internal/utils"
"github.com/bartmika/osin-example/internal/validators"
)
// To run this API, try running in your console:
// $ http post 127.0.0.1:8000/api/v1/register email="fherbert@dune.com" password="the-spice-must-flow" name="Frank Herbert"
func (h *Controller) registerEndpoint(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
// Initialize our array which will store all the results from the remote server.
var requestData idos.RegisterRequestIDO
defer r.Body.Close()
// Read the JSON string and convert it into our golang stuct else we need
// to send a `400 Bad Request` errror message back to the client,
err := json.NewDecoder(r.Body).Decode(&requestData) // [1]
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
// Perform our validation and return validation error on any issues detected.
isValid, errStr := validators.ValidateRegisterRequest(&requestData)
if isValid == false {
http.Error(w, errStr, http.StatusBadRequest)
return
}
// // For debugging purposes, print our output so you can see the code working.
// fmt.Println(requestData.Name)
// fmt.Println(requestData.Email)
// fmt.Println(requestData.Password)
// Lookup the email and if it is not unique we need to generate a `400 Bad Request` response.
if userFound, _ := h.UserRepo.CheckIfExistsByEmail(ctx, requestData.Email); userFound {
http.Error(w, "Email alread exists", http.StatusBadRequest)
return
}
//
// Create our tenant organization.
//
t := &models.Tenant{
UUID: uuid.NewString(),
Name: uuid.NewString(),
State: models.TenantActiveState,
Timezone: "UTC",
Language: requestData.Language,
CreatedTime: time.Now().UTC(),
ModifiedTime: time.Now().UTC(),
}
// Save our new user account.
if err := h.TenantRepo.Insert(ctx, t); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
t, err = h.TenantRepo.GetByName(ctx, t.Name)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
//
// Create our user account.
//
// Secure our password.
passwordHash, err := utils.HashPassword(requestData.Password)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
m := &models.User{
UUID: uuid.NewString(),
TenantID: t.ID, // TenantID
Email: requestData.Email,
FirstName: requestData.FirstName,
LastName: requestData.LastName,
Name: requestData.FirstName + " " + requestData.LastName,
LexicalName: requestData.LastName + ", " + requestData.FirstName,
PasswordHash: passwordHash,
State: models.UserActiveState,
RoleID: models.UserTenantAdminRoleID,
Timezone: "UTC",
Language: requestData.Language,
CreatedTime: time.Now().UTC(),
ModifiedTime: time.Now().UTC(),
}
// Save our new user account.
if err := h.UserRepo.Insert(ctx, m); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
// Generate our response.
responseData := idos.RegisterResponseIDO{
Message: "You have successfully registered an account.",
}
if err := json.NewEncoder(w).Encode(&responseData); err != nil { // [2]
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
}
// To run this API, try running in your console:
// $ http post 127.0.0.1:8000/api/v1/login email="fherbert@dune.com" password="the-spice-must-flow"
func (h *Controller) loginEndpoint(w http.ResponseWriter, r *http.Request) {
defer r.Body.Close()
ctx := r.Context()
var requestData idos.LoginRequestIDO
err := json.NewDecoder(r.Body).Decode(&requestData)
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
// Perform our validation and return validation error on any issues detected.
isValid, errStr := validators.ValidateLoginRequest(&requestData)
if isValid == false {
http.Error(w, errStr, http.StatusBadRequest)
return
}
// DEVELOPERS NOTE:
// WE WILL TAKE ADVANTAGE OF OUR OAUTH SERVER AND MAKE THE CALL TO THE
// PASSWORD GRANT TO GET OUR TOKENS.
log.Println("Beginning Password Based Authorization")
client := &oauth2.Config{
ClientID: h.Config["ClientID"],
ClientSecret: h.Config["ClientSecret"],
Scopes: []string{"all"},
Endpoint: oauth2.Endpoint{
AuthURL: h.Config["ClientAuthURL"],
TokenURL: h.Config["ClientTokenURL"],
},
RedirectURL: h.Config["ClientReturnURL"],
}
// NOTE: https://pkg.go.dev/golang.org/x/oauth2#Config.PasswordCredentialsToken
token, err := client.PasswordCredentialsToken(ctx, requestData.Email, requestData.Password)
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
log.Println("Finshed Password Based Authorization")
// Finally return success.
responseData := idos.LoginResponseIDO{
FirstName: token.Extra("first_name").(string),
LastName: token.Extra("last_name").(string),
Email: token.Extra("email").(string),
RoleID: int8(token.Extra("role_id").(float64)),
TenantID: uint64(token.Extra("tenant_id").(float64)),
Language: token.Extra("language").(string),
AccessToken: token.AccessToken,
TokenType: token.TokenType,
RefreshToken: token.RefreshToken,
Expiry: token.Expiry,
}
if err := json.NewEncoder(w).Encode(&responseData); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
}
// To run this API, try running in your console:
// $ http post 127.0.0.1:8000/api/v1/refresh-token value="xxx"
func (h *Controller) postRefreshToken(w http.ResponseWriter, r *http.Request) {
defer r.Body.Close()
var requestData idos.RefreshTokenRequestIDO
err := json.NewDecoder(r.Body).Decode(&requestData)
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
// Perform our validation and return validation error on any issues detected.
isValid, errStr := validators.ValidateRefreshTokenRequest(&requestData)
if isValid == false {
http.Error(w, errStr, http.StatusBadRequest)
return
}
endpoint := h.Config["ClientTokenURL"]
data := url.Values{}
data.Set("grant_type", requestData.GrantType)
data.Set("refresh_token", requestData.RefreshToken)
//
// Submit the code.
//
preq, err := http.NewRequest("POST", endpoint, strings.NewReader(data.Encode()))
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
preq.Header.Add("Content-Type", "application/x-www-form-urlencoded")
preq.Header.Add("Content-Length", strconv.Itoa(len(data.Encode())))
preq.SetBasicAuth(h.Config["ClientID"], h.Config["ClientSecret"])
pclient := &http.Client{}
presp, err := pclient.Do(preq)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
defer presp.Body.Close()
if presp.StatusCode != 200 {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
// Read the response body
responseBytes, err := ioutil.ReadAll(presp.Body)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
// We will simply return the bytes! Thus skipping the marhsal/unmarhsal step.
w.Write(responseBytes)
}
func (h *Controller) profileEndpoint(w http.ResponseWriter, r *http.Request) {
// ctx := r.Context()
// userID := uint64(ctx.Value("user_id").(uint64))
// user, err := h.UserRepo.GetByID(ctx, userID)
//
// // Start our session.
// sessionExpiryTime := time.Hour * 24 * 7 // 1 week
// sessionUUID := uuid.NewString()
// err = h.SessionManager.SaveUser(ctx, sessionUUID, user, sessionExpiryTime)
// if err != nil {
// http.Error(w, err.Error(), http.StatusInternalServerError)
// return
// }
//
// // Generate our JWT token.
// accessToken, refreshToken, err := utils.GenerateJWTTokenPair(h.SecretSigningKeyBin, sessionUUID, sessionExpiryTime)
// if err != nil {
// http.Error(w, err.Error(), http.StatusInternalServerError)
// return
// }
//
// // Update our results.
// user.AccessToken = accessToken
// user.RefreshToken = refreshToken
// user.PasswordHash = ""
//
// // Return our serialized result.
// if err := json.NewEncoder(w).Encode(&user); err != nil {
// http.Error(w, err.Error(), http.StatusInternalServerError)
// return
// }
}
func (h *Controller) getVersion(w http.ResponseWriter, req *http.Request) {
w.Write([]byte("OSIN Example v1.0"))
}
// API endpoint used to test out making posts to our API gateway. Here is an example:
//
// http post 127.0.0.1:8000/api/v1/greeting name=Bart
//
func (h *Controller) postGreeting(w http.ResponseWriter, r *http.Request) {
var requestData idos.GreetingRequestIDO
if err := json.NewDecoder(r.Body).Decode(&requestData); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
responseData := idos.GreetingResponseIDO{
Message: "Hello," + requestData.Name,
}
if err := json.NewEncoder(w).Encode(&responseData); err != nil { // [2]
http.Error(w, err.Error(), http.StatusInternalServerError)
}
}