/
authenticationflow_v1_get.go
61 lines (51 loc) · 1.77 KB
/
authenticationflow_v1_get.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
package api
import (
"net/http"
"github.com/authgear/authgear-server/pkg/api"
"github.com/authgear/authgear-server/pkg/lib/infra/redis/appredis"
"github.com/authgear/authgear-server/pkg/util/httproute"
"github.com/authgear/authgear-server/pkg/util/httputil"
"github.com/authgear/authgear-server/pkg/util/log"
"github.com/authgear/authgear-server/pkg/util/validation"
)
func ConfigureAuthenticationFlowV1GetRoute(route httproute.Route) httproute.Route {
return route.WithMethods("OPTIONS", "POST").WithPathPattern("/api/v1/authentication_flows/states")
}
type AuthenticationFlowV1NonRestfulGetRequest struct {
StateToken string `json:"state_token,omitempty"`
}
var AuthenticationFlowV1NonRestfulGetRequestSchema = validation.NewSimpleSchema(`
{
"type": "object",
"properties": {
"state_token": { "type": "string" }
},
"required": ["state_token"]
}
`)
type AuthenticationFlowV1GetHandler struct {
LoggerFactory *log.Factory
RedisHandle *appredis.Handle
JSON JSONResponseWriter
Workflows AuthenticationFlowV1WorkflowService
}
func (h *AuthenticationFlowV1GetHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
var err error
var request AuthenticationFlowV1NonRestfulGetRequest
err = httputil.BindJSONBody(r, w, AuthenticationFlowV1NonRestfulGetRequestSchema.Validator(), &request)
if err != nil {
h.JSON.WriteResponse(w, &api.Response{Error: err})
return
}
stateToken := request.StateToken
h.get(w, r, stateToken)
}
func (h *AuthenticationFlowV1GetHandler) get(w http.ResponseWriter, r *http.Request, stateToken string) {
output, err := h.Workflows.Get(stateToken)
if err != nil {
h.JSON.WriteResponse(w, &api.Response{Error: err})
return
}
result := output.ToFlowResponse()
h.JSON.WriteResponse(w, &api.Response{Result: result})
}