func (a *Handler) PostSaved(ctx context.Context, w http.ResponseWriter, r *http.Request) error { c, err := rellenv.FromContext(ctx) if err != nil { return err } if !rellenv.IsEmployee(ctx) { return ctxerr.Wrap(ctx, errSaveDisabled) } if !a.Xsrf.Validate(r.FormValue(paramName), w, r, savedPath) { return ctxerr.Wrap(ctx, errTokenMismatch) } content := strings.TrimSpace(r.FormValue("code")) content = strings.Replace(content, "\x13", "", -1) // remove CR id := examples.ContentID(content) db := a.ExampleStore.DB example, ok := db.Reverse[id] if ok { http.Redirect(w, r, c.ViewURL(example.URL), 302) return nil } err = a.ExampleStore.Save(id, content) if err != nil { return err } http.Redirect(w, r, c.ViewURL(savedPath+id), 302) return nil }
func (a *Handler) Response(ctx context.Context, w http.ResponseWriter, r *http.Request) error { c, err := rellenv.FromContext(ctx) if err != nil { return err } if r.FormValue("state") != a.state(w, r) { return ctxerr.Wrap(ctx, errInvalidState) } values := url.Values{} values.Set("client_id", strconv.FormatUint(a.App.ID(), 10)) values.Set("client_secret", a.App.Secret()) values.Set("redirect_uri", redirectURI(c)) values.Set("code", r.FormValue("code")) atURL := &fburl.URL{ Scheme: "https", SubDomain: fburl.DGraph, Env: rellenv.FbEnv(ctx), Path: "/oauth/access_token", Values: values, } req, err := http.NewRequest("GET", atURL.String(), nil) if err != nil { return ctxerr.Wrap(ctx, errOAuthFail) } res, err := a.HttpTransport.RoundTrip(req) if err != nil { return ctxerr.Wrap(ctx, err) } defer res.Body.Close() bd, err := ioutil.ReadAll(res.Body) if err != nil { return ctxerr.Wrap(ctx, err) } h.WriteResponse(w, r, &h.Frag{ &h.Script{Inner: h.Unsafe("window.location.hash = ''")}, h.String(string(bd)), }) return nil }
// Parse the Env and an Example. func (h *Handler) parse(ctx context.Context, r *http.Request) (*rellenv.Env, *examples.Example, error) { context, err := rellenv.FromContext(ctx) if err != nil { return nil, nil, err } example, err := h.ExampleStore.Load(r.URL.Path) if err != nil { return nil, nil, ctxerr.Wrap(ctx, err) } return context, example, nil }
func (a *Handler) Handler(ctx context.Context, w http.ResponseWriter, r *http.Request) error { if !rellenv.IsEmployee(ctx) { return ctxerr.Wrap(ctx, errEmployeesOnly) } switch r.URL.Path { case Path: return a.Start(ctx, w, r) case Path + resp: return a.Response(ctx, w, r) } w.Header().Set("Content-Type", "text/html; charset=utf-8") w.WriteHeader(http.StatusNotFound) h.WriteResponse(w, r, &h.Script{ Inner: h.Unsafe("top.location='/'"), }) return nil }
// FromContext retrieves the Env from the Context. If one isn't found, an error // is returned. func FromContext(ctx context.Context) (*Env, error) { if e, ok := ctx.Value(contextEnvKey).(*Env); ok { return e, nil } return nil, ctxerr.Wrap(ctx, errEnvNotFound) }