Skip to content

Commit edacd7e

Browse files
authored
add header auth (#52)
1 parent bbae546 commit edacd7e

2 files changed

Lines changed: 21 additions & 1 deletion

File tree

.vscode/launch.json

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,12 @@
1616
"type": "go",
1717
"request": "launch",
1818
"mode": "debug",
19-
"program": "server/run.go"
19+
"program": "server/run.go",
20+
"env": {
21+
"DATABASE_URL": "mongodb://localhost:27017/tppVerifier",
22+
"AUTH_HEADER_NAME": "X-RapidAPI-Proxy-Secret",
23+
"AUTH_HEADER_VALUE": "secret"
24+
}
2025
},
2126
{
2227
"name": "Launch Package",

app/run.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package app
22

33
import (
44
"net/http"
5+
"os"
56

67
"github.com/gin-gonic/gin"
78

@@ -14,6 +15,20 @@ type HttpClient interface {
1415

1516
func SetupRouter(vs *verify.VerifySvc) *gin.Engine {
1617
r := gin.Default()
18+
headerName := os.Getenv("AUTH_HEADER_NAME")
19+
headerValue := os.Getenv("AUTH_HEADER_VALUE")
20+
if headerName == "" || headerValue == "" {
21+
panic("AUTH_HEADER_NAME and AUTH_HEADER_VALUE must be set")
22+
}
23+
24+
r.Use(func(c *gin.Context) {
25+
if c.GetHeader(headerName) != headerValue {
26+
c.AbortWithStatusJSON(http.StatusForbidden, gin.H{"error": "Invalid or missing header"})
27+
return
28+
}
29+
c.Next()
30+
})
31+
1732
r.POST("/tpp/verify", vs.Verify)
1833
r.GET("/health", func(c *gin.Context) {
1934
c.JSON(http.StatusOK, gin.H{"status": "ok"})

0 commit comments

Comments
 (0)