func (c OAuthCache) Token() (*oauth.Token, error) { var tok oauth.Token c.bucket.Get(fmt.Sprintf("authtoken-%s", c.session["authtoken"]), &tok) if tok.Expired() || tok.AccessToken == "" { return nil, errors.New("No AccessToken available") } return &tok, nil }
// people fetches the list of people user has shared with this app func people(w http.ResponseWriter, r *http.Request) { c := appengine.NewContext(r) //client := urlfetch.Client(c) session, err := store.Get(r, "sessionName") if err != nil { c.Infof("error fetching session: %v", err) serveAppError(c, w, &appError{err, "Error fetching session", 500}) return } token := session.Values["accessToken"] // Only fetch a list of people for connected users if token == nil { m := "Current user not connected" serveAppError(c, w, &appError{errors.New(m), m, 401}) return } t := &oauth.Transport{Config: config, Transport: &urlfetch.Transport{Context: c}, } tok := new(oauth.Token) tok.AccessToken = token.(string) t.Token = tok service, err := plus.New(t.Client()) if err != nil { serveAppError(c, w, &appError{err, "Create Plus Client", 500}) return } // Get a list of people that this user has shared with this app people := service.People.List("me", "visible") peopleFeed, err := people.Do() if err != nil { m := "Failed to refresh access token" if err.Error() == "AccessTokenRefreshError" { serveAppError(c, w, &appError{errors.New(m), m, 500}) return } serveAppError(c, w, &appError{err, m, 500}) return } w.Header().Set("Content-type", "application/json") err = json.NewEncoder(w).Encode(&peopleFeed) if err != nil { serveAppError(c, w, &appError{err, "Convert PeopleFeed to JSON", 500}) } }
func people(w http.ResponseWriter, r *http.Request) *appError { log.Printf("Responding to request %s with people handler", r.URL.Path) session, err := store.Get(r, "DigitalCampusSession") if err != nil { log.Println("error fetching session:", err) return &appError{err, "Error fetching session", http.StatusInternalServerError} } token := session.Values["accessToken"] // Only fetch a list of people for connected users if token == nil { m := "Current user not connected" return &appError{errors.New(m), m, http.StatusUnauthorized} } // Create a new authorized API client t := &oauth.Transport{Config: config} tok := new(oauth.Token) tok.AccessToken = token.(string) t.Token = tok service, err := plus.New(t.Client()) if err != nil { return &appError{err, "Create Plus Client", http.StatusInternalServerError} } // Get a list of people that this user has shared with this app people := service.People.List("me", "visible") peopleFeed, err := people.Do() if err != nil { m := "Failed to refresh access token" if err.Error() == "AccessTokenRefreshError" { return &appError{errors.New(m), m, http.StatusInternalServerError} } return &appError{err, m, http.StatusInternalServerError} } w.Header().Set("Content-Type", "application/json") err = json.NewEncoder(w).Encode(&peopleFeed) if err != nil { return &appError{err, "Convert PeopleFeed to JSON", http.StatusInternalServerError} } log.Printf("Successfully handled request %s", r.URL.Path) return nil }
func (c *DBTokenCache) PutToken(t *goauth2.Token) error { if t.AccessToken == "" { return ErrEmptyAccessToken } var email string if t.Extra == nil || t.Extra["email"] == "" { conf, err := c.scheme.loadConfig() if err != nil { return err } transport := &goauth2.Transport{Config: &conf} transport.Token = t client := transport.Client() response, err := client.Get(c.scheme.InfoUrl) if err != nil { return err } defer response.Body.Close() email, err = c.scheme.Parser.Parse(response) if email == "" { return ErrEmptyUserEmail } user, err := auth.GetUserByEmail(email) if err != nil { if err != auth.ErrUserNotFound { return err } registrationEnabled, _ := config.GetBool("auth:user-registration") if !registrationEnabled { return err } user = &auth.User{Email: email} err := user.Create() if err != nil { return err } } err = user.CreateOnGandalf() if err != nil { log.Errorf("Ignored error trying to create user on gandalf: %s", err.Error()) } t.Extra = make(map[string]string) t.Extra["email"] = email } return makeToken(t).save() }