Skip to content

Commit

Permalink
generates openapi.json and openapi.yaml in dev-resources on startup
Browse files Browse the repository at this point in the history
  • Loading branch information
invaliduser committed Feb 9, 2024
1 parent fad0428 commit 6d6c52d
Show file tree
Hide file tree
Showing 5 changed files with 599 additions and 66 deletions.
2 changes: 2 additions & 0 deletions deps.edn
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,10 @@

camel-snake-kebab/camel-snake-kebab {:mvn/version "0.4.2"}
cheshire/cheshire {:mvn/version "5.11.0"}
clj-commons/clj-yaml {:mvn/version "1.0.27"}
clojure.java-time/clojure.java-time {:mvn/version "1.2.0"}
danlentz/clj-uuid {:mvn/version "0.1.9"}
metosin/spec-tools {:mvn/version "0.10.6"}
aero/aero {:mvn/version "1.1.6"}
selmer/selmer {:mvn/version "1.12.59"}
ch.qos.logback/logback-classic {:mvn/version "1.3.14"}
Expand Down
11 changes: 11 additions & 0 deletions src/main/lrsql/admin/interceptors/lrs_management.clj
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,14 @@
(assoc ctx
:response {:status 200
:body params})))}))


#_(def holder (atom nil))
(def openapi
(interceptor
{:name ::openapi
:enter (fn openapi [ctx]
#_(reset! holder ctx)
(assoc ctx :response {:status 200
:body "OK"})
#_(let [{lrs :com.yetanalytics/lrs} ctx]))}))
222 changes: 157 additions & 65 deletions src/main/lrsql/admin/routes.clj
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@
[lrsql.admin.interceptors.jwt :as ji]
[lrsql.admin.interceptors.status :as si]
[lrsql.util.interceptor :as util-i]
[lrsql.util.headers :as h]))
[lrsql.util.headers :as h]
[lrsql.system.openapi :as oa]))

(defn- make-common-interceptors
[lrs sec-head-opts]
Expand All @@ -25,83 +26,170 @@
(defn admin-account-routes
[common-interceptors jwt-secret jwt-exp jwt-leeway no-val-opts]
#{;; Log into an existing account
["/admin/account/login" :post (conj common-interceptors
ai/validate-params
ai/authenticate-admin
(ai/generate-jwt jwt-secret jwt-exp))
:route-name :lrsql.admin.account/login]
;; Create new account
["/admin/account/create" :post (conj common-interceptors
(oa/annotate-short
["/admin/account/login" :post (conj common-interceptors
ai/validate-params
(ji/validate-jwt
jwt-secret jwt-leeway no-val-opts)
ji/validate-jwt-account
ai/create-admin)
:route-name :lrsql.admin.account/create]
ai/authenticate-admin
(ai/generate-jwt jwt-secret jwt-exp))
:route-name :lrsql.admin.account/login]
{:description "Log into an existing account"
:requestBody (oa/json-content (oa/owrap {:username {:type :string}
:password {:type :string}}))
:operationId :login
:responses {200 (oa/response "Account ID and JWT"
(oa/owrap {:account-id {:type :string}
:json-web-token {:type :string}}))
400 oa/error-400
401 oa/error-401}})
;; Create new account
(oa/annotate-short
["/admin/account/create" :post (conj common-interceptors
ai/validate-params
(ji/validate-jwt
jwt-secret jwt-leeway no-val-opts)
ji/validate-jwt-account
ai/create-admin)
:route-name :lrsql.admin.account/create]
{:description "Create new account"
:requestBody (oa/json-content (oa/owrap {:username {:type "string"}
:password {:type "string"}}))
:operationId :create-account
:security [{:bearerAuth []}]
:responses {200 (oa/response "ID of new account"
(oa/owrap {:account-id {:type "string"}}))
400 oa/error-401
401 oa/error-401}})
;; Update account password
["/admin/account/password"
:put (conj common-interceptors
ai/validate-update-password-params
(ji/validate-jwt jwt-secret jwt-leeway no-val-opts)
ji/validate-jwt-account
ai/update-admin-password)]
(oa/annotate-short
["/admin/account/password" :put (conj common-interceptors
ai/validate-update-password-params
(ji/validate-jwt jwt-secret jwt-leeway no-val-opts)
ji/validate-jwt-account
ai/update-admin-password)]
{:description "Update account password"
:requestBody (oa/json-content(oa/owrap {:old-password {:type :string}
:new-password {:type :string}}))
:operationId :update-password
:security [{:bearerAuth []}]
:responses {200 (oa/response "ID of updated account"
(oa/owrap {:account-id {:type "string"}}))
400 oa/error-400
401 oa/error-401}})
;; Get all accounts
["/admin/account" :get (conj common-interceptors
(ji/validate-jwt
jwt-secret jwt-leeway no-val-opts)
ji/validate-jwt-account
ai/get-accounts)
:route-name :lrsql.admin.account/get]
(oa/annotate-short
["/admin/account" :get (conj common-interceptors
(ji/validate-jwt
jwt-secret jwt-leeway no-val-opts)
ji/validate-jwt-account
ai/get-accounts)
:route-name :lrsql.admin.account/get]
{:description "Get all accounts"
:operationId :get-admin-accounts
:security [{:bearerAuth []}]
:responses {200 (oa/response "Array of account objects"
(oa/awrap (oa/owrap {:account-id {:type "string"}
:username {:type "string"}})))
401 oa/error-401}})
;; Get my accounts
["/admin/me" :get (conj common-interceptors
(ji/validate-jwt
jwt-secret jwt-leeway no-val-opts)
ji/validate-jwt-account
ai/me)
:route-name :lrsql.admin.me/get]
(oa/annotate-short
["/admin/me" :get (conj common-interceptors
(ji/validate-jwt
jwt-secret jwt-leeway no-val-opts)
ji/validate-jwt-account
ai/me)
:route-name :lrsql.admin.me/get]
{:description "Get account of querying account"
:operationId :get-own-account
:security [{:bearerAuth []}]
:responses {200 (oa/response "Account object referring to own account"
(oa/owrap {:account-id {:type "string"}
:username {:type "string"}}) )
401 oa/error-401}})
;; Delete account (and associated credentials)
["/admin/account" :delete (conj common-interceptors
ai/validate-delete-params
(ji/validate-jwt
jwt-secret jwt-leeway no-val-opts)
ji/validate-jwt-account
ai/delete-admin)
:route-name :lrsql.admin.account/delete]})
(oa/annotate-short
["/admin/account" :delete (conj common-interceptors
ai/validate-delete-params
(ji/validate-jwt
jwt-secret jwt-leeway no-val-opts)
ji/validate-jwt-account
ai/delete-admin)
:route-name :lrsql.admin.account/delete]
{:description "Delete account (and associated credentials)"
:requestBody (oa/json-content (oa/owrap {:account-id {:type "string"}}))
:operationId :delete-admin-account
:security [{:bearerAuth []}]
:responses {200 (oa/response "ID of deleted account"
(oa/owrap {:account-id {:type "string"}}))
400 oa/error-400
401 oa/error-401}})})

(defn admin-cred-routes
[common-interceptors jwt-secret jwt-leeway no-val-opts]
#{;; Create new API key pair w/ scope set
["/admin/creds" :post (conj common-interceptors
(ci/validate-params {:scopes? true})
(oa/annotate-short
["/admin/creds" :post (conj common-interceptors
(ci/validate-params {:scopes? true})
(ji/validate-jwt
jwt-secret jwt-leeway no-val-opts)
ji/validate-jwt-account
ci/create-api-keys)
:route-name :lrsql.admin.creds/put]
{:description "Create new API key pair w/scope set"
:requestBody (oa/json-content (oa/ref :Scopes))
:operationId :create-api-keys
:security [{:bearerAuth []}]
:responses {400 oa/error-400
401 oa/error-401
200 (oa/response "Object containing key, secret key, and array of scopes"
(oa/ref :ScopedKeyPair))}})
;; Create or update new keys w/ scope set
(oa/annotate-short
["/admin/creds" :put (conj common-interceptors
(ci/validate-params {:key-pair? true
:scopes? true})
(ji/validate-jwt
jwt-secret jwt-leeway no-val-opts)
ji/validate-jwt-account
ci/create-api-keys)
:route-name :lrsql.admin.creds/put]
;; Create or update new keys w/ scope set
["/admin/creds" :put (conj common-interceptors
(ci/validate-params {:key-pair? true
:scopes? true})
(ji/validate-jwt
jwt-secret jwt-leeway no-val-opts)
ji/validate-jwt-account
ci/update-api-keys)
:route-name :lrsql.admin.creds/post]
ci/update-api-keys)
:route-name :lrsql.admin.creds/post]
{:description "Create or update new keys w/scope set"
:requestBody (oa/json-content (oa/ref :ScopedKeyPair))
:operationId :update-api-keys
:security [{:bearerAuth []}]
:responses {400 oa/error-400
401 oa/error-401
200 (oa/response "Key, secret key, and scopes of updated account"
(oa/ref :ScopedKeyPair))}})
;; Get current keys + scopes associated w/ account
["/admin/creds" :get (conj common-interceptors
(ji/validate-jwt
jwt-secret jwt-leeway no-val-opts)
ji/validate-jwt-account
ci/read-api-keys)
:route-name :lrsql.admin.creds/get]
(oa/annotate-short
["/admin/creds" :get (conj common-interceptors
(ji/validate-jwt
jwt-secret jwt-leeway no-val-opts)
ji/validate-jwt-account
ci/read-api-keys)
:route-name :lrsql.admin.creds/get]
{:description "Get current keys + scopes associated w/account"
:operationId :get-api-keys
:security [{:bearerAuth []}]
:responses {200 (oa/response "Array of scoped key pairs"
(oa/awrap (oa/ref :ScopedKeyPair)))
401 oa/error-401}})
;; Delete API key pair and associated scopes
["/admin/creds" :delete (conj common-interceptors
(ci/validate-params {:key-pair? true})
(ji/validate-jwt
jwt-secret jwt-leeway no-val-opts)
ji/validate-jwt-account
ci/delete-api-keys)
:route-name :lrsql.admin.creds/delete]})
(oa/annotate-short
["/admin/creds" :delete (conj common-interceptors
(ci/validate-params {:key-pair? true})
(ji/validate-jwt
jwt-secret jwt-leeway no-val-opts)
ji/validate-jwt-account
ci/delete-api-keys)
:route-name :lrsql.admin.creds/delete]
{:description "Delete API key pair and associated scopes"
:requestBody (oa/json-content (oa/ref :KeyPair))
:operationId :delete-api-key
:security [{:bearerAuth []}]
:responses {200 (oa/response "Empty body" {})
400 oa/error-400
401 oa/error-401}})})

(defn admin-status-routes
[common-interceptors jwt-secret jwt-leeway no-val-opts]
Expand Down Expand Up @@ -135,7 +223,11 @@
(ji/validate-jwt jwt-secret jwt-leeway no-val-opts)
ji/validate-jwt-account
lm/delete-actor)
:route-name :lrsql.lrs-management/delete-actor]})
:route-name :lrsql.lrs-management/delete-actor]
["/admin/openapi" :get (conj common-interceptors
(ji/validate-jwt jwt-secret jwt-leeway no-val-opts)
ji/validate-jwt-account
lm/openapi)]})

(defn add-admin-routes
"Given a set of routes `routes` for a default LRS implementation,
Expand Down
Loading

0 comments on commit 6d6c52d

Please sign in to comment.