Custom api key security and added env file : GWM

This commit is contained in:
Gowtham M 2025-08-20 11:59:35 +05:30
parent ef8e67a5be
commit e61b684759
5 changed files with 179 additions and 6 deletions

3
.env Normal file
View File

@ -0,0 +1,3 @@
APP_SIGNATURE = super-secret-key
BASE_URL = https://pb.venbait.in

1
go.mod
View File

@ -60,6 +60,7 @@ require (
github.com/googleapis/gax-go/v2 v2.14.1 // indirect
github.com/hashicorp/golang-lru/v2 v2.0.7 // indirect
github.com/inconshreveable/mousetrap v1.1.0 // indirect
github.com/joho/godotenv v1.5.1 // indirect
github.com/kballard/go-shellquote v0.0.0-20180428030007-95032a82bc51 // indirect
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-isatty v0.0.20 // indirect

2
go.sum
View File

@ -161,6 +161,8 @@ github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2
github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw=
github.com/jmespath/go-jmespath v0.4.0 h1:BEgLn5cpjn8UN1mAw4NjwDrS35OdebyEtFe+9YPoQUg=
github.com/jmespath/go-jmespath v0.4.0/go.mod h1:T8mJZnbsbmF+m6zOOFylbeCJqk5+pHWvzYPziyZiYoo=
github.com/joho/godotenv v1.5.1 h1:7eLL/+HRGLY0ldzfGMeQkb7vMd0as4CfYvUVzLqw0N0=
github.com/joho/godotenv v1.5.1/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4=
github.com/kballard/go-shellquote v0.0.0-20180428030007-95032a82bc51 h1:Z9n2FFNUXsshfwJMBgNA0RU6/i7WVaAegv3PtuIHPMs=
github.com/kballard/go-shellquote v0.0.0-20180428030007-95032a82bc51/go.mod h1:CzGEWj7cYgsdH8dAjBGEr58BoE7ScuLd+fwFZ44+/x8=
github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE=

179
main.go
View File

@ -20,6 +20,7 @@ import (
firebase "firebase.google.com/go"
"firebase.google.com/go/messaging"
"github.com/joho/godotenv"
"github.com/labstack/echo/v5"
"github.com/pocketbase/dbx"
"github.com/pocketbase/pocketbase"
@ -34,7 +35,7 @@ import (
"google.golang.org/api/option"
)
const baseUrl = "https://pb.venbait.in"
var baseUrl = os.Getenv("BASE_URL")
type EmailConfigurationResponse struct {
Page int `json:"page"`
@ -463,7 +464,14 @@ func generateToken() string {
var app *pocketbase.PocketBase
func main() {
const baseUrl = "https://pb.venbait.in"
// load .env file
err := godotenv.Load()
if err != nil {
log.Println("No .env file found")
}
var baseUrl = os.Getenv("BASE_URL")
app = pocketbase.New()
@ -584,6 +592,12 @@ func main() {
})
e.Router.GET("/api/auth/request-password-reset", func(c echo.Context) error {
apiKey := c.Request().Header.Get("APP_SIGNATURE")
if apiKey != os.Getenv("APP_SIGNATURE") {
return c.JSON(http.StatusForbidden, map[string]string{"error": "Invalid API Key"})
}
email := c.QueryParam("email")
if email == "" {
return c.JSON(http.StatusBadRequest, map[string]string{"error": "Email is required"})
@ -710,6 +724,12 @@ func main() {
})
e.Router.GET("/api/auth/reset-password", func(c echo.Context) error {
apiKey := c.Request().Header.Get("APP_SIGNATURE")
if apiKey != os.Getenv("APP_SIGNATURE") {
return c.JSON(http.StatusForbidden, map[string]string{"error": "Invalid API Key"})
}
token := c.QueryParam("token")
if token == "" {
return c.JSON(http.StatusBadRequest, map[string]string{"error": "Token is required"})
@ -738,6 +758,11 @@ func main() {
e.Router.GET("/api/login_success", func(c echo.Context) error {
apiKey := c.Request().Header.Get("APP_SIGNATURE")
if apiKey != os.Getenv("APP_SIGNATURE") {
return c.JSON(http.StatusForbidden, map[string]string{"error": "Invalid API Key"})
}
userID := c.QueryParam("id")
if userID == "" {
return c.JSON(http.StatusBadRequest, map[string]string{"error": "User ID is required"})
@ -778,6 +803,11 @@ func main() {
e.Router.GET("/api/getNotification", func(c echo.Context) error {
apiKey := c.Request().Header.Get("APP_SIGNATURE")
if apiKey != os.Getenv("APP_SIGNATURE") {
return c.JSON(http.StatusForbidden, map[string]string{"error": "Invalid API Key"})
}
language := c.QueryParam("language")
if language == "" {
return c.JSON(http.StatusBadRequest, map[string]string{"error": "language is required"})
@ -837,6 +867,12 @@ func main() {
})
e.Router.GET("/api/removeNotification", func(c echo.Context) error {
apiKey := c.Request().Header.Get("APP_SIGNATURE")
if apiKey != os.Getenv("APP_SIGNATURE") {
return c.JSON(http.StatusForbidden, map[string]string{"error": "Invalid API Key"})
}
userID := c.QueryParam("user_id")
notificationID := c.QueryParam("notification_id")
@ -890,6 +926,11 @@ func main() {
e.Router.GET("/api/pushNotification", func(c echo.Context) error {
apiKey := c.Request().Header.Get("APP_SIGNATURE")
if apiKey != os.Getenv("APP_SIGNATURE") {
return c.JSON(http.StatusForbidden, map[string]string{"error": "Invalid API Key"})
}
// type Request struct {
// DeviceToken string `json:"device_token"`
// }
@ -943,6 +984,11 @@ func main() {
e.Router.POST("/api/getDataSet", func(c echo.Context) error {
apiKey := c.Request().Header.Get("APP_SIGNATURE")
if apiKey != os.Getenv("APP_SIGNATURE") {
return c.JSON(http.StatusForbidden, map[string]string{"error": "Invalid API Key"})
}
type RequestData struct {
Dataset string `json:"dataset"`
KPI string `json:"kpi"`
@ -1268,6 +1314,11 @@ func main() {
e.Router.GET("/api/getHomePageData", func(c echo.Context) error {
apiKey := c.Request().Header.Get("APP_SIGNATURE")
if apiKey != os.Getenv("APP_SIGNATURE") {
return c.JSON(http.StatusForbidden, map[string]string{"error": "Invalid API Key"})
}
language := c.QueryParam("language")
colorMode := c.QueryParam("color_mode")
@ -1284,10 +1335,10 @@ func main() {
// Execute the query
query := fmt.Sprintf(`
SELECT
main_topic_en,
main_topic_ar,
main_topic_list_order,
SELECT
main_topic_en,
main_topic_ar,
main_topic_list_order,
%s AS color_pattern
FROM home_screen
GROUP BY main_topic_en, main_topic_ar, main_topic_list_order, %s
@ -1362,8 +1413,119 @@ func main() {
})
// e.Router.GET("/api/getHomePageData", func(c echo.Context) error {
// language := c.QueryParam("language")
// colorMode := c.QueryParam("color_mode")
// var colorPatternColumn string
// if colorMode == "dark" {
// colorPatternColumn = "color_pattern_dark"
// } else {
// colorPatternColumn = "color_pattern_light"
// }
// var languageSpecificColumn string
// if language == "en" {
// languageSpecificColumn = "data_set_tile_heading_en"
// } else {
// languageSpecificColumn = "data_set_tile_heading_ar"
// }
// // Define the struct for holding the query results
// mainTopics := []struct {
// MainTopicEn string `db:"main_topic_en" json:"main_topic_en"`
// MainTopicAr string `db:"main_topic_ar" json:"main_topic_ar"`
// MainTopicListOrder string `db:"main_topic_list_order" json:"main_topic_list_order"`
// ColorPattern string `db:"color_pattern" json:"color_pattern"`
// }{}
// // Execute the query
// query := fmt.Sprintf(`
// SELECT
// main_topic_en,
// main_topic_ar,
// main_topic_list_order,
// %s AS color_pattern
// FROM home_screen
// GROUP BY main_topic_en, main_topic_ar, main_topic_list_order, %s
// ORDER BY main_topic_list_order ASC
// `, colorPatternColumn, colorPatternColumn)
// err := app.DB().NewQuery(query).All(&mainTopics)
// // err := app.DB().
// // Select("main_topic_en", "main_topic_ar", "main_topic_list_order", "color_pattern").
// // From("home_screen").
// // GroupBy("main_topic_en", "main_topic_ar", "color_pattern").
// // OrderBy("main_topic_list_order ASC").
// // All(&mainTopics)
// if err != nil {
// log.Printf("Failed to fetch home_screen data: %v", err)
// return c.JSON(http.StatusInternalServerError, map[string]string{"error": "Failed to fetch records"})
// }
// // If no records are found
// if len(mainTopics) == 0 {
// return c.JSON(http.StatusNotFound, map[string]string{"error": "No records found for home screen"})
// }
// // Final result structure
// result := []map[string]interface{}{}
// // Loop through main topics and fetch sub-topic data for each
// for _, mainTopic := range mainTopics {
// dataSets := []struct {
// DataSet string `db:"data_set" json:"data_set"`
// DataSetTileHeading string `db:"data_set_tile_heading" json:"data_set_tile_heading"`
// ValueSource string `db:"value_source" json:"value_source"`
// Value string `db:"value" json:"value"`
// DataSetListOrder string `db:"data_set_list_order" json:"data_set_list_order"`
// }{}
// query := ` SELECT data_set,` + languageSpecificColumn + ` AS data_set_tile_heading,value_source,value, data_set_list_order FROM home_screen WHERE main_topic_en = {:topic}`
// err := app.DB().
// NewQuery(query).
// Bind(dbx.Params{
// "topic": mainTopic.MainTopicEn,
// }).
// All(&dataSets)
// if err != nil {
// log.Printf("Error fetching data sets: %v", err)
// continue
// }
// var MainTopic string
// if language == "en" {
// MainTopic = mainTopic.MainTopicEn
// } else {
// MainTopic = mainTopic.MainTopicAr
// }
// // Add the main topic and its sub-topics to the result
// result = append(result, map[string]interface{}{
// "main_topic": MainTopic,
// "main_topic_list_order": mainTopic.MainTopicListOrder,
// "color_pattern": mainTopic.ColorPattern,
// "tile_data": dataSets,
// })
// }
// // Send the combined result as JSON
// return c.JSON(http.StatusOK, result)
// })
e.Router.GET("/api/getUAENumbersData", func(c echo.Context) error {
apiKey := c.Request().Header.Get("APP_SIGNATURE")
if apiKey != os.Getenv("APP_SIGNATURE") {
return c.JSON(http.StatusForbidden, map[string]string{"error": "Invalid API Key"})
}
language := c.QueryParam("language")
colorMode := c.QueryParam("color_mode")
@ -1509,6 +1671,11 @@ func main() {
e.Router.GET("/api/getUserBookmark", func(c echo.Context) error {
apiKey := c.Request().Header.Get("APP_SIGNATURE")
if apiKey != os.Getenv("APP_SIGNATURE") {
return c.JSON(http.StatusForbidden, map[string]string{"error": "Invalid API Key"})
}
language := c.QueryParam("language")
user_id := c.QueryParam("user_id")
colorMode := c.QueryParam("color_mode")

Binary file not shown.