This repository was archived by the owner on Jul 13, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathset_password.go
148 lines (128 loc) · 3.96 KB
/
set_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
package forms
import (
"context"
"fmt"
"net/http"
"time"
auth "github.com/moapis/authenticator"
"github.com/moapis/ehtml"
clog "github.com/usrpro/clog15"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
)
// DefaultSetPWTmpl is a placeholder template for `Login`
const DefaultSetPWTmpl = `{{ define "setpw" -}}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Set password</title>
</head>
<body>
<h1>Set a new password</h1>
<form method="post" action="{{ .SubmitURL }}">
<input type="password" placeholder="Password" name="password" required>
<button type="submit">Submit</button>
</form>
{{- if .Flash }}
<p>{{ .Flash.Lvl }}: {{ .Flash.Msg }}</p>
{{- end }}
</body>
</html>
{{- end -}}
`
func (f *Forms) setPWGet(w http.ResponseWriter, r *http.Request) {
ctx := clog.AddArgs(r.Context(), "method", "setPWGet")
tkn := r.URL.Query().Get("jwt")
if tkn == "" {
if err := f.EP.Render(w, &ehtml.Data{Req: r, Code: http.StatusBadRequest, Msg: "Missing token in URL"}); err != nil {
clog.Error(ctx, "During handling error", "err", err)
}
return
}
f.renderForm(w, r, SetPWTmpl, SetPWTitle, nil)
}
func (f *Forms) setPWRedirect(w http.ResponseWriter, r *http.Request) {
ctx := clog.AddArgs(r.Context(), "method", "setPWRedirect")
rURL, err := f.getRedirect(r)
if err != nil {
if err := f.EP.Render(w, &ehtml.Data{Req: r, Code: http.StatusOK,
Msg: "Password set succesfully. You can now close this window",
}); err != nil {
clog.Error(ctx, "During handling error", "err", err)
}
return
}
http.Redirect(w, r,
fmt.Sprintf("%s?%s=%s", f.Paths.login(), f.Paths.redirectKey(), rURL),
http.StatusSeeOther)
}
func (f *Forms) setPWPost(w http.ResponseWriter, r *http.Request) {
ctx, cancel := context.WithTimeout(r.Context(), 10*time.Second)
defer cancel()
ctx = clog.AddArgs(ctx, "method", "setPWPost")
if err := r.ParseForm(); err != nil {
clog.Warn(ctx, "Parseform", "err", err)
fl := &Flash{ErrFlashLvl, "Malformed form data"}
f.renderForm(w, r, SetPWTmpl, SetPWTitle, fl, http.StatusBadRequest)
return
}
tkn := r.URL.Query().Get("jwt")
if tkn == "" {
if err := f.EP.Render(w, &ehtml.Data{Req: r, Code: http.StatusBadRequest, Msg: "Missing token in URL"}); err != nil {
clog.Error(ctx, "During handling error", "err", err)
}
return
}
npw := r.PostForm.Get("password")
if npw == "" {
clog.Warn(ctx, "Missing password in form")
fl := &Flash{ErrFlashLvl, "Missing form data: password"}
f.renderForm(w, r, SetPWTmpl, SetPWTitle, fl, http.StatusBadRequest)
return
}
reply, err := f.Client.ChangeUserPw(ctx,
&auth.NewUserPassword{
Credential: &auth.NewUserPassword_ResetToken{ResetToken: tkn},
NewPassword: npw,
})
if err == nil && reply.Success {
f.setPWRedirect(w, r)
return
}
s, ok := status.FromError(err)
if !ok || s.Code() != codes.Unauthenticated {
clog.Error(ctx, "ChangeUserPw gRPC call", "err", err)
f.renderForm(w, r, SetPWTmpl, SetPWTitle,
&Flash{ErrFlashLvl, "Internal server error"},
http.StatusInternalServerError,
)
} else {
clog.Info(ctx, "ChangeUserPw gRPC call", "err", err)
if err := f.EP.Render(w, &ehtml.Data{Req: r, Code: http.StatusUnauthorized, Msg: "Token verification failed, please request a new one."}); err != nil {
clog.Error(ctx, "During handling error", "err", err)
}
}
}
// SetPWHandler returns the handler taking care
// of password setting, using a reset token.
// GET serves the "setpw" form template.
// POST checks the JWT and sets the new password over gRPC.
func (f *Forms) SetPWHandler() http.Handler {
return &setPWHandler{f}
}
type setPWHandler struct {
*Forms
}
func (h *setPWHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
r = r.WithContext(clog.AddArgs(r.Context(), "pkg", "authenticator.forms", "handler", "SetPW"))
switch r.Method {
case http.MethodGet:
h.setPWGet(w, r)
case http.MethodPost:
h.setPWPost(w, r)
default:
w.Header().Add("Allow", AllowedMethods)
w.WriteHeader(http.StatusMethodNotAllowed)
}
}