-
Notifications
You must be signed in to change notification settings - Fork 0
/
handler.go
299 lines (267 loc) · 8.77 KB
/
handler.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
package auth
import (
"errors"
"fmt"
"net/http"
"strings"
"time"
"github.com/dgrijalva/jwt-go"
"github.com/gofrs/uuid"
"github.com/gorilla/mux"
"github.com/jinzhu/gorm"
"github.com/loopcontext/auth-api-go/gen"
"github.com/loopcontext/auth-api-go/src/auth/database"
"github.com/loopcontext/auth-api-go/src/auth/models"
"github.com/loopcontext/auth-api-go/src/auth/rest"
"github.com/loopcontext/auth-api-go/src/utils"
"github.com/markbates/goth/gothic"
"github.com/rs/zerolog/log"
)
var (
errEmailPass = errors.New("please enter a correct email address and password")
domain = "app"
)
// Register entry point of the slsfn /v{X}/auth/register
func Register(db *gen.DB) func(http.ResponseWriter, *http.Request) {
return func(res http.ResponseWriter, req *http.Request) {
var input models.RegisterInput
err := rest.ReadJSON(res, req, &input)
if err != nil {
rest.HandleErr(res, req, err)
return
}
err = input.Validate()
if err != nil {
rest.HandleErr(res, req, err)
return
}
log.Debug().Msgf("Registering user: %+v", input)
passwd, err := utils.HashPassword(input.Password)
if err != nil {
rest.HandleErr(res, req, err)
return
}
user := &gen.User{
Active: true,
DisplayName: utils.StrToPtrStr(input.FirstName + " " + input.LastName),
FirstName: utils.StrToPtrStr(input.FirstName),
LastName: utils.StrToPtrStr(input.LastName),
Password: &passwd,
Email: input.Email,
}
resp := rest.Response{Status: http.StatusUnprocessableEntity}
if db.Query().Model(user).First(user, "email = ?", input.Email).Error != nil {
user.ID = uuid.Must(uuid.NewV4()).String()
if err := db.Query().Model(user).Create(user).Error; err != nil {
status := []int{}
if errors.Is(err, gorm.ErrRecordNotFound) {
status = append(status, http.StatusNotFound)
}
rest.HandleErr(res, req, err, status...)
return
}
userProfile := &gen.Profile{
ID: uuid.Must(uuid.NewV4()).String(),
Email: user.Email,
ExternalUserID: &user.ID,
Provider: &domain,
Name: user.DisplayName,
FirstName: user.FirstName,
LastName: user.LastName,
Users: []*gen.User{user},
}
if err := db.Query().Model(userProfile).Create(userProfile).Error; err != nil {
status := []int{}
if errors.Is(err, gorm.ErrRecordNotFound) {
status = append(status, http.StatusNotFound)
}
rest.HandleErr(res, req, err, status...)
return
}
resp.Msg = "successfully created"
resp.Status = http.StatusCreated
} else {
// already exists
resp.Msg = "already exists"
}
err = rest.SendJSON(res, req, resp, http.StatusCreated)
if err != nil {
rest.HandleErr(res, req, err)
return
}
}
}
// Login entry point of the slsfn /v{X}/auth/login
func Login(db *gen.DB) func(http.ResponseWriter, *http.Request) {
return func(res http.ResponseWriter, req *http.Request) {
var input models.LoginInput
err := rest.ReadJSON(res, req, &input)
if err != nil {
rest.HandleErr(res, req, err)
return
}
err = input.Validate()
if err != nil {
rest.HandleErr(res, req, err)
return
}
log.Debug().Msgf("Login user: %s", input.Email)
user := &gen.User{}
if err := db.Query().Model(user).Preload("Roles").Preload("Roles.Permissions").Preload("Permissions").
First(user, "email = ? AND active = ?", input.Email, true).Error; err != nil {
status := []int{}
if errors.Is(err, gorm.ErrRecordNotFound) {
status = append(status, http.StatusNotFound)
}
rest.HandleErr(res, req, errEmailPass, status...)
return
}
passw := ""
if user.Password != nil {
passw = *user.Password
}
if err := utils.PasswordCheck(passw, input.Password); err != nil {
rest.HandleErr(res, req, errEmailPass, http.StatusNotFound)
return
}
now := time.Now()
token, err := generateToken(user, jwt.StandardClaims{
Id: user.ID, Issuer: "app", Subject: user.ID, Audience: req.Host,
ExpiresAt: now.Add(12 * time.Hour).UTC().Unix(), IssuedAt: now.UTC().Unix(),
NotBefore: now.UTC().Unix(),
})
if err != nil {
rest.HandleErr(res, req, err)
return
}
response := map[string]interface{}{
"type": "Bearer",
"token": token,
}
if err := rest.SendJSON(res, req, response); err != nil {
rest.HandleErr(res, req, err)
}
}
}
// Begin entry point of the slsfn /v{X}/auth/[provider]
func Begin(res http.ResponseWriter, req *http.Request) {
// You have to add value context with provider name to get provider name in GetProviderName method
req = addProviderToContext(req, mux.Vars(req)[string(utils.ProjectContextKeys.ProviderCtxKey)])
// try to get the user without re-authenticating
if _, err := gothic.CompleteUserAuth(res, req); err != nil {
log.Error().Msg(err.Error())
gothic.BeginAuthHandler(res, req)
}
}
// BeginNamedProvider entry point of the slsfn /v{X}/auth/[provider] - Named helper
func BeginNamedProvider(provider string, res http.ResponseWriter, req *http.Request) {
// You have to add value context with provider name to get provider name in GetProviderName method
req = addProviderToContext(req, provider)
// try to get the user without re-authenticating
if _, err := gothic.CompleteUserAuth(res, req); err != nil {
log.Error().Msg(err.Error())
gothic.BeginAuthHandler(res, req)
}
}
// CallbackHandler to complete auth provider flow
func CallbackHandler(db *gen.DB) func(http.ResponseWriter, *http.Request) {
return func(res http.ResponseWriter, req *http.Request) {
// You have to add value context with provider name to get provider name in GetProviderName method
req = addProviderToContext(req, mux.Vars(req)[string(utils.ProjectContextKeys.ProviderCtxKey)])
callbackHandlerExec(res, req, db)
}
}
// CallbackHandlerNamedProvider to complete auth provider flow - Named provider helper
func CallbackHandlerNamedProvider(db *gen.DB, provider string) func(http.ResponseWriter, *http.Request) {
return func(res http.ResponseWriter, req *http.Request) {
req = addProviderToContext(req, provider)
callbackHandlerExec(res, req, db)
}
}
func callbackHandlerExec(res http.ResponseWriter, req *http.Request, db *gen.DB) {
user, err := gothic.CompleteUserAuth(res, req)
if err != nil {
rest.HandleErr(res, req, err, http.StatusUnprocessableEntity)
return
}
u, err := database.FindUserByJWT(db, user.UserID, user.Email, user.Provider)
if err != nil {
if u, err = database.UpsertUserProfile(db, &user); err != nil {
log.Err(fmt.Errorf("[Auth.CallbackHandler.Error]: %q", err)).Send()
rest.HandleErr(res, req, err, http.StatusUnprocessableEntity)
}
}
token, err := generateToken(u, jwt.StandardClaims{
Id: user.UserID, Issuer: user.Provider, Subject: u.ID, Audience: req.Host,
ExpiresAt: user.ExpiresAt.UTC().Unix(), IssuedAt: time.Now().UTC().Unix(),
NotBefore: time.Now().UTC().Unix(),
})
if err != nil {
rest.HandleErr(res, req, err, http.StatusUnprocessableEntity)
return
}
response := map[string]interface{}{
"type": "Bearer",
"token": token,
}
if err := rest.SendJSON(res, req, response); err != nil {
rest.HandleErr(res, req, err)
}
}
// Logout logs out of the auth provider
func Logout(res http.ResponseWriter, req *http.Request) {
req = addProviderToContext(req, mux.Vars(req)[string(utils.ProjectContextKeys.ProviderCtxKey)])
err := gothic.Logout(res, req)
if err != nil {
rest.HandleErr(res, req, err)
return
}
res.Header().Set("Location", "/")
res.WriteHeader(http.StatusTemporaryRedirect)
}
// Get the user roles from object
func getUserRoles(u *gen.User) (roles []string) {
for _, r := range u.Roles {
roles = append(roles, r.Name)
}
return
}
func generateToken(user *gen.User, stdClaims jwt.StandardClaims) (string, error) {
claims := gen.JWTClaims{
Email: user.Email,
StandardClaims: jwt.StandardClaims{
Id: stdClaims.Id,
Subject: stdClaims.Subject,
Issuer: stdClaims.Issuer,
Audience: stdClaims.Audience,
IssuedAt: time.Now().UTC().Unix(),
NotBefore: time.Now().UTC().Unix(),
ExpiresAt: stdClaims.ExpiresAt,
}, Roles: getUserRoles(user),
Permissions: getUserPermissions(user),
}
if user.DisplayName != nil {
claims.Name = *user.DisplayName
}
if user.NickName != nil {
claims.Nickname = *user.NickName
}
if user.AvatarURL != nil {
claims.Picture = *user.AvatarURL
}
jwtToken := jwt.NewWithClaims(jwt.GetSigningMethod(utils.MustGet("AUTH_JWT_SIGNING_ALGORITHM")), claims)
return jwtToken.SignedString([]byte(utils.MustGet("AUTH_JWT_SECRET")))
}
// Get user's permissions from object
func getUserPermissions(u *gen.User) map[string]string {
perms := make(map[string]string)
for _, p := range u.Permissions {
charIdx := strings.Index(p.Tag, ":")
if perms[p.Tag[:charIdx]] == "" {
perms[p.Tag[:charIdx]] += p.Tag[charIdx+1 : charIdx+2]
} else {
perms[p.Tag[:charIdx]] += "," + p.Tag[charIdx+1:charIdx+2]
}
}
return perms
}