Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ More detailed documentation of every endpoint will come..

### Authentication

If you want to use command or logging endpoints such as `/api/v1/cars/:CarID/command/:Command`, `/api/v1/cars/:CarID/wake_up`, or `/api/v1/cars/:CarID/logging/:Command` you need to add authentication to your request.
All endpoints that interact with TeslaMate require authentication.

You need to specify a token yourself (called **API_TOKEN**) in the environment variables file, to set it. The token has the requirement to be a minimum of 32 characters long.

Expand Down
9 changes: 9 additions & 0 deletions src/v1_TeslaMateAPICars.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package main

import (
"net/http"

"github.com/gin-gonic/gin"
_ "github.com/lib/pq"
)
Expand Down Expand Up @@ -75,6 +77,13 @@ func TeslaMateAPICarsV1(c *gin.Context) {
// creating required vars
var CarsData []Cars

// authentication for the endpoint
validToken, errorMessage := validateAuthToken(c)
if !validToken {
TeslaMateAPIHandleOtherResponse(c, http.StatusUnauthorized, "TeslaMateAPICarsV1", gin.H{"error": errorMessage})
return
}

// getting data from database
query := `
SELECT
Expand Down
10 changes: 10 additions & 0 deletions src/v1_TeslaMateAPICarsBatteryHealth.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,23 @@
package main

import (
"net/http"

"github.com/gin-gonic/gin"
_ "github.com/lib/pq"
)

// TeslaMateAPICarsBatteryHealthV1 func
func TeslaMateAPICarsBatteryHealthV1(c *gin.Context) {
var CarsBatteryHealthError1 = "Unable to load battery health data."

// authentication for the endpoint
validToken, errorMessage := validateAuthToken(c)
if !validToken {
TeslaMateAPIHandleOtherResponse(c, http.StatusUnauthorized, "TeslaMateAPICarsBatteryHealthV1", gin.H{"error": errorMessage})
return
}

CarID := convertStringToInteger(c.Param("CarID"))

// creating structs for /cars/<CarID>/battery-health
Expand Down
8 changes: 8 additions & 0 deletions src/v1_TeslaMateAPICarsCharges.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,26 @@

import (
"fmt"
"net/http"

"github.com/gin-gonic/gin"
_ "github.com/lib/pq"
)

// TeslaMateAPICarsChargesV1 func
func TeslaMateAPICarsChargesV1(c *gin.Context) {

Check failure on line 12 in src/v1_TeslaMateAPICarsCharges.go

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Refactor this method to reduce its Cognitive Complexity from 16 to the 15 allowed.

See more on https://sonarcloud.io/project/issues?id=tobiasehlert_teslamateapi&issues=AZ438UwhwWSCt-IK0a64&open=AZ438UwhwWSCt-IK0a64&pullRequest=352

// define error messages
var CarsChargesError1 = "Unable to load charges."
var CarsChargesError2 = "Invalid date format."

// authentication for the endpoint
validToken, errorMessage := validateAuthToken(c)
if !validToken {
TeslaMateAPIHandleOtherResponse(c, http.StatusUnauthorized, "TeslaMateAPICarsChargesV1", gin.H{"error": errorMessage})
return
}

// getting CarID param from URL
CarID := convertStringToInteger(c.Param("CarID"))
// query options to modify query when collecting data
Expand Down
8 changes: 8 additions & 0 deletions src/v1_TeslaMateAPICarsChargesDetails.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package main

import (
"database/sql"
"net/http"

"github.com/gin-gonic/gin"
_ "github.com/lib/pq"
Expand All @@ -16,6 +17,13 @@ func TeslaMateAPICarsChargesDetailsV1(c *gin.Context) {
CarsChargesDetailsError2 = "Unable to load charge details."
)

// authentication for the endpoint
validToken, errorMessage := validateAuthToken(c)
if !validToken {
TeslaMateAPIHandleOtherResponse(c, http.StatusUnauthorized, "TeslaMateAPICarsChargesDetailsV1", gin.H{"error": errorMessage})
return
}

// getting CarID and ChargeID param from URL
CarID := convertStringToInteger(c.Param("CarID"))
ChargeID := convertStringToInteger(c.Param("ChargeID"))
Expand Down
8 changes: 8 additions & 0 deletions src/v1_TeslaMateAPICarsDrives.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package main

import (
"fmt"
"net/http"

"github.com/gin-gonic/gin"
_ "github.com/lib/pq"
Expand All @@ -14,6 +15,13 @@ func TeslaMateAPICarsDrivesV1(c *gin.Context) {
var CarsDrivesError1 = "Unable to load drives."
var CarsDrivesError2 = "Invalid date format."

// authentication for the endpoint
validToken, errorMessage := validateAuthToken(c)
if !validToken {
TeslaMateAPIHandleOtherResponse(c, http.StatusUnauthorized, "TeslaMateAPICarsDrivesV1", gin.H{"error": errorMessage})
return
}

// getting CarID param from URL
CarID := convertStringToInteger(c.Param("CarID"))
// query options to modify query when collecting data
Expand Down
8 changes: 8 additions & 0 deletions src/v1_TeslaMateAPICarsDrivesDetails.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package main

import (
"database/sql"
"net/http"

"github.com/gin-gonic/gin"
_ "github.com/lib/pq"
Expand All @@ -16,6 +17,13 @@ func TeslaMateAPICarsDrivesDetailsV1(c *gin.Context) {
CarsDrivesDetailsError2 = "Unable to load drive details."
)

// authentication for the endpoint
validToken, errorMessage := validateAuthToken(c)
if !validToken {
TeslaMateAPIHandleOtherResponse(c, http.StatusUnauthorized, "TeslaMateAPICarsDrivesDetailsV1", gin.H{"error": errorMessage})
return
}

// getting CarID and DriveID param from URL
CarID := convertStringToInteger(c.Param("CarID"))
DriveID := convertStringToInteger(c.Param("DriveID"))
Expand Down
14 changes: 7 additions & 7 deletions src/v1_TeslaMateAPICarsLogging.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,13 @@ func TeslaMateAPICarsLoggingV1(c *gin.Context) {
err error
)

// authentication for the endpoint
validToken, errorMessage := validateAuthToken(c)
if !validToken {
TeslaMateAPIHandleOtherResponse(c, http.StatusUnauthorized, "TeslaMateAPICarsLoggingV1", gin.H{"error": errorMessage})
return
}

// check if commands are enabled.. if not we need to abort
if !getEnvAsBool("ENABLE_COMMANDS", false) {
log.Println("[warning] TeslaMateAPICarsLoggingV1 ENABLE_COMMANDS is not true.. returning 403 forbidden.")
Expand All @@ -34,13 +41,6 @@ func TeslaMateAPICarsLoggingV1(c *gin.Context) {
return
}

// authentication for the endpoint
validToken, errorMessage := validateAuthToken(c)
if !validToken {
TeslaMateAPIHandleOtherResponse(c, http.StatusUnauthorized, "TeslaMateAPICarsLoggingV1", gin.H{"error": errorMessage})
return
}

// getting CarID param from URL and validating that it's not zero
CarID := convertStringToInteger(c.Param("CarID"))
if CarID == 0 {
Expand Down
9 changes: 9 additions & 0 deletions src/v1_TeslaMateAPICarsUpdates.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package main

import (
"net/http"

"github.com/gin-gonic/gin"
_ "github.com/lib/pq"
)
Expand All @@ -11,6 +13,13 @@ func TeslaMateAPICarsUpdatesV1(c *gin.Context) {
// define error messages
var CarsUpdatesError1 = "Unable to load updates."

// authentication for the endpoint
validToken, errorMessage := validateAuthToken(c)
if !validToken {
TeslaMateAPIHandleOtherResponse(c, http.StatusUnauthorized, "TeslaMateAPICarsUpdatesV1", gin.H{"error": errorMessage})
return
}

// getting CarID param from URL
CarID := convertStringToInteger(c.Param("CarID"))
// query options to modify query when collecting data
Expand Down
8 changes: 8 additions & 0 deletions src/v1_TeslaMateAPIGlobalsettings.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package main

import (
"database/sql"
"net/http"

"github.com/gin-gonic/gin"
_ "github.com/lib/pq"
Expand Down Expand Up @@ -54,6 +55,13 @@ func TeslaMateAPIGlobalsettingsV1(c *gin.Context) {
// creating required vars
var globalSetting GlobalSettings

// authentication for the endpoint
validToken, errorMessage := validateAuthToken(c)
if !validToken {
TeslaMateAPIHandleOtherResponse(c, http.StatusUnauthorized, "TeslaMateAPIGlobalsettingsV1", gin.H{"error": errorMessage})
return
}

// getting data from database
query := `
SELECT
Expand Down
Loading