/
password.go
206 lines (174 loc) · 6.52 KB
/
password.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
package password
import (
"html/template"
"net/http"
"strings"
"github.com/glebtv/auth"
"github.com/glebtv/auth/auth_identity"
"github.com/glebtv/auth/claims"
"github.com/glebtv/auth/providers/password/encryptor"
"github.com/glebtv/auth/providers/password/encryptor/bcrypt_encryptor"
"github.com/qor/session"
)
// Config password config
type Config struct {
Confirmable bool
ConfirmMailer func(email string, context *auth.Context, claims *claims.Claims, currentUser interface{}) error
ConfirmHandler func(*auth.Context) error
ConfirmSkip func(context *auth.Context) bool
ResetPasswordMailer func(email string, context *auth.Context, claims *claims.Claims, currentUser interface{}) error
ResetPasswordHandler func(*auth.Context) error
RecoverPasswordHandler func(*auth.Context) error
Encryptor encryptor.Interface
AuthorizeHandler func(*auth.Context) (*claims.Claims, error)
RegisterHandler func(*auth.Context) (*claims.Claims, error)
}
// New initialize password provider
func New(config *Config) *Provider {
if config == nil {
config = &Config{}
}
if config.Encryptor == nil {
config.Encryptor = bcrypt_encryptor.New(&bcrypt_encryptor.Config{})
}
provider := &Provider{Config: config}
if config.ConfirmMailer == nil {
config.ConfirmMailer = DefaultConfirmationMailer
}
if config.ConfirmHandler == nil {
config.ConfirmHandler = DefaultConfirmHandler
}
if config.ResetPasswordMailer == nil {
config.ResetPasswordMailer = DefaultResetPasswordMailer
}
if config.ResetPasswordHandler == nil {
config.ResetPasswordHandler = DefaultResetPasswordHandler
}
if config.RecoverPasswordHandler == nil {
config.RecoverPasswordHandler = DefaultRecoverPasswordHandler
}
if config.AuthorizeHandler == nil {
config.AuthorizeHandler = DefaultAuthorizeHandler
}
if config.RegisterHandler == nil {
config.RegisterHandler = DefaultRegisterHandler
}
if config.ConfirmSkip == nil {
config.ConfirmSkip = func(context *auth.Context) bool { return false }
}
return provider
}
// Provider provide login with password method
type Provider struct {
*Config
}
// GetName return provider name
func (Provider) GetName() string {
return "password"
}
// ConfigAuth config auth
func (provider Provider) ConfigAuth(auth *auth.Auth) {
auth.Render.RegisterViewPath("github.com/glebtv/auth/providers/password/views")
if auth.Mailer != nil {
auth.Mailer.RegisterViewPath("github.com/glebtv/auth/providers/password/views/mailer")
}
}
// Login implemented login with password provider
func (provider Provider) Login(context *auth.Context) {
context.Auth.LoginHandler(context, provider.AuthorizeHandler)
}
// Register implemented register with password provider
func (provider Provider) Register(context *auth.Context) {
context.Auth.RegisterHandler(context, provider.RegisterHandler)
}
// Logout implemented logout with password provider
func (provider Provider) Logout(context *auth.Context) {
context.Auth.LogoutHandler(context)
}
// Callback implement Callback with password provider
func (provider Provider) Callback(context *auth.Context) {
}
// ServeHTTP implement ServeHTTP with password provider
func (provider Provider) ServeHTTP(context *auth.Context) {
var (
req = context.Request
reqPath = strings.TrimPrefix(req.URL.Path, context.Auth.URLPrefix)
paths = strings.Split(reqPath, "/")
)
if len(paths) >= 2 {
switch paths[1] {
case "confirmation":
var err error
if len(paths) >= 3 {
switch paths[2] {
case "new":
// render new confirmation page
context.Auth.Config.Render.Execute("auth/confirmation/new", context, context.Request, context.Writer)
case "send":
var (
currentUser interface{}
authInfo auth_identity.Basic
tx = context.Auth.GetDB(req)
)
authInfo.Provider = provider.GetName()
authInfo.UID = strings.TrimSpace(req.Form.Get("email"))
if tx.Model(context.Auth.AuthIdentityModel).Where(authInfo).Scan(&authInfo).RecordNotFound() {
err = auth.ErrInvalidAccount
}
if err == nil {
if currentUser, err = context.Auth.UserStorer.Get(authInfo.ToClaims(), context); err == nil {
err = provider.Config.ConfirmMailer(authInfo.UID, context, authInfo.ToClaims(), currentUser)
}
}
if err == nil {
context.SessionStorer.Flash(context.Writer, req, session.Message{Message: ConfirmFlashMessage, Type: "success"})
context.Auth.Redirector.Redirect(context.Writer, context.Request, "send_confirmation")
}
}
}
if err != nil {
context.SessionStorer.Flash(context.Writer, req, session.Message{Message: template.HTML(err.Error()), Type: "error"})
}
// render new confirmation page
context.Auth.Config.Render.Execute("auth/confirmation/new", context, context.Request, context.Writer)
case "confirm":
// confirm user
err := provider.ConfirmHandler(context)
if err != nil {
context.SessionStorer.Flash(context.Writer, req, session.Message{Message: template.HTML(err.Error()), Type: "error"})
context.Auth.Redirector.Redirect(context.Writer, context.Request, "confirm_failed")
return
}
case "new":
// render change password page
context.Auth.Config.Render.Execute("auth/password/new", context, context.Request, context.Writer)
case "recover":
// send recover password mail
err := provider.RecoverPasswordHandler(context)
if err != nil {
context.SessionStorer.Flash(context.Writer, req, session.Message{Message: template.HTML(err.Error()), Type: "error"})
http.Redirect(context.Writer, context.Request, context.Auth.AuthURL("password/new"), http.StatusSeeOther)
return
}
case "edit":
// render edit password page
if token := context.Request.URL.Query().Get("token"); token != "" {
context.Auth.Config.Render.Funcs(template.FuncMap{
"reset_password_token": func() string { return token },
}).Execute("auth/password/edit", context, context.Request, context.Writer)
return
}
context.SessionStorer.Flash(context.Writer, req, session.Message{Message: template.HTML(ErrInvalidResetPasswordToken.Error()), Type: "error"})
http.Redirect(context.Writer, context.Request, context.Auth.AuthURL("password/new"), http.StatusSeeOther)
case "update":
// update password
err := provider.ResetPasswordHandler(context)
if err != nil {
context.SessionStorer.Flash(context.Writer, req, session.Message{Message: template.HTML(err.Error()), Type: "error"})
http.Redirect(context.Writer, context.Request, context.Auth.AuthURL("password/new"), http.StatusSeeOther)
return
}
}
}
return
}