-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathflowAuthCodeVerify.go
58 lines (48 loc) · 1.39 KB
/
flowAuthCodeVerify.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
package main
import (
"github.com/patrickmn/go-cache"
"log"
"net/http"
)
func init() {
Router.HandleFunc("/verify", handlerVerifyPost).Methods("POST")
Router.HandleFunc("/re-verify", handlerReVerifyPost).Methods("POST")
}
func handlerVerifyPost(w http.ResponseWriter, r *http.Request) {
ClearSession(w)
code := r.FormValue("code")
savedId, ok := VerifyStorage.Get(code)
owner := &Owner{}
if ok {
owner.Id = savedId.(string)
if err := owner.verify(); ok && err == nil {
VerifyStorage.Delete(code)
log.Printf("user '%s' successfully verified", owner.Id)
w.WriteHeader(http.StatusOK)
} else {
log.Print(err)
Rnd.JSON(w, http.StatusForbidden, ErrorResponse{err.Error()})
}
} else {
log.Printf("code '%s' is invalid", code)
Rnd.JSON(w, http.StatusForbidden, ErrorResponse{"invalid code"})
}
return
}
func handlerReVerifyPost(w http.ResponseWriter, r *http.Request) {
code := generateRandomNumbers(flowConfig.VerificationCodeLength)
owner := Owner{
Username: r.FormValue("username"),
VerificationCode: code,
VerificationRoute: authCodeConfig.OauthCodeUi + "/verify/" + code,
}
if id, err := owner.reVerify(); err == nil {
VerifyStorage.Set(code, id, cache.DefaultExpiration)
log.Printf("Re-verification code for user '%s' is: %s", id, code)
w.WriteHeader(http.StatusOK)
} else {
log.Printf(err.Error())
w.WriteHeader(http.StatusOK)
}
return
}