예제 #1
0
파일: oauth.go 프로젝트: vokal/googauth
func tokenFromWeb(g_config *oauth.Config) *oauth.Token {
	ch := make(chan string)
	randState := fmt.Sprintf("st%d", time.Now())

	var listener *net.TCPListener

	go serveCallback(listener, randState, ch)
	defer listener.Close()

	g_config.RedirectURL = "http://localhost:8080/callback"
	authUrl := g_config.AuthCodeURL(randState)
	go openUrl(authUrl)
	log.Printf("Authorize this app at: %s", authUrl)
	code := <-ch
	log.Printf("Got code: %s", code)

	t := &oauth.Transport{
		Config:    g_config,
		Transport: condDebugTransport(http.DefaultTransport),
	}
	_, err := t.Exchange(code)
	if err != nil {
		log.Fatalf("Token exchange error: %v", err)
	}
	return t.Token
}
예제 #2
0
func tokenFromWeb(g_config *oauth.Config) *oauth.Token {
	ch := make(chan string)
	randState := fmt.Sprintf("st%d", time.Now())
	ts := httptest.NewServer(http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) {
		if req.URL.Path == "/favicon.ico" {
			http.Error(rw, "", 404)
			return
		}
		if req.FormValue("state") != randState {
			log.Printf("State doesn't match: req = %#v", req)
			http.Error(rw, "", 500)
			return
		}
		if code := req.FormValue("code"); code != "" {
			fmt.Fprintf(rw, "<h1>Success</h1>Authorized.")
			rw.(http.Flusher).Flush()
			ch <- code
			return
		}
		log.Printf("no code")
		http.Error(rw, "", 500)
	}))
	defer ts.Close()

	g_config.RedirectURL = ts.URL
	fmt.Println(ts.URL)
	authUrl := g_config.AuthCodeURL(randState)
	go openUrl(authUrl)
	log.Printf("Authorize this app at: %s", authUrl)
	code := <-ch
	log.Printf("Got code: %s", code)

	t := &oauth.Transport{
		Config:    g_config,
		Transport: condDebugTransport(http.DefaultTransport),
	}
	_, err := t.Exchange(code)
	if err != nil {
		log.Fatalf("Token exchange error: %v", err)
	}
	return t.Token
}
예제 #3
0
func tokenFromWeb(config *oauth.Config) *oauth.Token {
	ch := make(chan string)
	randState := fmt.Sprintf("st%d", time.Now().UnixNano())
	http.HandleFunc("/auth", func(rw http.ResponseWriter, req *http.Request) {
		if req.FormValue("state") != randState {
			log.Printf("State doesn't match: req = %#v", req)
			http.Error(rw, "", 500)
			return
		}
		if code := req.FormValue("code"); code != "" {
			fmt.Fprintf(rw, "<h1>Success</h1>Authorized.")
			rw.(http.Flusher).Flush()
			ch <- code
			return
		}
		log.Printf("no code")
		http.Error(rw, "", 500)
	})

	config.RedirectURL = fmt.Sprintf("http://localhost:%s/auth", *port)
	authUrl := config.AuthCodeURL(randState)
	go openUrl(authUrl)
	log.Printf("Authorize this app at: %s", authUrl)
	code := <-ch
	log.Printf("Got code: %s", code)

	t := &oauth.Transport{
		Config:    config,
		Transport: http.DefaultTransport,
	}
	_, err := t.Exchange(code)
	if err != nil {
		log.Fatalf("Token exchange error: %v", err)
	}
	return t.Token
}
예제 #4
0
func tokenFromWeb(config *oauth.Config) *oauth.Token {
	randState := fmt.Sprintf("st%d", time.Now().UnixNano())

	config.RedirectURL = RedirectURI
	authURL := config.AuthCodeURL(randState)

	log.Info("Opening auth URL in browser.")
	log.Info(authURL)
	log.Info("If the URL doesn't open please open it manually and copy the code here.")
	openURL(authURL)
	code := getCodeFromStdin()

	log.Infof("Got code: %s", code)

	t := &oauth.Transport{
		Config:    config,
		Transport: http.DefaultTransport,
	}
	_, err := t.Exchange(code)
	if err != nil {
		log.Fatalf("Token exchange error: %v", err)
	}
	return t.Token
}
예제 #5
0
파일: oauth.go 프로젝트: 0x7cc/rsc
// Token obtains an OAuth token, keeping a cached copy in file.
// If the file name is not an absolute path, it is interpreted relative to the
// user's home directory.
func Token(file string, cfg *oauth.Config) (*oauth.Transport, error) {
	if !filepath.IsAbs(file) {
		file = filepath.Join(os.Getenv("HOME"), file)
	}
	cfg1 := *cfg
	cfg = &cfg1
	cfg.TokenCache = oauth.CacheFile(file)
	tok, err := cfg.TokenCache.Token()
	if err == nil {
		return &oauth.Transport{Config: cfg, Token: tok}, nil
	}

	// Start HTTP server on localhost.
	l, err := net.Listen("tcp", "127.0.0.1:0")
	if err != nil {
		var err1 error
		if l, err1 = net.Listen("tcp6", "[::1]:0"); err1 != nil {
			return nil, fmt.Errorf("oauthprompt.Token: starting HTTP server: %v", err)
		}
	}

	type done struct {
		err  error
		code string
	}
	ch := make(chan done, 100)

	randState, err := randomID()
	if err != nil {
		return nil, err
	}

	cfg.RedirectURL = "http://" + l.Addr().String() + "/done"
	authURL := cfg1.AuthCodeURL(randState)

	handler := http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
		if req.URL.Path == "/auth" {
			http.Redirect(w, req, authURL, 301)
			return
		}
		if req.URL.Path != "/done" {
			http.Error(w, "", 404)
			return
		}
		if req.FormValue("state") != randState {
			ch <- done{err: fmt.Errorf("oauthprompt.Token: incorrect response")}
			http.Error(w, "", 500)
			return
		}
		if code := req.FormValue("code"); code != "" {
			ch <- done{code: code}
			w.Write([]byte(success))
			return
		}
		http.Error(w, "", 500)
	})

	srv := &http.Server{Handler: handler}
	go srv.Serve(l)
	if err := openURL("http://" + l.Addr().String() + "/auth"); err != nil {
		l.Close()
		return nil, err
	}
	d := <-ch
	l.Close()

	if d.err != nil {
		return nil, err
	}

	tr := &oauth.Transport{Config: &cfg1}
	_, err = tr.Exchange(d.code)
	if err != nil {
		return nil, err
	}
	return tr, nil
}