Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/discord #17

Merged
merged 15 commits into from
Aug 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 "[email protected]",
:id 3694535,
:name "Elisabeth Rau",
:reference "UAA3"}
{:email "[email protected]",
:id 5870786,
:name "Ellsworth Doyle",
:reference "AZCE"}
{:email "[email protected]",
:id 7495219,
:name "Willie Schowalter",
:reference "U1L3"}
{:email "[email protected]",
:id 928267,
:name "Augustus Rogahn",
:reference "7N7G"}
{:email "[email protected]",
:id 1295322,
:name "Preston Rice",
:reference "722H"}
{:email "[email protected]",
:id 7430047,
:name "Myrtis Gorczany",
:reference "ZDAO"}
{:email "[email protected]",
:id 8078654,
:name "Glynda Nikolaus",
:reference "TFYW"}
{:email "[email protected]",
:id 9037751,
:name "Aurelia D'Amore",
:reference "W2WB"}
{:email "[email protected]",
:id 2405625,
:name "Janay Predovic",
:reference "B01E"}
{:email "[email protected]",
:id 8833862,
:name "Juana Powlowski",
:reference "78W6"})
({:email "[email protected]",
:id 3943464,
:name "Amb. Damon Purdy",
:reference "QUTU",
:state "complete"}
{:email "[email protected]",
:id 7425449,
:name "Pres. Calvin Howell",
:reference "8A3F",
:state "complete"}
{:email "[email protected]",
:id 8328992,
:name "Brendon Rogahn",
:reference "M0Q4",
:state "complete"}
{:email "[email protected]",
:id 4421735,
:name "Reanna King",
:reference "7TL1",
:state "complete"}
{:email "[email protected]",
:id 2195685,
:name "Thomas Hodkiewicz",
:reference "22G5",
:state "complete"}
{:email "[email protected]",
:id 662414,
:name "Rep. Adelina Kreiger",
:reference "9891",
:state "complete"}
{:email "[email protected]",
:id 7191893,
:name "Ms. Nichelle Langworth",
:reference "T7Y3",
:state "complete"}
{:email "[email protected]",
:id 6741258,
:name "Msgr. Guillermina Sawayn",
:reference "41L3",
:state "complete"}
{:email "[email protected]",
:id 5894895,
:name "Zachariah Mraz",
:reference "P3C3",
:state "complete"}
{:email "[email protected]",
: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 "[email protected]",
:id 9350106,
:name "Thuy Green",
:reference "UAA3-1",
:registration-id 3694535,
:release-id 1494672,
:state "reminder"}
{:email "[email protected]",
:id 1409765,
:name "Santiago Mraz",
:reference "AZCE-1",
:registration-id 5870786,
:release-id 1492465,
({:email "[email protected]",
:id 1511427,
:name "Yetta Gusikowski",
:reference "QUTU-1",
:registration-id 3943464,
:release-id 1499881,
:state "reminder"}
{:email "[email protected]",
:id 2292401,
:name "Tomi Quigley",
:reference "U1L3-1",
:registration-id 7495219,
:release-id 1492844,
:state "incomplete"}
{:email "[email protected]",
:id 7892017,
:name "Werner Collier",
:reference "7N7G-1",
:registration-id 928267,
{:email "[email protected]",
:id 4091138,
:name "Ferdinand Emmerich",
:reference "QUTU-2",
:registration-id 3943464,
:release-id 1509582,
:state "complete"}
{:email "[email protected]",
:id 3046436,
:name "Rodrick Okuneva",
:reference "QUTU-3",
:registration-id 3943464,
:release-id 1509582,
:state "new"}
{:email "[email protected]",
:id 5004792,
:name "Prof. Bella Kling",
:reference "722H-1",
:registration-id 1295322,
:release-id 1492846,
:state "reminder"}
{:email "[email protected]",
:id 2094205,
:name "Osvaldo Williamson",
:reference "ZDAO-1",
:registration-id 7430047,
:release-id 1494678,
{:email "[email protected]",
:id 3774432,
:name "Len Hickle",
:reference "8A3F-1",
:registration-id 7425449,
:release-id 1492845,
:state "new"}
{:email "[email protected]",
:id 3923717,
:name "Josefine Herzog I",
:reference "M0Q4-1",
:registration-id 8328992,
:release-id 1503936,
:state "complete"}
{:email "[email protected]",
: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 "[email protected]",
:id 4821390,
:name "Alton Walter",
:reference "TFYW-2",
:registration-id 8078654,
:release-id 1494672,
{:email "[email protected]",
:id 8009514,
:name "Muriel Murray",
:reference "22G5-2",
:registration-id 2195685,
:release-id 1503936,
:state "incomplete"}
{:email "[email protected]",
:id 5249373,
:name "Marquis Keebler",
:reference "TFYW-3",
:registration-id 8078654,
{:email "[email protected]",
:id 6813580,
:name "Sharon Lind I",
:reference "9891-1",
:registration-id 662414,
:release-id 1494678,
:state "complete"}
{:email "[email protected]",
:id 4875657,
:name "Jasper Osinski",
:reference "T7Y3-1",
:registration-id 7191893,
:release-id 1494672,
:state "new"}
{:email "[email protected]",
:id 7202708,
:name "Keshia Kerluke DDS",
:reference "41L3-1",
:registration-id 6741258,
:release-id 1492844,
:state "reminder"}
{:email "[email protected]",
:id 7506318,
:name "Elma Walter",
:reference "W2WB-1",
:registration-id 9037751,
:release-id 1492846,
:state "reminder"}
{:email "[email protected]",
:id 1736733,
:name "Tanja Weber",
:reference "B01E-1",
:registration-id 2405625,
:release-id 1509582,
{:email "[email protected]",
: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 "[email protected]",
:id 3883988,
:name "Prof. Teddy Dibbert",
:reference "78W6-1",
:registration-id 8833862,
:release-id 1503936,
:state "complete"})
{:email "[email protected]",
: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
Loading