Exemplo n.º 1
0
// authCallbackHandler handles redirect from Facebook OAuth2 authorization page.
func authCallbackHandler(req *web.Request) {
	code := req.Param.Get("code")
	if code == "" {
		// should display error_reason
		req.Redirect("/", false)
		return
	}
	f, err := getUrlEncodedForm("https://graph.facebook.com/oauth/access_token",
		web.NewValues(
			"client_id", appID, // defined in settings.go
			"client_secret", appSecret, // defined in settings.go
			"redirect_uri", req.URL.Scheme+"://"+req.URL.Host+"/callback",
			"code", code))
	if err != nil {
		req.Error(web.StatusInternalServerError, err)
		return
	}
	token := f.Get("access_token")
	expires := f.Get("expires")
	if expires == "" {
		expires = "3600"
	}
	maxAge, err := strconv.Atoi(expires)
	if err != nil {
		maxAge = 3600
	} else {
		maxAge -= 30 // fudge
	}
	req.Redirect("/", false,
		web.HeaderSetCookie, web.NewCookie("fbtok", token).
			MaxAge(maxAge-30).String())
}
Exemplo n.º 2
0
// loginHandler redirects to Facebook OAuth2 authorization page.
func loginHandler(req *web.Request) {
	m := web.NewValues(
		"client_id", appID, // defined in settings.go
		"scope", "read_stream",
		"redirect_uri", req.URL.Scheme+"://"+req.URL.Host+"/callback")
	req.Redirect("https://graph.facebook.com/oauth/authorize?"+m.FormEncodedString(), false)
}
Exemplo n.º 3
0
func renameTag(req *web.Request) {
	// oldTag := req.Param.Get("oldTag")
	newTag := req.Param.Get("newTag")
	// updateTag(oldTag, newTag)
	url := fmt.Sprintf("/tag/%s", url.QueryEscape(newTag))
	req.Redirect(url, false)
}
Exemplo n.º 4
0
func pathHandler(req *web.Request, targetPattern string) {
	if newPath := req.Param.Get("path"); newPath == "" {
		req.Error(web.StatusNotFound, os.NewError("Not Found."))
	} else {
		newUrl := fmt.Sprintf(targetPattern, req.URL.Scheme, req.URL.Host, newPath)
		req.Redirect(newUrl, true)
	}
}
Exemplo n.º 5
0
func createCustomerHandler(req *web.Request) {
	orderId := req.Param.Get("order")
	name := req.Param.Get("name")
	email := req.Param.Get("email")
	id := NewUUID()
	storeCustomer(id.String(), name, email)
	req.Redirect("/order/?id="+orderId, false)
}
Exemplo n.º 6
0
func createEventHandler(req *web.Request) {
	orderId := req.Param.Get("order")
	name := req.Param.Get("name")
	value := req.Param.Get("value")
	id := NewUUID()
	storeEvent(id.String(), orderId, name, value)
	req.Redirect("/order/?id="+orderId, false)
}
Exemplo n.º 7
0
func viewHandler(req *web.Request) {
	title := req.URLParam["title"]
	p, err := loadPage(title)
	if err != nil {
		req.Redirect("/edit/"+title, false)
		return
	}
	renderTemplate(req, "view", p)
}
Exemplo n.º 8
0
// login redirects the user to the Twitter authorization page.
func login(req *web.Request) {
	callback := req.URL.Scheme + "://" + req.URL.Host + "/callback"
	temporaryCredentials, err := oauthClient.RequestTemporaryCredentials(http.DefaultClient, callback)
	if err != nil {
		req.Error(web.StatusInternalServerError, err)
		return
	}
	req.Redirect(oauthClient.AuthorizationURL(temporaryCredentials), false,
		web.HeaderSetCookie, credentialsCookie("tmp", temporaryCredentials, 0))
}
Exemplo n.º 9
0
func saveHandler(req *web.Request) {
	body := req.Param.Get("body")
	title := req.URLParam["title"]
	p := &page{Title: title, Body: []byte(body)}
	err := p.save()
	if err != nil {
		req.Error(web.StatusInternalServerError, err)
		return
	}
	req.Redirect("/view/"+title, false)
}
Exemplo n.º 10
0
func createLineHandler(req *web.Request) {
	orderId := req.Param.Get("order")
	customer := req.Param.Get("customer")
	name := req.Param.Get("name")
	quantity := req.Param.Get("quantity")
	price := req.Param.Get("price")
	id := NewUUID()
	quantityInt, _ := strconv.Atoi(quantity)
	priceInt, _ := strconv.Atof64(price)
	storeLine(id.String(), orderId, customer, name, quantityInt, priceInt)
	req.Redirect("/order/?id="+orderId, false)
}
Exemplo n.º 11
0
func saveHandler(req *web.Request) {
	permalink := req.Param.Get("page-permalink")
	title := req.Param.Get("page-title")
	description := req.Param.Get("page-description")
	pageTitle := req.Param.Get("page-page-title")
	keywords := req.Param.Get("page-keywords")
	content := req.Param.Get("page-content")
	template := req.Param.Get("page-template")

	byline := req.Param.Get("newsitem-byline")
	blurb := req.Param.Get("newsitem-blurb")
	fulldescription := req.Param.Get("newsitem-fulldescription")
	imagepath := req.Param.Get("newsitem-imagepath")
	externallink := req.Param.Get("newsitem-externallink")
	newscategory := req.Param.Get("newsitem-newscategory")
	contributedby := req.Param.Get("newsitem-contributedby")

	n := &NewsItem{Page: Page{Permalink: permalink,
		Title:       title,
		Description: description,
		Keywords:    keywords,
		PageTitle:   pageTitle,
		Content:     content,
		Template:    template},
		Tags:            strings.Split(req.Param.Get("newsitem-tags"), ",", -1),
		ContributedBy:   contributedby,
		Byline:          byline,
		PostedTime:      time.Seconds(),
		Blurb:           blurb,
		ImagePath:       imagepath,
		ExternalLink:    externallink,
		NewsCategory:    newscategory,
		FullDescription: fulldescription}

	mongo, err := mgo.Mongo("127.0.0.1")
	defer mongo.Close()
	if err != nil {
		panic(err)
	}

	c := mongo.DB(*database).C("newsitems")

	err = c.Upsert(bson.M{"page.permalink": permalink}, n)
	if err != nil {
		log.Println(err)
	}
	go removeCachedNewsItem(permalink)

	req.Redirect("/"+permalink, false)

}
Exemplo n.º 12
0
func handleSign(r *web.Request) {
	c := gae.Context(r)
	g := &Greeting{
		Content: r.Param.Get("content"),
		Date:    datastore.SecondsToTime(time.Seconds()),
	}
	if u := user.Current(c); u != nil {
		g.Author = u.String()
	}
	if _, err := datastore.Put(c, datastore.NewIncompleteKey("Greeting"), g); err != nil {
		r.Error(web.StatusInternalServerError, err)
		return
	}
	r.Redirect("/", false)
}
Exemplo n.º 13
0
// authCallback handles OAuth callbacks from Twitter.
func authCallback(req *web.Request) {
	temporaryCredentials, err := credentials(req, "tmp")
	if err != nil {
		req.Error(web.StatusNotFound, err)
		return
	}
	s := req.Param.Get("oauth_token")
	if s == "" {
		req.Error(web.StatusNotFound, os.NewError("main: no token"))
		return
	}
	if s != temporaryCredentials.Token {
		req.Error(web.StatusNotFound, os.NewError("main: token mismatch"))
		return
	}
	tokenCredentials, _, err := oauthClient.RequestToken(http.DefaultClient, temporaryCredentials, req.Param.Get("oauth_verifier"))
	if err != nil {
		req.Error(web.StatusNotFound, err)
		return
	}
	req.Redirect("/", false,
		web.HeaderSetCookie, credentialsCookie("tok", tokenCredentials, 30),
		web.HeaderSetCookie, web.NewCookie("tmp", "").Delete().String())
}
Exemplo n.º 14
0
// logoutHandler logs the user out by clearing the access token cookie.
func logoutHandler(req *web.Request) {
	req.Redirect("/", false,
		web.HeaderSetCookie, web.NewCookie("fbtok", "").Delete().String())
}
Exemplo n.º 15
0
func paidHandler(req *web.Request) {
	orderId := req.Param.Get("order")
	customerId := req.Param.Get("customer")
	payLines(orderId, customerId)
	req.Redirect("/order/?id="+orderId, false)
}