Github integration hello-world

This commit is contained in:
kagel 2016-08-21 03:29:39 +03:00
parent 7e8d0f1f0b
commit 0382fa9888
10 changed files with 63 additions and 15 deletions

View File

@ -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}

View File

@ -1,4 +1,2 @@
{:production true
:port 3000
:github-access-token "c9323a357c2beeebe4e8d618e0fda47dc9e15f62"
:github-user "kagel"}
{:production true
:port 3000}

View File

@ -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}

View File

@ -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"]

View File

@ -21,6 +21,7 @@
<script type="text/javascript">
var context = "{{servlet-context}}";
var csrfToken = "{{csrf-token}}";
var authorizeUrl = "{{authorize-url}}";
</script>
{% script "/js/app.js" %}
</body>

View File

@ -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}))

View File

@ -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

View File

@ -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*)))

View File

@ -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 "<br/>"
"Your repositories: " (reduce str (github/list-repos access-token)))))))

View File

@ -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"]]]])