-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathserver.clj
85 lines (70 loc) · 2.89 KB
/
server.clj
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
(ns cerber.oauth2.standalone.server
(:require [cerber.handlers :as handlers]
[cerber.oauth2.context :as ctx]
[cerber.oauth2.core :as core]
[cerber.oauth2.standalone.config :refer [app-config]]
[cerber.store :refer :all]
[compojure.core :refer [defroutes GET POST routes wrap-routes]]
[conman.core :as conman]
[mount.core :as mount :refer [defstate]]
[org.httpkit.server :as web]
[ring.middleware.defaults :refer [api-defaults wrap-defaults]]))
(defn user-info-handler
[req]
{:status 200
:body (select-keys (::ctx/user req) [:login :name :email :roles])})
(defroutes oauth2-routes
(GET "/authorize" [] handlers/authorization-handler)
(POST "/approve" [] handlers/client-approve-handler)
(GET "/refuse" [] handlers/client-refuse-handler)
(POST "/token" [] handlers/token-handler)
(GET "/login" [] handlers/login-form-handler)
(POST "/login" [] handlers/login-submit-handler)
(GET "/logout" [] handlers/logout-handler))
(defroutes restricted-routes
(GET "/users/me" [] user-info-handler))
(def app-handler
(wrap-defaults
(routes oauth2-routes
(wrap-routes restricted-routes handlers/wrap-authorized))
api-defaults))
(defn init-server
"Initializes preconfigured users, clients and standalone
HTTP server handling OAuth2 endpoints."
[]
(core/init-users (:users app-config))
(core/init-clients (:clients app-config))
(when-let [url (:landing-url app-config)]
(core/set-landing-url! url))
(when-let [http-config (:server app-config)]
(web/run-server app-handler http-config)))
(defstate db-conn
:start (and (Class/forName "org.h2.Driver")
(conman/connect! {:init-size 1
:min-idle 1
:max-idle 4
:max-active 32
:jdbc-url "jdbc:h2:mem:testdb;MODE=MySQL;INIT=RUNSCRIPT FROM 'classpath:/db/migrations/h2/cerber_schema.sql'"
;:driver-class "org.postgresql.Driver"
;:jdbc-url "jdbc:postgresql://localhost:5432/template1?user=postgres"
}))
:stop (conman/disconnect! db-conn))
;; oauth2 stores
(defstate client-store
:start (core/create-client-store :sql db-conn)
:stop (close! client-store))
(defstate user-store
:start (core/create-user-store :sql db-conn)
:stop (close! user-store))
(defstate token-store
:start (core/create-token-store :sql db-conn)
:stop (close! token-store))
(defstate authcode-store
:start (core/create-authcode-store :sql db-conn)
:stop (close! authcode-store))
(defstate session-store
:start (core/create-session-store :sql db-conn)
:stop (close! session-store))
(defstate http-server
:start (init-server)
:stop (when http-server (http-server)))