-
Notifications
You must be signed in to change notification settings - Fork 0
/
svc_smsguard.go
50 lines (40 loc) · 1016 Bytes
/
svc_smsguard.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
package http
import (
"net/http"
"time"
"github.com/go-chi/chi"
"github.com/kudarap/ratatxt/ratatxt"
"github.com/kudarap/ratatxt/smsguard"
)
type smsGuardResp struct {
Code string `json:"code"`
Expr time.Time `json:"expiration"`
}
func handleSMSGuardCreate(core *ratatxt.Core) http.HandlerFunc {
fn := func(w http.ResponseWriter, r *http.Request) error {
var p ratatxt.SMSGuardCreateParam
if err := parseForm(r, &p); err != nil {
return err
}
p.UserID = userIDFrom(r.Context())
code, expr, err := core.SendSMSGuard(p)
if err != nil {
return err
}
respondOK(w, smsGuardResp{code, expr})
return nil
}
return handleWithError(fn)
}
func handleSMSGuardCheck() http.HandlerFunc {
fn := func(w http.ResponseWriter, r *http.Request) error {
code := chi.URLParam(r, "code")
uid := userIDFrom(r.Context())
isValid := smsguard.IsValid(code, uid.String())
respondOK(w, struct {
IsValid bool `json:"is_valid"`
}{isValid})
return nil
}
return handleWithError(fn)
}