Browse Source

Extract some messages to constants

This is my first attempt at contribution, and aimed at reducing number
of code smells that had been identified by SonarCube
Adam Hepner 3 months ago
parent
commit
216aa91456
1 changed files with 11 additions and 8 deletions
  1. 11
    8
      api/handlers/auth.go

+ 11
- 8
api/handlers/auth.go View File

@@ -15,6 +15,9 @@ import (
15 15
 	log "github.com/sirupsen/logrus"
16 16
 )
17 17
 
18
+const unsupportedMethod string = "Unsupported Method"
19
+const errorParsingRequestJSON string = "Error parsing request JSON"
20
+
18 21
 // RequestAuthSession represents an HTTP handler capable of processing /admin/requestAuthSession requests.
19 22
 type RequestAuthSession struct {
20 23
 	Db *database.ServiceDB
@@ -43,11 +46,11 @@ type RequestAuthSession struct {
43 46
 func (h *RequestAuthSession) OnIncomingRequest(req *http.Request) util.JSONResponse {
44 47
 	logger := util.GetLogger(req.Context())
45 48
 	if req.Method != "POST" {
46
-		return util.MessageResponse(405, "Unsupported Method")
49
+		return util.MessageResponse(405, unsupportedMethod)
47 50
 	}
48 51
 	var body api.RequestAuthSessionRequest
49 52
 	if err := json.NewDecoder(req.Body).Decode(&body); err != nil {
50
-		return util.MessageResponse(400, "Error parsing request JSON")
53
+		return util.MessageResponse(400, errorParsingResponseJson)
51 54
 	}
52 55
 	logger.WithFields(log.Fields{
53 56
 		"realm_id": body.RealmID,
@@ -100,14 +103,14 @@ type RemoveAuthSession struct {
100 103
 func (h *RemoveAuthSession) OnIncomingRequest(req *http.Request) util.JSONResponse {
101 104
 	logger := util.GetLogger(req.Context())
102 105
 	if req.Method != "POST" {
103
-		return util.MessageResponse(405, "Unsupported Method")
106
+		return util.MessageResponse(405, unsupportedMethod)
104 107
 	}
105 108
 	var body struct {
106 109
 		RealmID string
107 110
 		UserID  string
108 111
 	}
109 112
 	if err := json.NewDecoder(req.Body).Decode(&body); err != nil {
110
-		return util.MessageResponse(400, "Error parsing request JSON")
113
+		return util.MessageResponse(400, errorParsingRequestJSON)
111 114
 	}
112 115
 	logger.WithFields(log.Fields{
113 116
 		"realm_id": body.RealmID,
@@ -201,11 +204,11 @@ type ConfigureAuthRealm struct {
201 204
 func (h *ConfigureAuthRealm) OnIncomingRequest(req *http.Request) util.JSONResponse {
202 205
 	logger := util.GetLogger(req.Context())
203 206
 	if req.Method != "POST" {
204
-		return util.MessageResponse(405, "Unsupported Method")
207
+		return util.MessageResponse(405, unsupportedMethod)
205 208
 	}
206 209
 	var body api.ConfigureAuthRealmRequest
207 210
 	if err := json.NewDecoder(req.Body).Decode(&body); err != nil {
208
-		return util.MessageResponse(400, "Error parsing request JSON")
211
+		return util.MessageResponse(400, errorParsingRequestJSON)
209 212
 	}
210 213
 
211 214
 	if err := body.Check(); err != nil {
@@ -272,14 +275,14 @@ type GetSession struct {
272 275
 func (h *GetSession) OnIncomingRequest(req *http.Request) util.JSONResponse {
273 276
 	logger := util.GetLogger(req.Context())
274 277
 	if req.Method != "POST" {
275
-		return util.MessageResponse(405, "Unsupported Method")
278
+		return util.MessageResponse(405, unsupportedMethod)
276 279
 	}
277 280
 	var body struct {
278 281
 		RealmID string
279 282
 		UserID  string
280 283
 	}
281 284
 	if err := json.NewDecoder(req.Body).Decode(&body); err != nil {
282
-		return util.MessageResponse(400, "Error parsing request JSON")
285
+		return util.MessageResponse(400, errorParsingRequestJSON)
283 286
 	}
284 287
 
285 288
 	if body.RealmID == "" || body.UserID == "" {

Loading…
Cancel
Save