Exemplo n.º 1
0
func saveToken(w http.ResponseWriter, r *http.Request) *appError {
	c := appengine.NewContext(r)
	if appErr := loadConfig(r); appErr != nil {
		return appErr
	}
	if user.Current(c) == nil {
		return &appError{nil, "Must be signed in to save token.", 400}
	}
	code := r.FormValue("code")
	if code == "" {
		return &appError{nil, "No 'code' parameter found", 500}
	}
	t := &oauth.Transport{
		Config:    &cfg,
		Transport: urlfetch.Client(c).Transport,
	}

	if _, err := t.Exchange(code); err != nil {
		return &appError{err, "Error exchanging code for token.", 500}
	}

	d := datastore.New(c)
	if err := d.SaveToken(user.Current(c).Email, t.Token); err != nil {
		return &appError{err, "Error saving token.", 500}
	}
	http.Redirect(w, r, "/app", http.StatusFound)
	return nil
}
Exemplo n.º 2
0
func loadConfig(r *http.Request) *appError {
	d := datastore.New(appengine.NewContext(r))
	params, err := d.Config()
	if err != nil {
		return &appError{err, "Error loading config from datastore.", 500}
	}
	cfg.ClientId = params.ClientID
	cfg.ClientSecret = params.ClientSecret
	return nil
}
Exemplo n.º 3
0
func saveConfigHandler(w http.ResponseWriter, r *http.Request) *appError {
	d := datastore.New(appengine.NewContext(r))
	cfg := datastore.Config{}
	cfg.ClientID = r.FormValue("ClientID")
	cfg.ClientSecret = r.FormValue("ClientSecret")
	if err := d.SaveConfig(cfg); err != nil {
		return &appError{err, "Error saving config", 500}
	}
	fmt.Fprintf(w, "Saved config!")
	return nil
}
Exemplo n.º 4
0
func initializeApiService(c appengine.Context) (*api.Service, error) {
	d := datastore.New(c)
	token, err := d.FindToken(user.Current(c).Email)
	if err != nil {
		return nil, err
	}

	transport := oauth.Transport{
		Token:     &token,
		Config:    &cfg,
		Transport: urlfetch.Client(c).Transport,
	}

	return api.NewFromClient(transport.Client(), token.AccessToken), nil
}
Exemplo n.º 5
0
func oauthHandler(w http.ResponseWriter, r *http.Request) *appError {
	c := appengine.NewContext(r)
	d := datastore.New(c)
	if user.Current(c) != nil {
		if _, err := d.FindToken(user.Current(c).Email); err == nil {
			// Maybe just serve the static file to avoid a redirect
			http.Redirect(w, r, "/app", http.StatusFound)
			return nil
		}
	}
	if appErr := loadConfig(r); appErr != nil {
		return appErr
	}
	destination := cfg.AuthCodeURL("savetoken") + "&response_type=code"
	http.Redirect(w, r, destination, http.StatusFound)
	return nil
}