Skip to content

Commit e8dc203

Browse files
authored
Merge pull request #119 from modulr-software/feat/api-key-implementation
api key implementation
2 parents 18e3207 + 1aa9afc commit e8dc203

File tree

5 files changed

+48
-6
lines changed

5 files changed

+48
-6
lines changed

src/source/middleware/auth/core.clj

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -57,9 +57,26 @@
5757
(assoc :bundle-id id)
5858
(handler))
5959
(->
60-
(res/response {:message "The bundle you are looking for does not exist"})
60+
(res/response {:message "The bundle you are looking for does not exist."})
6161
(res/status 404))))))
6262

63+
(defn wrap-auth-api-key
64+
"validates the api key from the Authorization header for unauthenticated
65+
users and attaches the bundle-id to the request"
66+
[handler]
67+
(fn [request]
68+
(let [ds (db.util/conn :master)
69+
{:keys [bundle-id user-id]} (validate-request request)
70+
existing-bundle (bundles/bundle ds {:where [:and
71+
[:= :id bundle-id]
72+
[:= :user-id user-id]]})]
73+
(if (some? existing-bundle)
74+
(-> request
75+
(assoc :bundle-id bundle-id)
76+
(handler))
77+
(-> (res/response {:message "The bundle you are looking for does not exist."})
78+
(res/status 404))))))
79+
6380
(comment
6481
(let [authed-request {:headers {"Authorization"
6582
(str
@@ -115,8 +132,8 @@
115132
(:bundle-id))))
116133
(println "tests passed")
117134
(db/delete! ds
118-
{:tname :bundles
119-
:where [:like :name "test-bundle-%"]
120-
:ret :*}))
135+
{:tname :bundles
136+
:where [:like :name "test-bundle-%"]
137+
:ret :*}))
121138

122139
())

src/source/middleware/core.clj

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
(assoc :store store)
2424
(handler))))
2525

26-
(defn wrap-js
26+
(defn wrap-js
2727
"attaches the provided job service to the handler's request"
2828
[handler js]
2929
(fn [request]
@@ -39,7 +39,7 @@
3939
(-> app
4040
(wrap-store store)))
4141

42-
(defn apply-js
42+
(defn apply-js
4343
"middleware for attaching the job service to the request"
4444
[app js]
4545
(-> app
@@ -96,3 +96,7 @@
9696
(defn apply-bundle [app]
9797
(-> app
9898
(auth/wrap-bundle-id)))
99+
100+
(defn apply-api-key [app]
101+
(-> app
102+
(auth/wrap-auth-api-key)))

src/source/middleware/interface.clj

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,6 @@
1111

1212
(defn apply-bundle [app]
1313
(mw/apply-bundle app))
14+
15+
(defn apply-api-key [app]
16+
(mw/apply-api-key app))
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
(ns source.routes.integration-key
2+
(:require [source.middleware.auth.util :as auth.util]
3+
[ring.util.response :as res]))
4+
5+
(defn post
6+
{:summary "generate an API key for the integration with the given id"
7+
:parameters {:path [:map [:id {:title "id"
8+
:description "integration id"} :int]]}
9+
:responses {200 {:body [:map [:key :string]]}
10+
401 {:body [:map [:message :string]]}
11+
403 {:body [:map [:message :string]]}}}
12+
13+
[{:keys [user path-params] :as _request}]
14+
(let [api-key (auth.util/sign-jwt {:user-id (:id user)
15+
:bundle-id (:id path-params)})]
16+
(res/response {:key api-key})))

src/source/routes/reitit.clj

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
[source.routes.feed-categories :as feed-categories]
4141
[source.routes.integrations :as integrations]
4242
[source.routes.integration :as integration]
43+
[source.routes.integration-key :as integration-key]
4344
[source.routes.integration-categories :as integration-categories]
4445
[source.routes.bundle :as bundle]
4546
[source.routes.bundle-feeds :as bundle-feeds]
@@ -171,6 +172,7 @@
171172
["/:id"
172173
["" (route {:get integration/get
173174
:post integration/post})]
175+
["/key" (route {:post integration-key/post})]
174176
["/categories" (route {:get integration-categories/get
175177
:post integration-categories/post})]]]
176178

0 commit comments

Comments
 (0)