func Search(w http.ResponseWriter, r *http.Request, params url.Values, limit, offset int) { session, err := auth.GetSession(r) if err != nil { util.Error(err, w) return } userID := r.URL.Query().Get("user_id") if len(userID) > 0 { user, err := users.GetByID(userID) if err != nil { util.Error(err, w) return } query := r.URL.Query().Get("query") res, err := db.Search(user, query) util.ToJSON(res, err, w) return } else { var user users.User if u, ok := session.Values["user"]; ok { user = u.(users.User) log.Println(user) query := r.URL.Query().Get("query") res, err := db.Search(user, query) util.ToJSON(res, err, w) return } } util.Error(errors.New("Unable to get session"), w) }
func parseRow(row utils.Row) (interface{}, error) { var o Link var userID string var meta, tags []byte err := row.Scan( &o.ID, &userID, &o.Title, &o.URL, &o.AddedAt, &o.LastViewedAt, &o.Viewed, &meta, &tags, &o.ClickCount, ) if err != nil { return o, err } err = json.Unmarshal(meta, &o.Meta) if err != nil { return o, err } err = json.Unmarshal(tags, &o.Tags) if err != nil { return o, err } o.User, err = users.GetByID(userID) if err != nil { return o, err } err = o.LoadTags() return o, err }
func Auth(w http.ResponseWriter, r *http.Request) { var params = r.URL.Query() var code = params.Get("code") if len(code) == 0 { utils.Error(errors.New("No code supplied for auth."), w) return } cfg := config.Get() clientID := cfg.GetString("google_client_id") clientSecret := cfg.GetString("google_client_secret") conf := &oauth2.Config{ ClientID: clientID, ClientSecret: clientSecret, RedirectURL: "postmessage", Scopes: []string{ "https://www.googleapis.com/auth/plus.login", "https://www.googleapis.com/auth/plus.profile.emails.read", }, Endpoint: google.Endpoint, } tok, err := conf.Exchange(oauth2.NoContext, code) if err != nil { utils.Error(errors.New("Unable to exchange code for a token."), w) return } if !tok.Valid() { // 401 utils.Error(errors.New("Invalid token."), w) return } client := conf.Client(oauth2.NoContext, tok) service, err := plus.New(client) if err != nil { utils.Error(errors.New("Unable to create a transport client."), w) return } gplusID, err := decodeIDToken(tok.Extra("id_token").(string)) if err != nil { utils.Error(errors.New("Unable to decode ID Token."), w) return } people := service.People.Get(gplusID) person, err := people.Do() if err != nil { log.Println(err) utils.Error(errors.New("Unable to get Google Plus profile"), w) return } user, err := users.GetByID(person.Id) if err != nil { user, err = users.New(person.DisplayName, person.Id) if err != nil { log.Println(err) utils.Error(errors.New("Unable to create new user in the database"), w) return } } log.Printf("Person: %+v\n", person) // logged in session, err := GetSession(r) if err != nil { log.Println(err) utils.Error(errors.New("Unable to get session."), w) return } session.Values["expires"] = time.Now().Add(time.Hour * 24) session.Values["user"] = user session.Options.MaxAge = 86400 * 30 if err := str.Save(r, w, session); err != nil { log.Println(err) utils.Error(errors.New("Unable to save session."), w) return } utils.ToJSON(user, err, w) }