-
Notifications
You must be signed in to change notification settings - Fork 0
/
oauth.go
333 lines (283 loc) · 7.99 KB
/
oauth.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
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
// Package oauth provides an HTTP handler to handle OAuth2
// redirect and callback requests for the bebop web app.
package oauth
import (
"context"
"fmt"
"log"
"net/http"
"runtime"
"strings"
"time"
"encoding/json"
"github.com/go-chi/chi"
"golang.org/x/oauth2"
"bebopExtended/store"
"github.com/disintegration/bebop/jwt"
)
const clientTimeout = 10 * time.Second
// Config is a configuration of an OAuth handler.
type Config struct {
Logger *log.Logger
UserStore store.UserStore
JWTService jwt.Service
MountURL string
CookiePath string
}
// Handler handles oauth2 authentication requests.
type Handler struct {
*Config
providers map[string]*provider
router chi.Router
}
//Email request signin and signup structure
type EmaiRequest struct {
Email *string `json:"email"`
Password *string `json:"password"`
}
// New creates a new handler based on the given config.
func New(config *Config) *Handler {
h := &Handler{Config: config}
h.providers = make(map[string]*provider)
h.router = chi.NewRouter()
h.router.Get("/begin/{provider}", h.handleBegin)
h.router.Get("/end/{provider}", h.handleEnd)
h.router.Post("/emailSignUp", h.handleEmailSignUp)
h.router.Post("/emailSignIn", h.handleEmailSignIn)
return h
}
//Server requests
func (h *Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
h.router.ServeHTTP(w, r)
}
// AddProvider adds a new provider to oauth handler.
func (h *Handler) AddProvider(name, id, secret string) error {
h.Logger.Printf("in AddProvider")
h.Logger.Printf(name)
pc, ok := providerConfigs[name]
if !ok {
return fmt.Errorf("oauth: unknown provider: %q", name)
}
if id == "" {
return fmt.Errorf("oauth: empty client id of provider %q", name)
} else if secret == "" {
return fmt.Errorf("oauth: empty client secret of provider %q", name)
} else if h.providers == nil {
h.providers = make(map[string]*provider)
}
h.providers[name] = &provider{
config: &oauth2.Config{
ClientID: id,
ClientSecret: secret,
RedirectURL: h.MountURL + "/end/" + name,
Endpoint: pc.endpoint,
Scopes: pc.scopes,
},
getUser: pc.getUser,
}
return nil
}
func (h *Handler) handleEmailSignIn(w http.ResponseWriter, r *http.Request) {
req := EmaiRequest{}
if !ValidEmailReqest(h, w, r, &req) {
return
}
SetStateCookie(h, w, 3600)
h.Logger.Printf("email")
h.Logger.Printf(*req.Email)
user, err := h.UserStore.GetByEmailPassword(*req.Email, *req.Password)
switch err {
case nil:
if user.Blocked {
h.renderOAuthResult(w, "error:UserBlocked")
return
}
authToken, tokenErr := h.JWTService.Create(user.ID)
if tokenErr != nil {
h.handleError(w, "failed to create auth token: %s", tokenErr)
return
}
SetResultCookie(h, w, "success:"+authToken, 3600)
h.Logger.Printf("redir url: %s", w)
fmt.Fprint(w, `success`)
return
case store.ErrNotFound:
fmt.Fprint(w, `Incorrect email or password`)
return
default:
h.handleError(w, "failed to get user by auth: %s", err)
return
}
}
func (h *Handler) handleEmailSignUp(w http.ResponseWriter, r *http.Request) {
req := EmaiRequest{}
if !ValidEmailReqest(h, w, r, &req) {
return
}
SetStateCookie(h, w, 3600)
h.Logger.Printf("email")
h.Logger.Printf(*req.Email)
user, err := h.UserStore.GetByEmailPassword(*req.Email,*req.Password)
switch err {
case nil:
if user.Blocked {
h.renderOAuthResult(w, "error:UserBlocked")
return
}
h.handleError(w, "error:UserAlreayRegistred")
return
case store.ErrNotFound:
userID, err := h.UserStore.New("email", *req.Email, *req.Password)
if err != nil {
h.handleError(w, "failed to create user: %s", err)
return
}
authToken, err := h.JWTService.Create(userID)
if err != nil {
h.handleError(w, "failed to create auth token: %s", err)
return
}
SetResultCookie(h, w, "success:"+authToken, 3600)
h.Logger.Printf("redir url: %s", w)
fmt.Fprint(w, `success`)
return
default:
h.handleError(w, "failed to get user by auth: %s", err)
return
}
}
func (h *Handler) handleBegin(w http.ResponseWriter, r *http.Request) {
h.Logger.Printf("in handleBegin")
providerName := chi.URLParam(r, "provider")
provider, ok := h.providers[providerName]
if !ok {
http.NotFound(w, r)
return
}
state := SetStateCookie(h, w, 3600)
redirectURL := "localhost:5000/#"
if providerName != "email" {
redirectURL = provider.config.AuthCodeURL(state)
}
h.Logger.Printf("path: %s", h.CookiePath)
http.Redirect(w, r, redirectURL, http.StatusFound)
}
func (h *Handler) handleEnd(w http.ResponseWriter, r *http.Request) {
h.Logger.Printf("in handleEnd")
providerName := chi.URLParam(r, "provider")
h.Logger.Printf("providerName: %s", providerName)
provider, ok := h.providers[providerName]
if !ok {
h.Logger.Printf("in not found")
http.NotFound(w, r)
return
}
cookie, err := r.Cookie(STATE_COOKIE)
if err != nil {
h.handleError(w, "failed to get oauth state cookie: %s", err)
return
}
state := cookie.Value
if state == "" {
h.handleError(w, "empty oauth state cookie")
return
}
queryState := r.URL.Query().Get("state")
h.Logger.Printf("queryState: %s", queryState)
h.Logger.Printf("state: %s", state)
if queryState != state {
h.handleError(w, "bad state value")
return
}
queryCode := r.URL.Query().Get("code")
if queryCode == "" {
h.handleError(w, "empty code value")
return
}
ctx, cancel := context.WithTimeout(r.Context(), clientTimeout)
defer cancel()
token, err := provider.config.Exchange(ctx, queryCode)
if err != nil {
h.handleError(w, "exchange failed: %s", err)
return
} else if !token.Valid() {
h.handleError(w, "invalid token")
return
}
u, err := provider.getUser(provider.config.Client(ctx, token))
if err != nil {
h.handleError(w, "get provider user: %s", err)
return
} else if u.id == "" {
h.handleError(w, "provider user id is empty")
return
}
user, err := h.UserStore.GetByAuth(providerName, u.id)
switch err {
case nil:
if user.Blocked {
h.renderOAuthResult(w, "error:UserBlocked")
return
}
authToken, err := h.JWTService.Create(user.ID)
if err != nil {
h.handleError(w, "failed to create auth token: %s", err)
return
}
h.renderOAuthResult(w, "success:"+authToken)
return
case store.ErrNotFound:
userID, err := h.UserStore.New(providerName, u.id, "")
if err != nil {
h.handleError(w, "failed to create user: %s", err)
return
}
authToken, err := h.JWTService.Create(userID)
if err != nil {
h.handleError(w, "failed to create auth token: %s", err)
return
}
h.renderOAuthResult(w, "success:"+authToken)
return
default:
h.handleError(w, "failed to get user by auth: %s", err)
return
}
}
func (h *Handler) renderOAuthResult(w http.ResponseWriter, message string) {
SetResultCookie(h, w, message, 600)
h.Logger.Printf("redir url: %s", w)
fmt.Fprint(w, `<!doctype html><title>OAuth</title><script>try {opener.bebopOAuthEnd()} finally {window.close()}</script>`)
}
func (h *Handler) renderError(w http.ResponseWriter, status int, code, message string) {
response := struct {
Error struct {
Code string `json:"code"`
Message string `json:"message"`
} `json:"error"`
}{}
response.Error.Code = code
response.Error.Message = message
h.render(w, status, response)
}
func (h *Handler) render(w http.ResponseWriter, status int, data interface{}) {
jsonData, err := json.Marshal(data)
if err != nil {
h.logError("marshal json: %s", err)
http.Error(w, "Internal Server Error", http.StatusInternalServerError)
return
}
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(status)
w.Write(jsonData)
}
func (h *Handler) logError(format string, a ...interface{}) {
pc, _, _, _ := runtime.Caller(1)
callerNameSplit := strings.Split(runtime.FuncForPC(pc).Name(), ".")
funcName := callerNameSplit[len(callerNameSplit)-1]
h.Logger.Printf("ERROR: %s: %s", funcName, fmt.Sprintf(format, a...))
}
func (h *Handler) handleError(w http.ResponseWriter, format string, a ...interface{}) {
h.logError(format, a)
h.renderOAuthResult(w, "error:Other")
}