func (rt RT) authenticate(jar *cookiejar.Jar) error { client := &http.Client{ Jar: jar, } data := url.Values{} data.Add("user", rt.Username) data.Add("pass", rt.Password) client.PostForm(rt.BaseURL, data) // Check that we got back at least one cookie -> probably successful authentication! // Alternatively could check that RT_SID_url.80 exists url, err := url.Parse(rt.BaseURL) if err != nil { syslog.Errf("Unable to parse BaseURL: %v", err) panic(err) } if len(jar.Cookies(url)) < 1 { return errors.New("Authentication to RT failed!") } return nil }
func GetCookie(cookieJar *cookiejar.Jar, name string) string { cookieUrl, _ := url.Parse("http://tieba.baidu.com") cookies := cookieJar.Cookies(cookieUrl) for _, cookie := range cookies { if name == cookie.Name { return cookie.Value } } return "" }
func Http_send(strUrl string, postDict map[string]string, cookies string, header map[string]string) ([]byte, string, error) { var gCurCookieJar *cookiejar.Jar gCurCookieJar, _ = cookiejar.New(nil) httpClient := &http.Client{Jar: gCurCookieJar} tr := &http.Transport{ TLSClientConfig: &tls.Config{InsecureSkipVerify: true}, } httpClient.Transport = tr var httpReq *http.Request var err error = nil if nil == postDict { httpReq, _ = http.NewRequest("GET", strUrl, nil) } else { postValues := url.Values{} for postKey, PostValue := range postDict { postValues.Set(postKey, PostValue) } postDataStr := postValues.Encode() postDataBytes := []byte(postDataStr) postBytesReader := bytes.NewReader(postDataBytes) httpReq, _ = http.NewRequest("POST", strUrl, postBytesReader) if header["Content-Type"] == "" { httpReq.Header.Add("Content-Type", "application/x-www-form-urlencoded") } } httpReq.Header.Add("Cookie", cookies) for headerKey, headerValue := range header { httpReq.Header.Add(headerKey, headerValue) } httpResp, err := httpClient.Do(httpReq) if err != nil { return make([]byte, 1), "", err } defer httpResp.Body.Close() body, errReadAll := ioutil.ReadAll(httpResp.Body) if errReadAll != nil { return make([]byte, 1), "", errReadAll } var gCurCookies []*http.Cookie gCurCookies = gCurCookieJar.Cookies(httpReq.URL) var cookieNum int = len(gCurCookies) cookies = "" for i := 0; i < cookieNum; i++ { var curCk *http.Cookie = gCurCookies[i] cookies += curCk.Name + "=" + curCk.Value + ";" } return body, cookies, nil }
func csrfTokenFromCookies(cookiejar *cookiejar.Jar) string { urlParse, _ := url.Parse(INSTAGRAM_HOST) for _, cookie := range cookiejar.Cookies(urlParse) { if cookie.Name == "csrftoken" { return cookie.Value } } return "" }
var oauthState auth.OAuthState err = json.Unmarshal(decoded, &oauthState) Expect(err).ToNot(HaveOccurred()) Expect(oauthState.Redirect).To(Equal("/some-path")) }) It("sets the base64-encoded redirect URI as the OAuth state cookie", func() { Expect(fakeProviderB.AuthCodeURLCallCount()).To(Equal(1)) state, _ := fakeProviderB.AuthCodeURLArgsForCall(0) serverURL, err := url.Parse(server.URL) Expect(err).ToNot(HaveOccurred()) Expect(cookieJar.Cookies(serverURL)).To(ContainElement(&http.Cookie{ Name: auth.OAuthStateCookie, Value: state, })) }) }) Context("to an unknown provider", func() { BeforeEach(func() { request.URL.Path = "/auth/bogus" }) It("returns Not Found", func() { Expect(response.StatusCode).To(Equal(http.StatusNotFound)) }) })
func getFinalURL(jar *cookiejar.Jar, addr *url.URL, level int) (*url.URL, error) { if flDebug { log.Printf("new request (level %d): %s", level, addr.String()) } if level == 0 { return nil, fmt.Errorf("too many redirects") } if addr.Scheme != "http" && addr.Scheme != "https" { return nil, fmt.Errorf("unsupported url scheme: %s", addr.Scheme) } req, err := http.NewRequest("GET", addr.String(), nil) if err != nil { return nil, fmt.Errorf("request create err: %s", err) } cookies := jar.Cookies(addr) for _, cookie := range cookies { if flDebug { log.Printf("add cookie: %s", cookie.String()) } req.AddCookie(cookie) } req.Header.Set("User-Agent", userAgent) resp, err := http.DefaultTransport.RoundTrip(req) if err != nil { if uErr, ok := err.(*url.Error); !ok || uErr.Err != errStopRedirect { return nil, fmt.Errorf("request err: %s", err) } } defer resp.Body.Close() ct := strings.ToLower(resp.Header.Get("Content-Type")) if flDebug { log.Printf("response code: %d, content-type %s", resp.StatusCode, ct) } jar.SetCookies(resp.Request.URL, resp.Cookies()) if resp.StatusCode == 301 || resp.StatusCode == 302 || resp.StatusCode == 303 || resp.StatusCode == 307 { location := resp.Header.Get("Location") addr, err = url.Parse(location) if err != nil { return nil, err } return getFinalURL(jar, addr, level-1) } if !strings.HasPrefix(ct, "text/html") { if flDebug { log.Printf("not html content-type: %s", ct) } return resp.Request.URL, err } buf := &bytes.Buffer{} _, err = io.CopyN(buf, resp.Body, 1024*1024*10) if err != nil && err != io.EOF { return nil, err } data := bytes.ToLower(buf.Bytes()) if flDebug { log.Printf("html: %s", string(data)) } metaRedirect, err := findMetaRedirect(data) if err != nil { return nil, fmt.Errorf("meta redirect err: %s", err) } if metaRedirect == nil { return addr, nil } if flDebug { log.Printf("html meta redirect: %s", metaRedirect) } return getFinalURL(jar, metaRedirect, level-1) }
func GetCookies(cookieJar *cookiejar.Jar) []*http.Cookie { cookieUrl, _ := url.Parse("http://tieba.baidu.com") cookies := cookieJar.Cookies(cookieUrl) return cookies }