From 0382fa9888516ff60dd93b6d5ec2d3c4cce0ea14 Mon Sep 17 00:00:00 2001 From: kagel Date: Sun, 21 Aug 2016 03:29:39 +0300 Subject: [PATCH] Github integration hello-world --- env/dev/resources/config.edn | 8 +++---- env/prod/resources/config.edn | 6 ++--- env/test/resources/config.edn | 8 +++---- project.clj | 1 + resources/templates/home.html | 1 + src/clj/commiteth/github/core.clj | 34 +++++++++++++++++++++++++++ src/clj/commiteth/handler.clj | 2 ++ src/clj/commiteth/layout.clj | 1 + src/clj/commiteth/routes/redirect.clj | 15 ++++++++++++ src/cljs/commiteth/core.cljs | 2 +- 10 files changed, 63 insertions(+), 15 deletions(-) create mode 100644 src/clj/commiteth/github/core.clj create mode 100644 src/clj/commiteth/routes/redirect.clj diff --git a/env/dev/resources/config.edn b/env/dev/resources/config.edn index dba383f..072067c 100644 --- a/env/dev/resources/config.edn +++ b/env/dev/resources/config.edn @@ -1,6 +1,4 @@ -{:dev true - :port 3000 +{:dev true + :port 3000 ;; when :nrepl-port is set the application starts the nREPL server on load - :nrepl-port 7000 - :github-access-token "c9323a357c2beeebe4e8d618e0fda47dc9e15f62" - :github-user "kagel"} + :nrepl-port 7000} diff --git a/env/prod/resources/config.edn b/env/prod/resources/config.edn index f8213e5..e7685a8 100644 --- a/env/prod/resources/config.edn +++ b/env/prod/resources/config.edn @@ -1,4 +1,2 @@ -{:production true - :port 3000 - :github-access-token "c9323a357c2beeebe4e8d618e0fda47dc9e15f62" - :github-user "kagel"} +{:production true + :port 3000} diff --git a/env/test/resources/config.edn b/env/test/resources/config.edn index ca9d9b1..ed90d2f 100644 --- a/env/test/resources/config.edn +++ b/env/test/resources/config.edn @@ -1,6 +1,4 @@ -{:test true - :port 3001 +{:test true + :port 3001 ;; when :nrepl-port is set the application starts the nREPL server on load - :nrepl-port 7001 - :github-access-token "c9323a357c2beeebe4e8d618e0fda47dc9e15f62" - :github-user "kagel"} + :nrepl-port 7001} diff --git a/project.clj b/project.clj index 88df704..a6e8dba 100644 --- a/project.clj +++ b/project.clj @@ -29,6 +29,7 @@ [org.clojure/tools.cli "0.3.5"] [luminus-nrepl "0.1.4"] [buddy "1.0.0"] + [buddy/buddy-auth "1.1.0"] [luminus-migrations "0.2.6"] [conman "0.6.0"] [org.postgresql/postgresql "9.4.1209"] diff --git a/resources/templates/home.html b/resources/templates/home.html index 6ac2dd3..c657c11 100644 --- a/resources/templates/home.html +++ b/resources/templates/home.html @@ -21,6 +21,7 @@ {% script "/js/app.js" %} diff --git a/src/clj/commiteth/github/core.clj b/src/clj/commiteth/github/core.clj new file mode 100644 index 0000000..75da8ab --- /dev/null +++ b/src/clj/commiteth/github/core.clj @@ -0,0 +1,34 @@ +(ns commiteth.github.core + (:require [tentacles.repos :as repos] + [ring.util.codec :as codec] + [clj-http.client :as http]) + (:import [java.util UUID])) + +(def client-id "caf23d90246fa99ca545") +(def client-secret "e8e7a088e7769c77e9e2d87c47ef81df51080bf3") +(def redirect-uri "http://localhost:3000/callback") +(def allow-signup true) + +(defn authorize-url [] + (let [params (codec/form-encode {:client_id client-id + :redirect_uri redirect-uri + :allow_signup allow-signup + :state (str (UUID/randomUUID))})] + (str "https://github.com/login/oauth/authorize" "?" params))) + +(defn post-for-token + [code state] + (http/post "https://github.com/login/oauth/access_token" + {:content-type :json + :form-params {:client_id client-id + :client_secret client-secret + :code code + :redirect_uri redirect-uri + :state state}})) + +(defn list-repos + [token] + (repos/repos + {:oauth-token token + :client-id client-id + :client-token client-secret})) diff --git a/src/clj/commiteth/handler.clj b/src/clj/commiteth/handler.clj index 6527512..8523968 100644 --- a/src/clj/commiteth/handler.clj +++ b/src/clj/commiteth/handler.clj @@ -2,6 +2,7 @@ (:require [compojure.core :refer [routes wrap-routes]] [commiteth.layout :refer [error-page]] [commiteth.routes.home :refer [home-routes]] + [commiteth.routes.redirect :refer [redirect-routes]] [commiteth.routes.services :refer [service-routes]] [compojure.route :as route] [commiteth.env :refer [defaults]] @@ -17,6 +18,7 @@ (-> #'home-routes (wrap-routes middleware/wrap-csrf) (wrap-routes middleware/wrap-formats)) + #'redirect-routes #'service-routes (route/not-found (:body diff --git a/src/clj/commiteth/layout.clj b/src/clj/commiteth/layout.clj index b10f380..8e648da 100644 --- a/src/clj/commiteth/layout.clj +++ b/src/clj/commiteth/layout.clj @@ -20,6 +20,7 @@ (parser/render-file template (assoc params + :authorize-url (commiteth.github.core/authorize-url) :page template :csrf-token *anti-forgery-token* :servlet-context *app-context*))) diff --git a/src/clj/commiteth/routes/redirect.clj b/src/clj/commiteth/routes/redirect.clj new file mode 100644 index 0000000..942759b --- /dev/null +++ b/src/clj/commiteth/routes/redirect.clj @@ -0,0 +1,15 @@ +(ns commiteth.routes.redirect + (:require [clojure.walk :refer [keywordize-keys]] + [compojure.core :refer [defroutes GET]] + [ring.util.codec :as codec] + [commiteth.github.core :as github])) + +(defroutes redirect-routes + (GET "/callback" [code state] + (let [resp (github/post-for-token code state) + body (keywordize-keys (codec/form-decode (:body resp))) + access-token (:access_token body)] + (if-let [error (:error body)] + (str "Error: " error) + (str "Your access token: " access-token "
" + "Your repositories: " (reduce str (github/list-repos access-token))))))) diff --git a/src/cljs/commiteth/core.cljs b/src/cljs/commiteth/core.cljs index b5e720e..5d057f1 100644 --- a/src/cljs/commiteth/core.cljs +++ b/src/cljs/commiteth/core.cljs @@ -34,7 +34,7 @@ [:div.jumbotron [:h1 "Welcome to commitETH"] [:p [:a.btn.btn-block.btn-social.btn-github - {:href "http://github.com"} + {:href js/authorizeUrl} [:i.fa.fa-github] "Sign in with GitHub"]]]])