Skip to content

Commit

Permalink
Merge pull request #17 from GaiwanTeam/feature/discord
Browse files Browse the repository at this point in the history
Feature/discord
  • Loading branch information
JohnnyJayJay authored Aug 15, 2024
2 parents 61527b6 + 8d1411d commit c2b1395
Show file tree
Hide file tree
Showing 8 changed files with 336 additions and 164 deletions.
90 changes: 50 additions & 40 deletions data/tito/registrations.edn
Original file line number Diff line number Diff line change
@@ -1,40 +1,50 @@
({:email "elisabeth.rau@rauandsons.net",
:id 3694535,
:name "Elisabeth Rau",
:reference "UAA3"}
{:email "ellsworth81@doyle-doyle.info",
:id 5870786,
:name "Ellsworth Doyle",
:reference "AZCE"}
{:email "willie97@schowalterschow.name",
:id 7495219,
:name "Willie Schowalter",
:reference "U1L3"}
{:email "augustus@rogahnrogahnand.net",
:id 928267,
:name "Augustus Rogahn",
:reference "7N7G"}
{:email "preston.rice@ricericeandrice.biz",
:id 1295322,
:name "Preston Rice",
:reference "722H"}
{:email "myrtis85@gorczanyinc.com",
:id 7430047,
:name "Myrtis Gorczany",
:reference "ZDAO"}
{:email "glynda82@nikolausnikolau.com",
:id 8078654,
:name "Glynda Nikolaus",
:reference "TFYW"}
{:email "aurelia@damoredamoreand.com",
:id 9037751,
:name "Aurelia D'Amore",
:reference "W2WB"}
{:email "janay@predovicpredovi.io",
:id 2405625,
:name "Janay Predovic",
:reference "B01E"}
{:email "juana@powlowskipowlow.co",
:id 8833862,
:name "Juana Powlowski",
:reference "78W6"})
({:email "damon82@purdypurdyandpu.biz",
:id 3943464,
:name "Amb. Damon Purdy",
:reference "QUTU",
:state "complete"}
{:email "calvin@howellhowelland.name",
:id 7425449,
:name "Pres. Calvin Howell",
:reference "8A3F",
:state "complete"}
{:email "brendonrogahn@rogahn-rogahn.org",
:id 8328992,
:name "Brendon Rogahn",
:reference "M0Q4",
:state "complete"}
{:email "reanna68@kingkingandking.info",
:id 4421735,
:name "Reanna King",
:reference "7TL1",
:state "complete"}
{:email "thomashodkiewicz@hodkiewiczandso.biz",
:id 2195685,
:name "Thomas Hodkiewicz",
:reference "22G5",
:state "complete"}
{:email "adelina@kreigerllc.info",
:id 662414,
:name "Rep. Adelina Kreiger",
:reference "9891",
:state "complete"}
{:email "nichelle5@langworthinc.biz",
:id 7191893,
:name "Ms. Nichelle Langworth",
:reference "T7Y3",
:state "complete"}
{:email "guillermina.sawayn@sawaynsawaynand.co",
:id 6741258,
:name "Msgr. Guillermina Sawayn",
:reference "41L3",
:state "complete"}
{:email "zachariah@mrazmrazandmraz.co",
:id 5894895,
:name "Zachariah Mraz",
:reference "P3C3",
:state "complete"}
{:email "mitchell@donnellydonnell.info",
:id 1123123,
:name "Mitchell Donnelly CPA",
:reference "M80Q",
:state "complete"})
167 changes: 87 additions & 80 deletions data/tito/tickets.edn
Original file line number Diff line number Diff line change
@@ -1,91 +1,98 @@
({:email "thuy.green@greengroup.net",
:id 9350106,
:name "Thuy Green",
:reference "UAA3-1",
:registration-id 3694535,
:release-id 1494672,
:state "reminder"}
{:email "santiago.mraz@mrazandsons.name",
:id 1409765,
:name "Santiago Mraz",
:reference "AZCE-1",
:registration-id 5870786,
:release-id 1492465,
({:email "yetta@gusikowski-gusi.info",
:id 1511427,
:name "Yetta Gusikowski",
:reference "QUTU-1",
:registration-id 3943464,
:release-id 1499881,
:state "reminder"}
{:email "tomiquigley@quigley-quigley.com",
:id 2292401,
:name "Tomi Quigley",
:reference "U1L3-1",
:registration-id 7495219,
:release-id 1492844,
:state "incomplete"}
{:email "werner@collierinc.net",
:id 7892017,
:name "Werner Collier",
:reference "7N7G-1",
:registration-id 928267,
{:email "ferdinand@emmerichemmeric.biz",
:id 4091138,
:name "Ferdinand Emmerich",
:reference "QUTU-2",
:registration-id 3943464,
:release-id 1509582,
:state "complete"}
{:email "rodrick76@okunevaokunevaa.biz",
:id 3046436,
:name "Rodrick Okuneva",
:reference "QUTU-3",
:registration-id 3943464,
:release-id 1509582,
:state "new"}
{:email "bella@klingklingandkl.com",
:id 5004792,
:name "Prof. Bella Kling",
:reference "722H-1",
:registration-id 1295322,
:release-id 1492846,
:state "reminder"}
{:email "osvaldowilliamson@williamson-will.io",
:id 2094205,
:name "Osvaldo Williamson",
:reference "ZDAO-1",
:registration-id 7430047,
:release-id 1494678,
{:email "lenhickle@hickle-hickle.com",
:id 3774432,
:name "Len Hickle",
:reference "8A3F-1",
:registration-id 7425449,
:release-id 1492845,
:state "new"}
{:email "josefine@herzogherzogand.name",
:id 3923717,
:name "Josefine Herzog I",
:reference "M0Q4-1",
:registration-id 8328992,
:release-id 1503936,
:state "complete"}
{:email "vannessalittle@little-little.net",
:id 6905618,
:name "Vannessa Little",
:reference "7TL1-1",
:registration-id 4421735,
:release-id 1503936,
:state "incomplete"}
{:email "adalberto@huelsllc.org",
:id 9301116,
:name "The Hon. Adalberto Huels",
:reference "TFYW-1",
:registration-id 8078654,
:release-id 1507003,
{:email "guadalupe80@fritschfritscha.org",
:id 8806928,
:name "Guadalupe Fritsch",
:reference "22G5-1",
:registration-id 2195685,
:release-id 1494678,
:state "incomplete"}
{:email "alton35@walter-walter.co",
:id 4821390,
:name "Alton Walter",
:reference "TFYW-2",
:registration-id 8078654,
:release-id 1494672,
{:email "muriel@murraygroup.biz",
:id 8009514,
:name "Muriel Murray",
:reference "22G5-2",
:registration-id 2195685,
:release-id 1503936,
:state "incomplete"}
{:email "marquis26@keeblergroup.info",
:id 5249373,
:name "Marquis Keebler",
:reference "TFYW-3",
:registration-id 8078654,
{:email "sharonlind@lind-lind.biz",
:id 6813580,
:name "Sharon Lind I",
:reference "9891-1",
:registration-id 662414,
:release-id 1494678,
:state "complete"}
{:email "jasper@osinskiosinskia.net",
:id 4875657,
:name "Jasper Osinski",
:reference "T7Y3-1",
:registration-id 7191893,
:release-id 1494672,
:state "new"}
{:email "keshia.kerluke@kerlukellc.net",
:id 7202708,
:name "Keshia Kerluke DDS",
:reference "41L3-1",
:registration-id 6741258,
:release-id 1492844,
:state "reminder"}
{:email "elmawalter@waltergroup.com",
:id 7506318,
:name "Elma Walter",
:reference "W2WB-1",
:registration-id 9037751,
:release-id 1492846,
:state "reminder"}
{:email "tanja83@weberllc.org",
:id 1736733,
:name "Tanja Weber",
:reference "B01E-1",
:registration-id 2405625,
:release-id 1509582,
{:email "maxwell@hintzinc.info",
:id 4196913,
:name "Maxwell Hintz",
:reference "41L3-2",
:registration-id 6741258,
:release-id 1494672,
:state "incomplete"}
{:email "trentfeest@feest-feest.net",
:id 4358782,
:name "Trent Feest DO",
:reference "B01E-2",
:registration-id 2405625,
{:email "bok@kunze-kunze.net",
:id 7193569,
:name "Bok Kunze IV",
:reference "P3C3-1",
:registration-id 5894895,
:release-id 1494678,
:state "incomplete"}
{:email "teddy@dibbertandsons.org",
:id 3883988,
:name "Prof. Teddy Dibbert",
:reference "78W6-1",
:registration-id 8833862,
:release-id 1503936,
:state "complete"})
{:email "william@mclaughlininc.net",
:id 8690234,
:name "William McLaughlin III",
:reference "M80Q-1",
:registration-id 1123123,
:release-id 1507003,
:state "reminder"})
2 changes: 1 addition & 1 deletion resources/public/css/styles.css
Original file line number Diff line number Diff line change
Expand Up @@ -516,4 +516,4 @@ body {
display: grid;
grid-template-columns: 10rem 1fr;
gap: var(--size-2);
}
}
64 changes: 61 additions & 3 deletions src/co/gaiwan/compass/http/oauth.clj
Original file line number Diff line number Diff line change
@@ -1,14 +1,21 @@
(ns co.gaiwan.compass.http.oauth
"Namespace for generic OAuth2 handling (authorization code flow).
For now, this contains Discord-specific scopes and URLs, like the routes/oauth namespace as well.
This can eventually be changed."
(:require
[clojure.string :as str]
[co.gaiwan.compass.config :as config]
[co.gaiwan.compass.db :as db]
[lambdaisland.uri :as uri]))
[co.gaiwan.compass.util :as util]
[hato.client :as hato]
[lambdaisland.uri :as uri]
[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"])
(def default-scopes ["email" "identify" "guilds.join"])

(defn flow-init-url
([]
Expand All @@ -27,3 +34,54 @@
:scope (str/join " " scopes)
:state state})))))

;; Add as bot to server
#_
(println (str (flow-init-url ["bot" "applications.commands"])))

(defn request-token [params]
(hato/post
"https://discord.com/api/oauth2/token"
{:as :auto
:form-params params
:basic-auth
{:user (config/value :discord/client-id)
:pass (config/value :discord/client-secret)}}))

(defn exchange-code [code]
(request-token
{:grant_type "authorization_code"
:code code
:redirect_uri (str (config/value :compass/origin) "/oauth2/discord/callback")}))

(defn refresh-token [refresh-token]
(request-token
{:grant_type "refresh_token"
:refresh_token refresh-token}))

(defn current-access-token
"Get a valid Discord bearer access token for a user.
This assumes there already is an access token in the database.
The function performs a token refresh if the old access token is considered to be expired.
If refreshing the token fails, returns `nil`.
There are no guarantees regarding the lifetime of the returned access token, so this function
should be called for every request to Discord."
[user-id]
(let [oauth-data
(db/q '[:find (pull ?u [:discord/access-token
:discord/refresh-token
:discord/expires-at])
:in $ ?uid
:where [?u :user/uuid ?uid]]
(db/db)
user-id)]
;; check if token is expired, do a refresh in that case
(if (.isAfter (Instant/now) (:discord/expires-at oauth-data))
(let [{:keys [body status]} (refresh-token (:discord/refresh-token oauth-data))]
(when (= status 200)
(db/transact [{:user/uuid user-id
:discord/access-token (:access_token body)
:discord/refresh-token (:refresh_token body)
:discord/expires-at (util/expires-in->instant (:discord/expires_in body))}])
(:access_token body)))
(:discord/access-token oauth-data))))
Loading

0 comments on commit c2b1395

Please sign in to comment.