From 9097e1508b3f9d378da4941172098f957d5ad58a Mon Sep 17 00:00:00 2001 From: Mihai Parparita Date: Sat, 5 Jul 2014 22:53:34 -0700 Subject: [PATCH] Add basic GitHub OAuth flow. --- .gitignore | 1 + TODO | 1 + app/config/github-oauth.json.SAMPLE | 5 +++ app/githop.go | 51 ++++++++++++++++++++++++----- app/templates/index.html | 7 ++-- 5 files changed, 53 insertions(+), 12 deletions(-) create mode 100644 .gitignore create mode 100644 TODO create mode 100644 app/config/github-oauth.json.SAMPLE diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..afc1d00 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +app/config/*oauth*.json diff --git a/TODO b/TODO new file mode 100644 index 0000000..cceb921 --- /dev/null +++ b/TODO @@ -0,0 +1 @@ +- Option to only do this for public repos (repo vs. public_repo scopes) diff --git a/app/config/github-oauth.json.SAMPLE b/app/config/github-oauth.json.SAMPLE new file mode 100644 index 0000000..100dd32 --- /dev/null +++ b/app/config/github-oauth.json.SAMPLE @@ -0,0 +1,5 @@ +{ + "ClientId": "REPLACE_ME", + "ClientSecret": "REPLACE_ME", + "RedirectURL": "https://REPLACE_ME/github/callback" +} diff --git a/app/githop.go b/app/githop.go index 361c76d..c6c7350 100644 --- a/app/githop.go +++ b/app/githop.go @@ -1,12 +1,16 @@ package githop import ( - "github.com/google/go-github/github" + "encoding/json" "html/template" + "io/ioutil" "net/http" "appengine" - "appengine/urlfetch" + "appengine/urlfetch" + + "code.google.com/p/goauth2/oauth" + "github.com/google/go-github/github" ) func init() { @@ -16,16 +20,47 @@ func init() { var indexTemplate = template.Must(template.ParseFiles("templates/index.html")) func index(w http.ResponseWriter, r *http.Request) { - appengine_context := appengine.NewContext(r) - http_client := urlfetch.Client(appengine_context) - - gitub_client := github.NewClient(http_client) - orgs, _, err := gitub_client.Organizations.List("mihaip", nil) + // TODO: Don't do this every request + github_oauth_config_path := "config/github-oauth" + if appengine.IsDevAppServer() { + github_oauth_config_path += "-dev" + } + github_oauth_config_path += ".json" + github_oauth_config_bytes, err := ioutil.ReadFile(github_oauth_config_path) if err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) return } - if err := indexTemplate.Execute(w, orgs); err != nil { + var github_oauth_config oauth.Config + err = json.Unmarshal(github_oauth_config_bytes, &github_oauth_config) + if err != nil { + http.Error(w, err.Error(), http.StatusInternalServerError) + return + } + github_oauth_config.Scope = "repo" + github_oauth_config.AuthURL = "https://github.com/login/oauth/authorize" + github_oauth_config.TokenURL = "https://github.com/login/oauth/access_token" + + code := r.FormValue("code") + if code == "" { + http.Redirect(w, r, github_oauth_config.AuthCodeURL(""), http.StatusFound) + return + } + appengine_context := appengine.NewContext(r) + oauth_transport := &oauth.Transport{ + Config: &github_oauth_config, + Transport: &urlfetch.Transport{Context: appengine_context}, + } + token, _ := oauth_transport.Exchange(code) + oauth_transport.Token = token + + gitub_client := github.NewClient(oauth_transport.Client()) + repos, _, err := gitub_client.Repositories.List("", nil) + if err != nil { + http.Error(w, err.Error(), http.StatusInternalServerError) + return + } + if err := indexTemplate.Execute(w, repos); err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) } } diff --git a/app/templates/index.html b/app/templates/index.html index 0d0ee46..326dc60 100644 --- a/app/templates/index.html +++ b/app/templates/index.html @@ -8,13 +8,12 @@

GitHop!

- mihaip is in the following organizations: + Your repositories: