Exemple #1
0
func handleRoot(w http.ResponseWriter, r *http.Request) {
	c := oldappengine.NewContext(r)
	u, err := user.CurrentOAuth(c, "")
	if err != nil {
		http.Error(w, "OAuth Authorization header required", http.StatusUnauthorized)
		return
	}
	if u == nil {
		fmt.Fprintf(w, `You are not logged in yet, please <a href='/authorize'>Login</a>`, u)
		return
	}

	fmt.Fprintf(w, `Welcome, %s!`, u)
}
Exemple #2
0
// ServeHTTP implements http.Handler.
func (fn call) ServeHTTP(w http.ResponseWriter, r *http.Request) {
	c := appengine.NewContext(r)
	user, err := user.CurrentOAuth(c, OAUTH_SCOPE)
	if err != nil {
		http.Error(w, "OAuth Authorization header not available", http.StatusUnauthorized)
		return
	}
	c.Debugf("user=%s admin=%s", user.Email, user.Admin)

	var body []byte
	if r.ContentLength >= 0 {
		defer r.Body.Close()
		body, err = ioutil.ReadAll(r.Body)
		if err != nil {
			// TODO: better code?
			http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
			return
		}
	}

	res := fn(&env{
		Context: c,
		User:    user,
		Req:     r,
		Body:    body,
	}, w.Header())

	switch t := res.(type) {
	case error:
		c.Warningf("%s", t)
		http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
	case int:
		http.Error(w, http.StatusText(t), t)
	case string:
		fmt.Fprintf(w, t)
	default:
		if t != nil {
			raw, err := json.Marshal(t)
			if err != nil {
				http.Error(w, "could not marshal output", http.StatusInternalServerError)
			} else {
				w.Header().Set("Content-Type", "application/json")
				fmt.Fprintf(w, "%s", string(raw))
			}
		}
	}
}
Exemple #3
0
func (h ApiHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
	c := appengine.NewContext(r)
	u := user.Current(c)
	if u == nil {
		u, _ = user.CurrentOAuth(c, oauthScope)
	}
	if u == nil {
		writeErr(w, ErrNoLogin, c)
		return
	}
	objs, err := h.ApiFunc(r, c, u)
	if err != nil {
		writeErr(w, err, c)
		return
	}
	w.Header().Set("Content-type", "text/json; charset=utf-8")
	jsonEncoder := json.NewEncoder(w)
	jsonEncoder.Encode(objs)
}
Exemple #4
0
func UserFromReq(r *http.Request) (*user.User, string) {

	c := appengine.NewContext(r)
	uType := ""

	//
	//
	u := user.Current(c)
	if u == nil {
		uType += "OAuth1 fail "
	} else {
		uType += "OAuth1 succ "
	}
	uType += "\n"

	//
	//
	u2, err := user.CurrentOAuth(c, "")
	if err != nil {
		uType += fmt.Sprintf("OAuth2 fail: %v", err)
	}
	if u2 != nil {
		uType += "OAuth2 succ "
	}
	uType += "\n"

	if appengine.IsDevAppServer() {
		if u2.Email == "*****@*****.**" {
			uType += fmt.Sprintf("OAuth2 reset %q. ", u2.Email)
			u2 = nil
		}
		uType += "CurrentOAuth() always exists on DEV system."
	}

	// Replace
	if u == nil {
		u = u2
	}

	return u, uType
}
Exemple #5
0
// Sign creates a new Greeting based on provided NewGreet.
// It stores newly created Greeting with Content being that of NewGreet.Message.
// Author field will be either currently signed in user or "Anonymous User".
func (gs *GreetingService) Sign(
	r *http.Request, req *NewGreet, greet *Greeting) error {

	ctx := appengine.NewContext(r)

	greet.Content = req.Message
	greet.Date = time.Now()

	// Most likely, this is not currently supported, yet.
	if u, err := user.CurrentOAuth(ctx, authScope); err == nil {
		greet.Author = u.String()
	} else {
		greet.Author = "Anonymous User"
	}

	key, err := datastore.Put(
		ctx, datastore.NewIncompleteKey(ctx, "Greeting", nil), greet)
	if err != nil {
		return err
	}

	greet.Id = key.Encode()
	return nil
}
Exemple #6
0
// Currently a stub.
// See https://github.com/crhym3/go-endpoints/issues/2
func CurrentUserWithScope(c appengine.Context, scope string) (*user.User, error) {
	return user.CurrentOAuth(c, scope)
}
Exemple #7
0
func handle(w http.ResponseWriter, req *http.Request) {
	c := appengine.NewContext(req)

	u := user.Current(c)
	if u == nil {
		u, _ = user.CurrentOAuth(c, "")
	}

	if u == nil || !u.Admin {
		w.Header().Set("Content-Type", "text/plain; charset=utf-8")
		w.WriteHeader(http.StatusUnauthorized)
		io.WriteString(w, "You must be logged in as an administrator to access this.\n")
		return
	}
	if req.Header.Get("X-Appcfg-Api-Version") == "" {
		w.Header().Set("Content-Type", "text/plain; charset=utf-8")
		w.WriteHeader(http.StatusForbidden)
		io.WriteString(w, "This request did not contain a necessary header.\n")
		return
	}

	if req.Method != "POST" {
		// Response must be YAML.
		rtok := req.FormValue("rtok")
		if rtok == "" {
			rtok = "0"
		}
		w.Header().Set("Content-Type", "text/yaml; charset=utf-8")
		fmt.Fprintf(w, `{app_id: %q, rtok: %q}`, c.FullyQualifiedAppID(), rtok)
		return
	}

	defer req.Body.Close()
	body, err := ioutil.ReadAll(req.Body)
	if err != nil {
		w.WriteHeader(http.StatusBadRequest)
		c.Errorf("Failed reading body: %v", err)
		return
	}
	remReq := &pb.Request{}
	if err := proto.Unmarshal(body, remReq); err != nil {
		w.WriteHeader(http.StatusBadRequest)
		c.Errorf("Bad body: %v", err)
		return
	}

	service, method := *remReq.ServiceName, *remReq.Method
	if !requestSupported(service, method) {
		w.WriteHeader(http.StatusBadRequest)
		c.Errorf("Unsupported RPC /%s.%s", service, method)
		return
	}

	rawReq := &rawMessage{remReq.Request}
	rawRes := &rawMessage{}
	err = c.Call(service, method, rawReq, rawRes, nil)

	remRes := &pb.Response{}
	if err == nil {
		remRes.Response = rawRes.buf
	} else if ae, ok := err.(*appengine_internal.APIError); ok {
		remRes.ApplicationError = &pb.ApplicationError{
			Code:   &ae.Code,
			Detail: &ae.Detail,
		}
	} else {
		// This shouldn't normally happen.
		c.Errorf("appengine/remote_api: Unexpected error of type %T: %v", err, err)
		remRes.ApplicationError = &pb.ApplicationError{
			Code:   proto.Int32(0),
			Detail: proto.String(err.Error()),
		}
	}
	out, err := proto.Marshal(remRes)
	if err != nil {
		// This should not be possible.
		w.WriteHeader(500)
		c.Errorf("proto.Marshal: %v", err)
		return
	}

	c.Infof("Spooling %d bytes of response to /%s.%s", len(out), service, method)
	w.Header().Set("Content-Type", "application/octet-stream")
	w.Header().Set("Content-Length", strconv.Itoa(len(out)))
	w.Write(out)
}