// Like Verify on a parsed URL func VerifyValues(values url.Values) (grant bool, identifier string, err error) { err = nil var postArgs url.Values postArgs = url.Values(map[string][]string{}) // Create the url URLEndPoint := values.Get("openid.op_endpoint") if URLEndPoint == "" { log.Printf("no openid.op_endpoint") return false, "", errors.New("no openid.op_endpoint") } for k, v := range values { postArgs[k] = v } postArgs.Set("openid.mode", "check_authentication") postContent := postArgs.Encode() // Post the request var client = new(http.Client) postReader := bytes.NewBuffer([]byte(postContent)) response, err := client.Post(URLEndPoint, "application/x-www-form-urlencoded", postReader) if err != nil { log.Printf("VerifyValues failed at post") return false, "", err } // Parse the response // Convert the reader // We limit the size of the response to 1024 bytes but it should be large enough for most cases buffer := make([]byte, 1024) _, err = response.Body.Read(buffer) if err != nil { log.Printf("VerifyValues failed reading response") return false, "", err } // Check for ns rematch := REVerifyDirectNs.FindSubmatch(buffer) if rematch == nil { return false, "", errors.New("VerifyValues: ns value not found on the response of the OP") } nsValue := string(rematch[1]) if !bytes.Equal([]byte(nsValue), []byte("http://specs.openid.net/auth/2.0")) { return false, "", errors.New("VerifyValues: ns value not correct: " + nsValue) } // Check for is_valid match, err := regexp.Match(REVerifyDirectIsValid, buffer) if err != nil { return false, "", err } identifier = values.Get("openid.claimed_id") if !match { log.Printf("no is_valid:true in \"%s\"", buffer) } return match, identifier, nil }
func RetrieveUserInfo(client oauth2_client.OAuth2Client, nickName string, m url.Values) (*GetUserInfoResponse, os.Error) { resp := new(GetUserInfoResponse) if m == nil { m = make(url.Values) } m.Set("NickName", nickName) err := retrieveInfo(client, "smugmug.users.getInfo", m, resp) return resp, err }
func RetrieveSelfUser(client oauth2_client.OAuth2Client, m url.Values) (*User, os.Error) { resp := new(User) if m == nil { m = make(url.Values) } m.Set("include_entities", "true") err := retrieveInfo(client, "account/verify_credentials.json", "", m, resp) return resp, err }
func RetrieveAlbumInfo(client oauth2_client.OAuth2Client, albumId int64, albumKey string, m url.Values) (*GetAlbumInfoResponse, os.Error) { resp := new(GetAlbumInfoResponse) if m == nil { m = make(url.Values) } m.Set("AlbumID", strconv.Itoa64(albumId)) m.Set("AlbumKey", albumKey) err := retrieveInfo(client, "smugmug.albums.getInfo", m, resp) return resp, err }
func RetrieveContactSyncForUser(client oauth2_client.OAuth2Client, userId string, revno int64, m url.Values) (*ContactSyncResponse, os.Error) { resp := new(ContactSyncResponse) if m == nil { m = make(url.Values) } m.Set("view", "sync") m.Set("rev", strconv.Itoa64(revno)) err := retrieveInfo(client, "user", userId, "contacts", "", "", "", m, resp) return resp, err }
func RetrieveAlbums(client oauth2_client.OAuth2Client, heavy bool, m url.Values) (*GetAlbumsResponse, os.Error) { resp := new(GetAlbumsResponse) if m == nil { m = make(url.Values) } if heavy { m.Set("Heavy", "true") } err := retrieveInfo(client, "smugmug.albums.get", m, resp) return resp, err }
func RetrieveProfileImage(client oauth2_client.OAuth2Client, screenName, size string, userId int64, includeEntities bool, m url.Values) (*http.Response, os.Error) { uri := TWITTER_API_ENDPOINT + "users/profile_image" if m == nil { m = make(url.Values) } if len(screenName) > 0 { m.Set("screen_name", screenName) } else { return nil, os.NewError("Must specify either screenName in twitter.RetrieveProfileImage()") } resp, _, err := oauth2_client.AuthorizedGetRequest(client, nil, uri, m) return resp, err }
func (t *Transport) updateToken(tok *Token, v url.Values) os.Error { v.Set("client_id", t.ClientId) v.Set("client_secret", t.ClientSecret) r, err := (&http.Client{Transport: t.transport()}).PostForm(t.TokenURL, v) if err != nil { return err } defer r.Body.Close() if r.StatusCode != 200 { return os.NewError("invalid response: " + r.Status) } if err = json.NewDecoder(r.Body).Decode(tok); err != nil { return err } if tok.TokenExpiry != 0 { tok.TokenExpiry = time.Seconds() + tok.TokenExpiry } return nil }
func retrieveInfo(client oauth2_client.OAuth2Client, scope, userId, projection, id string, m url.Values, value interface{}) (err os.Error) { var useUserId string if len(userId) <= 0 { useUserId = GOOGLE_DEFAULT_USER_ID } else { useUserId = url.QueryEscape(userId) } if len(projection) <= 0 { projection = GOOGLE_DEFAULT_PROJECTION } headers := make(http.Header) headers.Set("GData-Version", "3.0") if m == nil { m = make(url.Values) } if len(m.Get(CONTACTS_ALT_PARAM)) <= 0 { m.Set(CONTACTS_ALT_PARAM, "json") } uri := GOOGLE_FEEDS_API_ENDPOINT for _, s := range []string{scope, useUserId, projection, id} { if len(s) > 0 { if uri[len(uri)-1] != '/' { uri += "/" } uri += s } } resp, _, err := oauth2_client.AuthorizedGetRequest(client, headers, uri, m) if err != nil { return err } if resp != nil { if resp.StatusCode >= 400 { b, _ := ioutil.ReadAll(resp.Body) err = os.NewError(string(b)) } else { err = json.NewDecoder(resp.Body).Decode(value) } } return err }
func RetrieveAlbumStats(client oauth2_client.OAuth2Client, albumId int64, year, month int, heavy bool, m url.Values) (*AlbumStatsResponse, os.Error) { resp := new(AlbumStatsResponse) if m == nil { m = make(url.Values) } m.Set("AlbumID", strconv.Itoa64(albumId)) m.Set("Year", strconv.Itoa(year)) m.Set("Month", strconv.Itoa(month)) if heavy { m.Set("Heavy", "true") } err := retrieveInfo(client, "smugmug.albums.getStats", m, resp) return resp, err }
func retrieveInfo(client oauth2_client.OAuth2Client, method string, m url.Values, value interface{}) (err os.Error) { uri := SMUGMUG_API_ENDPOINT headers := make(http.Header) headers.Set("Accept", "application/json") if m == nil { m = make(url.Values) } m.Set("method", method) resp, _, err := oauth2_client.AuthorizedGetRequest(client, headers, uri, m) if err != nil { return err } if resp != nil { var e ErrorResponse b, _ := ioutil.ReadAll(resp.Body) json.Unmarshal(b, &e) if e.Stat != "ok" { err = &e } else { err = json.Unmarshal(b, value) } } return err }
func RetrieveUser(client oauth2_client.OAuth2Client, screenName string, userId int64, includeEntities bool, m url.Values) (*User, os.Error) { resp := new(User) if m == nil { m = make(url.Values) } if userId > 0 { m.Set("user_id", strconv.Itoa64(userId)) } else if len(screenName) > 0 { m.Set("screen_name", screenName) } else { return nil, os.NewError("Must specify either userId or screenName in twitter.RetrieveUser()") } if includeEntities { m.Set("include_entities", "true") } err := retrieveInfo(client, "users/show.json", "", m, resp) return resp, err }
func RetrieveStatus(client oauth2_client.OAuth2Client, id int64, trimUser, includeEntities bool, m url.Values) (*Status, os.Error) { resp := new(Status) if m == nil { m = make(url.Values) } if id > 0 { m.Set("id", strconv.Itoa64(id)) } else { return nil, os.NewError("Must specify either status Id in twitter.RetrieveStatus()") } if trimUser { m.Set("trim_user", "true") } if includeEntities { m.Set("include_entities", "true") } err := retrieveInfo(client, "statuses/show.json", "", m, resp) return resp, err }
func RetrieveStatusesWithOptions(client oauth2_client.OAuth2Client, screenName string, userId, sinceId, count, maxId, page int64, trimUser, includeRts, includeEntities, excludeReplies, contributorDetails bool, m url.Values) (*[]Status, os.Error) { resp := new([]Status) if m == nil { m = make(url.Values) } if userId > 0 { m.Set("user_id", strconv.Itoa64(userId)) } else if len(screenName) > 0 { m.Set("screen_name", screenName) } else { return nil, os.NewError("Must specify either userId or screenName in twitter.RetrieveStatus()") } if sinceId > 0 { m.Set("since_id", strconv.Itoa64(sinceId)) } if count > 0 { m.Set("count", strconv.Itoa64(count)) } if maxId > 0 { m.Set("max_id", strconv.Itoa64(maxId)) } if page > 0 { m.Set("page", strconv.Itoa64(page)) } if trimUser { m.Set("trim_user", "true") } if includeRts { m.Set("include_rts", "true") } if includeEntities { m.Set("include_entities", "true") } if excludeReplies { m.Set("exclude_replies", "true") } if contributorDetails { m.Set("contributor_details", "true") } err := retrieveInfo(client, "statuses/user_timeline.json", "", m, resp) return resp, err }