-
Notifications
You must be signed in to change notification settings - Fork 2
/
creds.go
57 lines (47 loc) · 1.59 KB
/
creds.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
package google
import (
"fmt"
"net/http"
"net/url"
"go.uber.org/zap"
"go.autokitteh.dev/autokitteh/integrations/google/internal/vars"
"go.autokitteh.dev/autokitteh/sdk/sdkintegrations"
"go.autokitteh.dev/autokitteh/sdk/sdktypes"
)
const (
// credsPath is the URL path for our handler to save a new autokitteh
// credentials-based connection, after the user submits it via a web form.
credsPath = "/google/save"
HeaderContentType = "Content-Type"
ContentTypeForm = "application/x-www-form-urlencoded"
)
// HandleCreds saves a new autokitteh connection with a user-submitted token.
func (h handler) HandleCreds(w http.ResponseWriter, r *http.Request) {
l := h.logger.With(zap.String("urlPath", r.URL.Path))
// Check "Content-Type" header.
ct := r.Header.Get(HeaderContentType)
if ct != ContentTypeForm {
l.Warn("Unexpected header value",
zap.String("header", HeaderContentType),
zap.String("got", ct),
zap.String("want", ContentTypeForm),
)
e := fmt.Sprintf("Unexpected Content-Type header: %q", ct)
u := fmt.Sprintf("%serror.html?error=%s", uiPath, url.QueryEscape(e))
http.Redirect(w, r, u, http.StatusFound)
return
}
// Read and parse POST request body.
err := r.ParseForm()
if err != nil {
l.Warn("Failed to parse inbound HTTP request",
zap.Error(err),
)
e := "Form parsing error: " + err.Error()
u := fmt.Sprintf("%serror.html?error=%s", uiPath, url.QueryEscape(e))
http.Redirect(w, r, u, http.StatusFound)
return
}
initData := sdktypes.EncodeVars(&vars.Vars{JSON: r.Form.Get("json")})
sdkintegrations.FinalizeConnectionInit(w, r, integrationID, initData)
}