/
authenticationflow_v1_input.go
141 lines (123 loc) · 3.83 KB
/
authenticationflow_v1_input.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
package api
import (
"encoding/json"
"errors"
"net/http"
"github.com/authgear/authgear-server/pkg/api"
authflow "github.com/authgear/authgear-server/pkg/lib/authenticationflow"
"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 ConfigureAuthenticationFlowV1InputRoute(route httproute.Route) httproute.Route {
return route.WithMethods("OPTIONS", "POST").WithPathPattern("/api/v1/authentication_flows/states/input")
}
var AuthenticationFlowV1NonRestfulInputRequestSchema = validation.NewSimpleSchema(`
{
"type": "object",
"required": [],
"properties": {
"state_token": { "type": "string" }
},
"oneOf": [
{
"properties": {
"input": {
"type": "object"
}
},
"required": ["input"]
},
{
"properties": {
"batch_input": {
"type": "array",
"items": {
"type": "object"
},
"minItems": 1
}
},
"required": ["batch_input"]
}
]
}
`)
type AuthenticationFlowV1NonRestfulInputRequest struct {
StateToken string `json:"state_token,omitempty"`
Input json.RawMessage `json:"input,omitempty"`
BatchInput []json.RawMessage `json:"batch_input,omitempty"`
}
type AuthenticationFlowV1InputHandler struct {
LoggerFactory *log.Factory
RedisHandle *appredis.Handle
JSON JSONResponseWriter
Cookies AuthenticationFlowV1CookieManager
Workflows AuthenticationFlowV1WorkflowService
}
func (h *AuthenticationFlowV1InputHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
var err error
var request AuthenticationFlowV1NonRestfulInputRequest
err = httputil.BindJSONBody(r, w, AuthenticationFlowV1NonRestfulInputRequestSchema.Validator(), &request)
if err != nil {
h.JSON.WriteResponse(w, &api.Response{Error: err})
return
}
if request.Input != nil {
h.input(w, r, request)
} else {
h.batchInput(w, r, request)
}
}
func (h *AuthenticationFlowV1InputHandler) input(w http.ResponseWriter, r *http.Request, request AuthenticationFlowV1NonRestfulInputRequest) {
stateToken := request.StateToken
output, err := h.input0(w, r, stateToken, request)
if err != nil {
apiResp, apiRespErr := prepareErrorResponse(h.Workflows, stateToken, err)
if apiRespErr != nil {
// failed to get the workflow when preparing the error response
h.JSON.WriteResponse(w, &api.Response{Error: apiRespErr})
return
}
h.JSON.WriteResponse(w, apiResp)
return
}
for _, c := range output.Cookies {
httputil.UpdateCookie(w, c)
}
result := output.ToFlowResponse()
h.JSON.WriteResponse(w, &api.Response{Result: result})
}
func (h *AuthenticationFlowV1InputHandler) input0(
w http.ResponseWriter,
r *http.Request,
stateToken string,
request AuthenticationFlowV1NonRestfulInputRequest,
) (*authflow.ServiceOutput, error) {
output, err := h.Workflows.FeedInput(stateToken, request.Input)
if err != nil && !errors.Is(err, authflow.ErrEOF) {
return nil, err
}
return output, nil
}
func (h *AuthenticationFlowV1InputHandler) batchInput(w http.ResponseWriter, r *http.Request, request AuthenticationFlowV1NonRestfulInputRequest) {
stateToken := request.StateToken
output, err := batchInput0(h.Workflows, w, r, stateToken, request.BatchInput)
if err != nil {
apiResp, apiRespErr := prepareErrorResponse(h.Workflows, stateToken, err)
if apiRespErr != nil {
// failed to get the workflow when preparing the error response
h.JSON.WriteResponse(w, &api.Response{Error: apiRespErr})
return
}
h.JSON.WriteResponse(w, apiResp)
return
}
for _, c := range output.Cookies {
httputil.UpdateCookie(w, c)
}
result := output.ToFlowResponse()
h.JSON.WriteResponse(w, &api.Response{Result: result})
}