Skip to content

Commit

Permalink
feat: join-server oauth function
Browse files Browse the repository at this point in the history
  • Loading branch information
JohnnyJayJay committed Aug 10, 2024
1 parent 82a1829 commit b3fcb8b
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 10 deletions.
4 changes: 1 addition & 3 deletions resources/co/gaiwan/compass/dev.edn
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
{:compass/origin "http://localhost:8099"
:datomic/url "datomic:mem://compass"
:http/show-exception-details? true
:discord/client-id "983315200398733362"
:discord/public-key "fca03a0789343028329c53ea68af6f6ed32858283329615dd89ce0dd3908c8d3"}
:http/show-exception-details? true}
33 changes: 26 additions & 7 deletions src/co/gaiwan/compass/http/oauth.clj
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,20 @@
[datomic.api :as d]
[hato.client :as hato]
[lambdaisland.uri :as uri]
[ring.util.response :as response])
[ring.util.response :as response]
[io.pedestal.log :as log])
(:import (java.time Instant)))

(def discord-oauth-endpoint "https://discord.com/oauth2/authorize")
(def discord-api-endpoint "https://discord.com/api/v10")


(def default-scopes ["email" "identify" "guilds.join" "role_connections.write"])

(defn bot-auth-headers []
{"Authorization" (str "Bot " (config/value :discord/bot-token))})


(defn flow-init-url
([]
(flow-init-url nil))
Expand Down Expand Up @@ -88,11 +94,22 @@
(:access_token body)))
(:discord/access-token oauth-data))))


(defn fetch-user-info [token]
(:body
(hato/get (str discord-api-endpoint "/users/@me")
{:as :auto
:headers {"Authorization" (str "Bearer " token)}})))
:oauth-token token})))

(defn join-server [token]
(let [{:keys [id username]} (fetch-user-info token)]
(log/trace :discord/adding-user username)
(hato/put
(str discord-api-endpoint "/guilds/" (config/value :discord/server-id) "/members/" id)
{:as :auto
:content-type :json
:form-params {:access_token token}
:headers (bot-auth-headers)})))

(defn GET-callback [{:keys [query-params]}]
(let [code (get query-params "code")
Expand All @@ -115,20 +132,22 @@
:discord/id id
:discord/access-token access_token
:discord/refresh-token refresh_token
:discord/expires-at (expires-in->instant expires_in)}]]
(def tx-data tx-data)
@(db/transact tx-data )
:discord/expires-at (expires-in->instant expires_in)}]
{:keys [status]} (join-server access_token)]
@(db/transact tx-data)
{:status 302
:headers {"Location" "/"}
:flash [:p "You are signed in!"]
:flash [:p "You are signed in!"
(when-not (= status 200)
[:br "Unfortunately, adding you to our Discord server didn't work."])]
:session {:identity user-uuid}}))))

(defn routes []
[""
["/oauth2"
["/discord"
["/callback"
{:get {:handler GET-callback}}]]]
{:get {:handler #'GET-callback}}]]]
["/logout"
{:get {:handler (fn [req]
(assoc
Expand Down

0 comments on commit b3fcb8b

Please sign in to comment.