-
Notifications
You must be signed in to change notification settings - Fork 1
/
http.go
122 lines (104 loc) · 3.26 KB
/
http.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
package auth
import (
"encoding/json"
"net/http"
oauth "golang.org/x/oauth2"
)
type emailPasswordParams struct {
Email string `json:"email"`
Password string `json:"password"`
}
// HTTP server
// Authorize is a middleware to authorize user in a defined provider.
// Send provider name as params and the return is a http handle
//
//
// GET /auth/google auth.Authorize("google")
// GET /auth/facebook auth.Authorize("facebook")
// POST /sign_in auth.Authorize("email")
//
func (a *Auth) Authorize(providerName string) http.HandlerFunc {
provider := a.Providers[providerName]
return func(w http.ResponseWriter, r *http.Request) {
switch {
case provider == nil:
w.WriteHeader(http.StatusNotFound)
w.Write([]byte("Provider '" + providerName + "' not found"))
return
case providerName == EmailPasswordProvider.Name:
if r.Method == "GET" {
w.WriteHeader(http.StatusNotFound)
} else {
a.SignIn(w, r)
}
return
}
a.oauthAuthorize(provider, w, r)
}
}
func (a *Auth) oauthAuthorize(provider *builderConfig, w http.ResponseWriter, r *http.Request) {
url := provider.Auth.AuthCodeURL("")
http.Redirect(w, r, url, http.StatusFound)
}
// HTTP Handler to sign in users is expected email and password in request body as JSON
//
// {"email": "[email protected]", "password": "abc123"}
func (a *Auth) SignIn(w http.ResponseWriter, r *http.Request) (string, bool) {
decoder := json.NewDecoder(r.Body)
var params emailPasswordParams
decoder.Decode(¶ms) // TODO: test error here
if params.Email == "" || params.Password == "" {
w.WriteHeader(http.StatusBadRequest)
return "", false
}
foundPassword, wasFound := a.Helper.PasswordByEmail(params.Email)
if !wasFound {
w.WriteHeader(http.StatusForbidden)
return "", false
}
err := checkHash(foundPassword, params.Password)
if err != nil {
w.WriteHeader(http.StatusUnauthorized)
return "", false
}
user, id, ok := a.Helper.FindUserDataByEmail(params.Email)
if !ok {
w.WriteHeader(http.StatusInternalServerError)
return "", false
}
w.Header().Set("Content-Type", "application/json")
w.Write([]byte(user))
return id, true
}
// The oauth endpoint callback, configured on provider, Send provider name as params
// OAuthCallback will receive code params from provider and get user information
//
// ```
// GET /auth/google/callback http.HandlerFunc -> auth.OAuthCallback("google", w, r)
// GET /auth/facebook/callback http.HandlerFunc -> auth.OAuthCallback("facebook", w, r)
// ```
func (a *Auth) OAuthCallback(providerName string, w http.ResponseWriter, r *http.Request) (string, error) {
return a.oAuthUser(providerName, w, r)
}
func (a *Auth) oAuthUser(providerName string, w http.ResponseWriter, r *http.Request) (userID string, err error) {
code := r.FormValue("code")
provider := a.Providers[providerName]
token, err := provider.Auth.Exchange(oauth.NoContext, code)
if err != nil {
return
}
client := provider.Auth.Client(oauth.NoContext, token)
response, err := client.Get(provider.UserInfoURL)
if err != nil {
return
}
defer response.Body.Close()
var user User
decoder := json.NewDecoder(response.Body)
err = decoder.Decode(&user)
// user.Token = NewUserToken()
if err != nil {
return
}
return a.Helper.FindUserFromOAuth(providerName, &user, response)
}