Skip to content

Commit 7d55a2c

Browse files
authored
Added token middleware for secured access to account API (#50)
1 parent 4d5c8f1 commit 7d55a2c

File tree

9 files changed

+343
-41
lines changed

9 files changed

+343
-41
lines changed

resources/postman/Switcher GitOps.postman_collection.json

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,16 @@
1212
{
1313
"name": "Create",
1414
"request": {
15+
"auth": {
16+
"type": "bearer",
17+
"bearer": [
18+
{
19+
"key": "token",
20+
"value": "{{gitopsToken}}",
21+
"type": "string"
22+
}
23+
]
24+
},
1525
"method": "POST",
1626
"header": [],
1727
"body": {
@@ -38,6 +48,16 @@
3848
{
3949
"name": "Update",
4050
"request": {
51+
"auth": {
52+
"type": "bearer",
53+
"bearer": [
54+
{
55+
"key": "token",
56+
"value": "{{gitopsToken}}",
57+
"type": "string"
58+
}
59+
]
60+
},
4161
"method": "PUT",
4262
"header": [],
4363
"body": {
@@ -64,6 +84,16 @@
6484
{
6585
"name": "Update (token)",
6686
"request": {
87+
"auth": {
88+
"type": "bearer",
89+
"bearer": [
90+
{
91+
"key": "token",
92+
"value": "{{gitopsToken}}",
93+
"type": "string"
94+
}
95+
]
96+
},
6797
"method": "PUT",
6898
"header": [],
6999
"body": {
@@ -90,6 +120,16 @@
90120
{
91121
"name": "Update (force sync)",
92122
"request": {
123+
"auth": {
124+
"type": "bearer",
125+
"bearer": [
126+
{
127+
"key": "token",
128+
"value": "{{gitopsToken}}",
129+
"type": "string"
130+
}
131+
]
132+
},
93133
"method": "PUT",
94134
"header": [],
95135
"body": {
@@ -119,6 +159,16 @@
119159
"disableBodyPruning": true
120160
},
121161
"request": {
162+
"auth": {
163+
"type": "bearer",
164+
"bearer": [
165+
{
166+
"key": "token",
167+
"value": "{{gitopsToken}}",
168+
"type": "string"
169+
}
170+
]
171+
},
122172
"method": "GET",
123173
"header": [],
124174
"body": {
@@ -146,6 +196,16 @@
146196
{
147197
"name": "Fetch By Domain Id / Env",
148198
"request": {
199+
"auth": {
200+
"type": "bearer",
201+
"bearer": [
202+
{
203+
"key": "token",
204+
"value": "{{gitopsToken}}",
205+
"type": "string"
206+
}
207+
]
208+
},
149209
"method": "GET",
150210
"header": [],
151211
"url": {
@@ -165,6 +225,16 @@
165225
{
166226
"name": "Delete By Domain Id / Env",
167227
"request": {
228+
"auth": {
229+
"type": "bearer",
230+
"bearer": [
231+
{
232+
"key": "token",
233+
"value": "{{gitopsToken}}",
234+
"type": "string"
235+
}
236+
]
237+
},
168238
"method": "DELETE",
169239
"header": [],
170240
"body": {

resources/swagger.yaml

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,11 @@ servers:
1515
description: Local
1616
- url: https://localhost:8000
1717
description: Remote
18+
tags:
19+
- name: API
20+
description: API status & docs
21+
- name: Account API
22+
description: Account management
1823
paths:
1924
/api/check:
2025
get:
@@ -64,6 +69,8 @@ paths:
6469
- Account API
6570
summary: Create a new account
6671
description: Create a new account and starts handler when active
72+
security:
73+
- bearerAuth: []
6774
requestBody:
6875
required: true
6976
content:
@@ -94,6 +101,8 @@ paths:
94101
- Account API
95102
summary: Update an existing account
96103
description: Update an existing account and starts handler when active
104+
security:
105+
- bearerAuth: []
97106
requestBody:
98107
required: true
99108
content:
@@ -125,6 +134,8 @@ paths:
125134
- Account API
126135
summary: Get All accounts by domain ID
127136
description: Get all accounts by domain ID
137+
security:
138+
- bearerAuth: []
128139
parameters:
129140
- name: domainId
130141
in: path
@@ -166,6 +177,8 @@ paths:
166177
- Account API
167178
summary: Get account by domain ID and environment
168179
description: Get account by domain ID and environment
180+
security:
181+
- bearerAuth: []
169182
parameters:
170183
- name: domainId
171184
in: path
@@ -210,6 +223,8 @@ paths:
210223
- Account API
211224
summary: Delete account by domain ID and environment
212225
description: Delete account by domain ID and environment
226+
security:
227+
- bearerAuth: []
213228
parameters:
214229
- name: domainId
215230
in: path
@@ -240,6 +255,11 @@ paths:
240255
schema:
241256
$ref: '#/components/schemas/ErrorResponse'
242257
components:
258+
securitySchemes:
259+
bearerAuth:
260+
type: http
261+
scheme: bearer
262+
bearerFormat: JWT
243263
schemas:
244264
AccountRequest:
245265
type: object

src/controller/account.go

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -31,18 +31,21 @@ func NewAccountController(repo repository.AccountRepository, coreHandler *core.C
3131
}
3232

3333
func (controller *AccountController) RegisterRoutes(r *mux.Router) http.Handler {
34-
r.NewRoute().Path(controller.routeAccountPath).Name("CreateAccount").HandlerFunc(controller.CreateAccountHandler).Methods(http.MethodPost)
35-
r.NewRoute().Path(controller.routeAccountPath).Name("UpdateAccount").HandlerFunc(controller.UpdateAccountHandler).Methods(http.MethodPut)
36-
r.NewRoute().Path(controller.routeAccountPath + "/{domainId}").Name("GelAllAccountsByDomainId").HandlerFunc(controller.FetchAllAccountsByDomainIdHandler).Methods(http.MethodGet)
37-
r.NewRoute().Path(controller.routeAccountPath + "/{domainId}/{enviroment}").Name("GetAccount").HandlerFunc(controller.FetchAccountHandler).Methods(http.MethodGet)
38-
r.NewRoute().Path(controller.routeAccountPath + "/{domainId}/{enviroment}").Name("DeleteAccount").HandlerFunc(controller.DeleteAccountHandler).Methods(http.MethodDelete)
34+
r.NewRoute().Path(controller.routeAccountPath).Name("CreateAccount").Handler(
35+
ValidateToken(http.HandlerFunc(controller.CreateAccountHandler))).Methods(http.MethodPost)
36+
r.NewRoute().Path(controller.routeAccountPath).Name("UpdateAccount").Handler(
37+
ValidateToken(http.HandlerFunc(controller.UpdateAccountHandler))).Methods(http.MethodPut)
38+
r.NewRoute().Path(controller.routeAccountPath + "/{domainId}").Name("GelAllAccountsByDomainId").Handler(
39+
ValidateToken(http.HandlerFunc(controller.FetchAllAccountsByDomainIdHandler))).Methods(http.MethodGet)
40+
r.NewRoute().Path(controller.routeAccountPath + "/{domainId}/{enviroment}").Name("GetAccount").Handler(
41+
ValidateToken(http.HandlerFunc(controller.FetchAccountHandler))).Methods(http.MethodGet)
42+
r.NewRoute().Path(controller.routeAccountPath + "/{domainId}/{enviroment}").Name("DeleteAccount").Handler(
43+
ValidateToken(http.HandlerFunc(controller.DeleteAccountHandler))).Methods(http.MethodDelete)
3944

4045
return r
4146
}
4247

4348
func (controller *AccountController) CreateAccountHandler(w http.ResponseWriter, r *http.Request) {
44-
ConfigureHeaders(w)
45-
4649
var accountRequest model.Account
4750
err := json.NewDecoder(r.Body).Decode(&accountRequest)
4851
if err != nil {
@@ -71,8 +74,6 @@ func (controller *AccountController) CreateAccountHandler(w http.ResponseWriter,
7174
}
7275

7376
func (controller *AccountController) FetchAccountHandler(w http.ResponseWriter, r *http.Request) {
74-
ConfigureHeaders(w)
75-
7677
domainId := mux.Vars(r)["domainId"]
7778
enviroment := mux.Vars(r)["enviroment"]
7879

@@ -88,8 +89,6 @@ func (controller *AccountController) FetchAccountHandler(w http.ResponseWriter,
8889
}
8990

9091
func (controller *AccountController) FetchAllAccountsByDomainIdHandler(w http.ResponseWriter, r *http.Request) {
91-
ConfigureHeaders(w)
92-
9392
domainId := mux.Vars(r)["domainId"]
9493

9594
accounts := controller.accountRepository.FetchAllByDomainId(domainId)
@@ -109,8 +108,6 @@ func (controller *AccountController) FetchAllAccountsByDomainIdHandler(w http.Re
109108
}
110109

111110
func (controller *AccountController) UpdateAccountHandler(w http.ResponseWriter, r *http.Request) {
112-
ConfigureHeaders(w)
113-
114111
var accountRequest model.Account
115112
err := json.NewDecoder(r.Body).Decode(&accountRequest)
116113
if err != nil {
@@ -136,8 +133,6 @@ func (controller *AccountController) UpdateAccountHandler(w http.ResponseWriter,
136133
}
137134

138135
func (controller *AccountController) DeleteAccountHandler(w http.ResponseWriter, r *http.Request) {
139-
ConfigureHeaders(w)
140-
141136
domainId := mux.Vars(r)["domainId"]
142137
enviroment := mux.Vars(r)["enviroment"]
143138

0 commit comments

Comments
 (0)