Beispiel #1
0
func (g *googleAPI) LoginWithCode(code string) (GoogleUser, error) {
	var valid = false

	fmt.Printf("%+v\n", g.config)

	token, err := g.config.Exchange(oauth2.NoContext, code)
	if err != nil {
		return nil, err
	}

	id, err := decodeToken(token.Extra("id_token").(string))
	if err != nil {
		return nil, err
	}

	client := g.config.Client(oauth2.NoContext, token)
	service, err := plus.New(client)
	getme := service.People.Get("me")
	me, err := getme.Do()

	email := "N/A"
	for _, e := range me.Emails {
		if e.Type == "account" {
			email = e.Value
		}
	}
	valid = true
	return &googleUser{id: id, valid: valid, email: email}, nil
}
Beispiel #2
0
func feedStorage(simpleFile string, lg *log.Logger) (FeedStorage, error) {
	simpleKey, err := ioutil.ReadFile(simpleFile)
	if err != nil {
		return nil, err
	}
	key := strings.TrimSpace(string(simpleKey))
	t := &SimpleKeyTransport{Key: key, Transport: http.DefaultTransport}
	srv, err := plus.New(&http.Client{Transport: t})
	if err != nil {
		return nil, err
	}
	retriever := &FeedRetriever{srv, lg}
	return retriever, nil
}
Beispiel #3
0
func googleCallbackHandler(w http.ResponseWriter, r *http.Request) {
	c := appengine.NewContext(r)
	code := r.FormValue("code")
	tr := emptyTransport()
	tr.Transport = &urlfetch.Transport{Context: c}
	if _, err := tr.Exchange(code); err != nil {
		c.Debugf("tr: %v\n", tr.Token)
		serveError(c, w, err)
		return
	}
	// get info on the user
	httpClient := tr.Client()
	p, err := plus.New(httpClient)
	if err != nil {
		serveError(c, w, err)
		return
	}
	c.Debugf("get person")
	person, err := p.People.Get("me").Do()
	c.Debugf("get person:::::::: %v\n", err)
	if err != nil {
		serveError(c, w, err)
		return
	}
	cookie := &http.Cookie{Name: "userId", Value: person.Id, Domain: appConfig.AppDomain, Path: "/", MaxAge: 30000000 /* about a year */}
	http.SetCookie(w, cookie)

	user := loadUser(r, person.Id)

	user.GoogleAccessToken = tr.Token.AccessToken
	user.GoogleTokenExpiry = tr.Token.Expiry.UnixNano()
	user.GoogleRefreshToken = tr.Token.RefreshToken
	if user.Id == "" {
		user.Id = person.Id
		user.GoogleLatest = time.Now().UnixNano()

	}
	saveUser(r, &user)

	http.Redirect(w, r, "/", http.StatusFound)
}
Beispiel #4
0
// GoogleCallback is callback for google oauth
func (ac *Auth) GoogleCallback(w http.ResponseWriter, r *http.Request) {
	sess := ac.SF.Get(r)
	stat := sess.Get("login_token")
	sess.Unset("login_token")
	if err := sess.Err(); err != nil {
		common.Fatalf(w, err, "Cannot read token from session")
		return
	}
	conf := ac.googleConfig(r)
	_ = r.ParseForm()
	code := r.Form.Get("code")
	state := r.Form.Get("state")
	if state != stat {
		http.Error(w, "Token mismatch!", http.StatusBadRequest)
		return
	}
	tok, err := conf.Exchange(oauth2.NoContext, code)
	if err != nil {
		common.Errorf(err, "Error occurs when exchanging token")
		http.Redirect(w, r, ac.Config.URL("/"), http.StatusTemporaryRedirect)
		return
	}
	client := conf.Client(oauth2.NoContext, tok)

	p, _ := plus.New(client)
	me, err := p.People.Get("me").Do()
	if err != nil {
		common.Errorf(err, "Error occurs when getting user info")
		http.Redirect(w, r, ac.Config.URL("/"), http.StatusTemporaryRedirect)
		return
	}

	var email string
	for _, e := range me.Emails {
		if email == "" {
			email = e.Value
		}
		if e.Type == "account" {
			email = e.Value
			break
		}
	}
	u, err := model.FindUser(email)
	if err != nil {
		common.Errorf(err, "Faile to save user, trying create one for %s.", email)
		u, err = model.NewUser(me.DisplayName, email, me.Image.Url)
		if err != nil {
			common.Fatalf(w, err, "Failed to login, please try again later.")
			return
		}
	}

	sess.Set("uid", fmt.Sprint(u.ID))
	sess.Save(r, w)
	if err := sess.Err(); err != nil {
		common.Fatal(w, err, "Cannot save user id in session")
		return
	}

	http.Redirect(w, r, ac.Config.URL("/"), http.StatusTemporaryRedirect)
}