Skip to content

Commit 40d3b67

Browse files
fix
1 parent ca5979d commit 40d3b67

File tree

1 file changed

+38
-37
lines changed

1 file changed

+38
-37
lines changed

middlewares/auth.go

Lines changed: 38 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -116,43 +116,6 @@ func Authenticate(c *fiber.Ctx) error {
116116
"message": "Unauthorized",
117117
})
118118
}
119-
// for backward compatability
120-
if strings.HasSuffix(path, "/produce/single") || strings.HasSuffix(path, "/produce/batch") {
121-
if user.Username == "" {
122-
accountId := 1
123-
conn, err := handlers.Connect(configuration.ROOT_PASSWORD, configuration.ROOT_USER, configuration.CONNECTION_TOKEN, accountId)
124-
if err != nil {
125-
errMsg := strings.ToLower(err.Error())
126-
if strings.Contains(errMsg, handlers.ErrorMsgAuthorizationViolation) || strings.Contains(errMsg, "token") || strings.Contains(errMsg, handlers.ErrorMsgMissionAccountId) {
127-
log.Warnf("Authentication error")
128-
return c.Status(401).JSON(fiber.Map{
129-
"message": "Unauthorized",
130-
})
131-
}
132-
}
133-
if handlers.ConnectionsCache["1"] == nil {
134-
handlers.ConnectionsCacheLock.Lock()
135-
handlers.ConnectionsCache["1"] = make(map[string]handlers.Connection)
136-
handlers.ConnectionsCacheLock.Unlock()
137-
}
138-
139-
handlers.ConnectionsCache["1"][configuration.ROOT_USER] = handlers.Connection{Connection: conn, ExpirationTime: int64(user.TokenExpiryMins)}
140-
141-
if !configuration.USER_PASS_BASED_AUTH {
142-
user = models.AuthSchema{
143-
Username: configuration.ROOT_USER,
144-
ConnectionToken: configuration.CONNECTION_TOKEN,
145-
AccountId: 1,
146-
}
147-
} else {
148-
user = models.AuthSchema{
149-
Username: configuration.ROOT_USER,
150-
Password: configuration.ROOT_PASSWORD,
151-
AccountId: 1,
152-
}
153-
}
154-
}
155-
}
156119

157120
} else if path == "/auth/refreshtoken" {
158121
var body models.RefreshTokenSchema
@@ -190,6 +153,44 @@ func Authenticate(c *fiber.Ctx) error {
190153
user.AccountId = 1
191154
}
192155

156+
// for backward compatability
157+
if strings.HasSuffix(path, "/produce/single") || strings.HasSuffix(path, "/produce/batch") || path == "/auth/refreshtoken" {
158+
if user.Username == "" {
159+
accountId := 1
160+
conn, err := handlers.Connect(configuration.ROOT_PASSWORD, configuration.ROOT_USER, configuration.CONNECTION_TOKEN, accountId)
161+
if err != nil {
162+
errMsg := strings.ToLower(err.Error())
163+
if strings.Contains(errMsg, handlers.ErrorMsgAuthorizationViolation) || strings.Contains(errMsg, "token") || strings.Contains(errMsg, handlers.ErrorMsgMissionAccountId) {
164+
log.Warnf("Authentication error")
165+
return c.Status(401).JSON(fiber.Map{
166+
"message": "Unauthorized",
167+
})
168+
}
169+
}
170+
if handlers.ConnectionsCache["1"] == nil {
171+
handlers.ConnectionsCacheLock.Lock()
172+
handlers.ConnectionsCache["1"] = make(map[string]handlers.Connection)
173+
handlers.ConnectionsCacheLock.Unlock()
174+
}
175+
176+
handlers.ConnectionsCache["1"][configuration.ROOT_USER] = handlers.Connection{Connection: conn, ExpirationTime: int64(user.TokenExpiryMins)}
177+
178+
if !configuration.USER_PASS_BASED_AUTH {
179+
user = models.AuthSchema{
180+
Username: configuration.ROOT_USER,
181+
ConnectionToken: configuration.CONNECTION_TOKEN,
182+
AccountId: 1,
183+
}
184+
} else {
185+
user = models.AuthSchema{
186+
Username: configuration.ROOT_USER,
187+
Password: configuration.ROOT_PASSWORD,
188+
AccountId: 1,
189+
}
190+
}
191+
}
192+
}
193+
193194
c.Locals("userData", user)
194195
return c.Next()
195196
}

0 commit comments

Comments
 (0)