From 5314290bbec53379372d14234fdc4f28cbe3286a Mon Sep 17 00:00:00 2001 From: Tim Date: Tue, 31 Dec 2024 16:23:46 +0100 Subject: Initial commit --- .gitignore | 8 ++++++++ README.md | 6 ++++++ TODO.md | 2 ++ build.clj | 29 +++++++++++++++++++++++++++++ deps.edn | 12 ++++++++++++ src/dummy_auth/config.clj | 8 ++++++++ src/dummy_auth/core.clj | 11 +++++++++++ src/dummy_auth/error.clj | 5 +++++ src/dummy_auth/oauth2/auth.clj | 12 ++++++++++++ src/dummy_auth/oauth2/token.clj | 9 +++++++++ src/dummy_auth/oidc/userinfo.clj | 13 +++++++++++++ src/dummy_auth/routes.clj | 23 +++++++++++++++++++++++ 12 files changed, 138 insertions(+) create mode 100644 .gitignore create mode 100644 README.md create mode 100644 TODO.md create mode 100644 build.clj create mode 100644 deps.edn create mode 100644 src/dummy_auth/config.clj create mode 100644 src/dummy_auth/core.clj create mode 100644 src/dummy_auth/error.clj create mode 100644 src/dummy_auth/oauth2/auth.clj create mode 100644 src/dummy_auth/oauth2/token.clj create mode 100644 src/dummy_auth/oidc/userinfo.clj create mode 100644 src/dummy_auth/routes.clj diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..ddb8e57 --- /dev/null +++ b/.gitignore @@ -0,0 +1,8 @@ +.cpcache +target + +.idea +.nrepl-port +dummy-auth.iml + +config.json diff --git a/README.md b/README.md new file mode 100644 index 0000000..6f1790b --- /dev/null +++ b/README.md @@ -0,0 +1,6 @@ +# dummy-auth + +## Development +The following resources could be useful: +- https://www.oauth.com/oauth2-servers/server-side-apps/authorization-code/ +- https://www.oauth.com/oauth2-servers/server-side-apps/example-flow/ diff --git a/TODO.md b/TODO.md new file mode 100644 index 0000000..76a8df5 --- /dev/null +++ b/TODO.md @@ -0,0 +1,2 @@ +- [ ] proper error handling -> https://www.oauth.com/oauth2-servers/server-side-apps/possible-errors/ +- [ ] make information returned from the openid connect userinfo endpoint customizable diff --git a/build.clj b/build.clj new file mode 100644 index 0000000..564005f --- /dev/null +++ b/build.clj @@ -0,0 +1,29 @@ +(ns build + (:require [clojure.tools.build.api :as b] + [clojure.edn :as edn])) + +(def class-dir "target/classes") + +(def project (-> (edn/read-string (slurp "deps.edn")) + :aliases :neil :project)) +(def lib (:name project)) +(def version (:version project)) + +(def basis (b/create-basis {:project "deps.edn"})) +(def uber-file (format "target/%s-%s-standalone.jar" (name lib) version)) +(def jar-file (format "target/%s-%s.jar" (name lib) version)) + +(defn clean [_] + (b/delete {:path "target"})) + +(defn uber [_] + (clean nil) + (b/copy-dir {:src-dirs ["src" "resources"] + :target-dir class-dir}) + (b/compile-clj {:basis basis + :src-dirs ["src"] + :class-dir class-dir}) + (b/uber {:class-dir class-dir + :uber-file uber-file + :basis basis + :main 'dummy-auth.core})) diff --git a/deps.edn b/deps.edn new file mode 100644 index 0000000..b157efc --- /dev/null +++ b/deps.edn @@ -0,0 +1,12 @@ +{:paths ["src"] + :deps {http-kit/http-kit {:mvn/version "2.8.0"} + metosin/reitit {:mvn/version "0.7.2"} + org.clojure/data.json {:mvn/version "2.5.1"} + ring/ring-json {:mvn/version "0.5.1"}} + :aliases + {:neil {:project {:name dummy-auth/dummy-auth + :version "0.0.1"}} + :build {:deps {io.github.clojure/tools.build {:git/tag "v0.10.6" + :git/sha "52cf7d6"} + slipset/deps-deploy {:mvn/version "0.2.2"}} + :ns-default build}}} diff --git a/src/dummy_auth/config.clj b/src/dummy_auth/config.clj new file mode 100644 index 0000000..d8e4ce2 --- /dev/null +++ b/src/dummy_auth/config.clj @@ -0,0 +1,8 @@ +(ns dummy-auth.config + (:require [clojure.data.json :as cdjson])) + +(defn read-config [file] + (merge {:port 8080} + (-> file + slurp + (cdjson/read-str :key-fn keyword)))) diff --git a/src/dummy_auth/core.clj b/src/dummy_auth/core.clj new file mode 100644 index 0000000..3567d6b --- /dev/null +++ b/src/dummy_auth/core.clj @@ -0,0 +1,11 @@ +(ns dummy-auth.core + (:require [org.httpkit.server :as http-server] + [dummy-auth.config :as dconfig] + [dummy-auth.routes :as droutes]) + (:gen-class)) + +(defn -main [& args] + (let [config (dconfig/read-config (or (first args) "./config.json")) + port (:port config)] + (println (str "Starting http server on port " port "...")) + (http-server/run-server droutes/ring-handler {:port port}))) diff --git a/src/dummy_auth/error.clj b/src/dummy_auth/error.clj new file mode 100644 index 0000000..d09ec59 --- /dev/null +++ b/src/dummy_auth/error.clj @@ -0,0 +1,5 @@ +(ns dummy-auth.error + (:require [ring.util.response :as ruresp])) + +(defn handle [_req] + (ruresp/response "An error occurred while processing your request.")) diff --git a/src/dummy_auth/oauth2/auth.clj b/src/dummy_auth/oauth2/auth.clj new file mode 100644 index 0000000..50b4a0e --- /dev/null +++ b/src/dummy_auth/oauth2/auth.clj @@ -0,0 +1,12 @@ +(ns dummy-auth.oauth2.auth + (:require [ring.util.response :as ruresp])) + +(defn handle [req] + (let [redirect-uri (get-in req [:query-params "redirect_uri"]) + state (get-in req [:query-params "state"]) + redirection (cond + (and (some? redirect-uri) (some? state)) (str redirect-uri "?state=" state "&code=abc") + (some? redirect-uri) (str redirect-uri "?code=abc") + :else "/error")] + (println "oauth2/auth: redirecting to" redirection) + (ruresp/redirect redirection))) diff --git a/src/dummy_auth/oauth2/token.clj b/src/dummy_auth/oauth2/token.clj new file mode 100644 index 0000000..fd5a534 --- /dev/null +++ b/src/dummy_auth/oauth2/token.clj @@ -0,0 +1,9 @@ +(ns dummy-auth.oauth2.token + (:require [ring.util.response :as ruresp])) + +(defn handle [_req] + (println "oauth2/token: responding...") + (ruresp/response {"access_token" "abc" + "refresh_token" "abc" + "token_type" "Bearer" + "expires" 0})) diff --git a/src/dummy_auth/oidc/userinfo.clj b/src/dummy_auth/oidc/userinfo.clj new file mode 100644 index 0000000..97414bb --- /dev/null +++ b/src/dummy_auth/oidc/userinfo.clj @@ -0,0 +1,13 @@ +(ns dummy-auth.oidc.userinfo + (:require [ring.util.response :as ruresp])) + +;; https://openid.net/specs/openid-connect-core-1_0.html#UserInfo +(defn handle [_req] + (println "oidc/userinfo: responding...") + (ruresp/response {"sub" "0" + "name" "Jane Doe" + "given_name" "Jane" + "family_name" "Doe" + "preferred_username" "j.doe" + "email" "janedoe@example.com" + "picture" "http://example.com/janedoe/me.jpg"})) diff --git a/src/dummy_auth/routes.clj b/src/dummy_auth/routes.clj new file mode 100644 index 0000000..8ecdc1c --- /dev/null +++ b/src/dummy_auth/routes.clj @@ -0,0 +1,23 @@ +(ns dummy-auth.routes + (:require [reitit.ring :as rring] + [ring.middleware.params :as rmparams] + [ring.middleware.json :as rmjson] + + [dummy-auth.error :as derror] + [dummy-auth.oauth2.auth :as do2auth] + [dummy-auth.oauth2.token :as do2token] + [dummy-auth.oidc.userinfo :as doidcuserinfo])) + +(def routes [["/oauth2" + ["/authorize" {:get {:handler do2auth/handle}}] + ["/token" {:post {:handler do2token/handle}}]] + ["/oidc" + ["/userinfo" {:get {:handler doidcuserinfo/handle} + :post {:handler doidcuserinfo/handle}}]] + ["/error" {:get {:handler derror/handle}}]]) + +(def ring-handler (-> routes + rring/router + rring/ring-handler + rmjson/wrap-json-response + rmparams/wrap-params)) -- cgit v1.2.3