aboutsummaryrefslogtreecommitdiff
path: root/src/dummy_auth
diff options
context:
space:
mode:
authorTim <contact@bytim.eu>2024-12-31 16:23:46 +0100
committerTim <contact@bytim.eu>2024-12-31 16:23:46 +0100
commit5314290bbec53379372d14234fdc4f28cbe3286a (patch)
tree32bcb32e394873692d16ff7d2c436a71acf2f6b3 /src/dummy_auth
downloaddummy-auth-5314290bbec53379372d14234fdc4f28cbe3286a.tar.xz
dummy-auth-5314290bbec53379372d14234fdc4f28cbe3286a.zip
Initial commit
Diffstat (limited to 'src/dummy_auth')
-rw-r--r--src/dummy_auth/config.clj8
-rw-r--r--src/dummy_auth/core.clj11
-rw-r--r--src/dummy_auth/error.clj5
-rw-r--r--src/dummy_auth/oauth2/auth.clj12
-rw-r--r--src/dummy_auth/oauth2/token.clj9
-rw-r--r--src/dummy_auth/oidc/userinfo.clj13
-rw-r--r--src/dummy_auth/routes.clj23
7 files changed, 81 insertions, 0 deletions
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))