changing from commands path to command

moving function checkArrayContainsString
This commit is contained in:
Tobias Lindberg
2021-03-11 22:59:42 +01:00
parent 77a63a5ac7
commit 97ae4f6cbb
2 changed files with 13 additions and 12 deletions
+1 -10
View File
@@ -63,8 +63,8 @@ func TeslaMateAPICarsCommandV1(c *gin.Context) {
// it would be nice to find a way to retrieve api.Group
command = (c.Request.RequestURI[len("/api/v1/cars/"+ParamCarID):])
if !contains(allowList, command) {
log.Print("[warning] TeslaMateAPICarsCommand command: " + command + " not allowed")
if !checkArrayContainsString(allowList, command) {
c.JSON(http.StatusUnauthorized, gin.H{"error": "unauthroized"})
return
}
@@ -141,15 +141,6 @@ func TeslaMateAPICarsCommandV1(c *gin.Context) {
c.JSON(resp.StatusCode, jsonData)
}
func contains(s []string, e string) bool {
for _, a := range s {
if a == e {
return true
}
}
return false
}
func getCommandToken() string {
// get token from environment variable COMMAND_TOKEN
token := getEnv("COMMAND_TOKEN", "")
+12 -2
View File
@@ -80,8 +80,8 @@ func main() {
v1.GET("/cars/:CarID/charges/:ChargeID", TeslaMateAPICarsChargesDetailsV1)
// v1 /api/v1/cars/:CarID/command endpoints
v1.GET("/cars/:CarID/commands", TeslaMateAPICarsCommandV1)
v1.POST("/cars/:CarID/commands/:Command", TeslaMateAPICarsCommandV1)
v1.GET("/cars/:CarID/command", TeslaMateAPICarsCommandV1)
v1.POST("/cars/:CarID/command/:Command", TeslaMateAPICarsCommandV1)
// v1 /api/v1/cars/:CarID/drives endpoints
v1.GET("/cars/:CarID/drives", TeslaMateAPICarsDrivesV1)
@@ -303,3 +303,13 @@ func fahrenheitToCelsiusNilSupport(f NullFloat64) NullFloat64 {
f.Float64 = ((f.Float64 - 32) * 5 / 9)
return (f)
}
// checkArrayContainsString func - check if string is inside stringarray
func checkArrayContainsString(s []string, e string) bool {
for _, a := range s {
if a == e {
return true
}
}
return false
}