chore: trackit route and tracking with no auth with ip address

This commit is contained in:
JesusPerez 2022-03-24 12:55:57 +00:00
parent 54d3c18436
commit 08c07fb009
2 changed files with 35 additions and 29 deletions

View File

@ -95,20 +95,21 @@ func post_tracking_handle(c *gin.Context, rtenv *RouteEnv) {
} }
userid := trackpost.Data userid := trackpost.Data
_, okmdl := rtenv.MdlsUsrs[userid] _, okmdl := rtenv.MdlsUsrs[userid]
auth := ""
if !okmdl { if !okmdl {
_, okusr := rtenv.Users.Accounts[userid] _, okusr := rtenv.Users.Accounts[userid]
if !okusr { // if !okusr {
c.JSON(http.StatusUnauthorized, gin.H{"error": "Authentication failed"}) // c.JSON(http.StatusUnauthorized, gin.H{"error": "Authentication failed"})
return // return
// }
if okusr && rtenv.Cfg.UseJWT {
tk, errtk := rtenv.AuthMiddleware.ParseToken(c)
if errtk == nil {
auth = fmt.Sprintf("%v",tk.Raw)
}
} }
} }
auth := "" ip, _ := getIP(c.Request)
if rtenv.Cfg.UseJWT {
tk, errtk := rtenv.AuthMiddleware.ParseToken(c)
if errtk == nil {
auth = fmt.Sprintf("%v",tk.Raw)
}
}
trackaction := &TrackAction{ trackaction := &TrackAction{
When: trackpost.When, When: trackpost.When,
Where: trackpost.Where, Where: trackpost.Where,
@ -116,10 +117,12 @@ func post_tracking_handle(c *gin.Context, rtenv *RouteEnv) {
Context: trackpost.Context, Context: trackpost.Context,
Data: trackpost.Data, Data: trackpost.Data,
Auth: auth, Auth: auth,
Ip: ip,
// Info: requestInfo(c,rtenv,"post_tracking",fmt.Sprintf("post %s",trackpost.Data), fmt.Sprintf("post %s",userid)),
} }
data,err := json.Marshal(trackaction) data,err := json.Marshal(trackaction)
if err != nil { if err != nil {
logRoute(c,rtenv,"post_trackng",fmt.Sprintf("post %s: error %v ",trackpost.Data,err), fmt.Sprintf("post %s",userid)) logRoute(c,rtenv,"post_tracking",fmt.Sprintf("post %s: error %v ",trackpost.Data,err), fmt.Sprintf("post %s",userid))
fmt.Printf("Error Parse tracking %s: %v\n", trackpost.Data, err) fmt.Printf("Error Parse tracking %s: %v\n", trackpost.Data, err)
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to save tracking"}) c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to save tracking"})
return return

View File

@ -56,23 +56,23 @@ func getRoutes(router *gin.Engine, cfg *cfg.Config, users *UsersAccounts, mdlsUs
// router.POST(cfg.Routes["post_login"].Path, routenv.AuthMiddleware.LoginHandler) // router.POST(cfg.Routes["post_login"].Path, routenv.AuthMiddleware.LoginHandler)
authorized = router.Group(cfg.Routes["root"].Path, gin.BasicAuth(webusrs.Accounts)) authorized = router.Group(cfg.Routes["root"].Path, gin.BasicAuth(webusrs.Accounts))
} }
authorized.GET("/hello", func(c *gin.Context) { // authorized.GET("/hello", func(c *gin.Context) {
if cfg.UseJWT { // if cfg.UseJWT {
claims := jwt.ExtractClaims(c) // claims := jwt.ExtractClaims(c)
user, _ := c.Get(cfg.IdentityKey) // user, _ := c.Get(cfg.IdentityKey)
c.JSON(200, gin.H{ // c.JSON(200, gin.H{
"userID": claims[cfg.IdentityKey], // "userID": claims[cfg.IdentityKey],
"userName": user.(*User).UserName, // "userName": user.(*User).UserName,
"text": "Hello World.", // "text": "Hello World.",
}) // })
} else { // } else {
c.JSON(200, gin.H{ // c.JSON(200, gin.H{
"userID": "", // "userID": "",
"userName": "", // "userName": "",
"text": "Hello World.", // "text": "Hello World.",
}) // })
} // }
}) // })
// authorized.GET("/page/:id", func(c *gin.Context) { // authorized.GET("/page/:id", func(c *gin.Context) {
// logRoute := func(info string, infoReq string) { // logRoute := func(info string, infoReq string) {
// logRequest(c, cfg, "route", "/", info, infoReq) // logRequest(c, cfg, "route", "/", info, infoReq)
@ -157,7 +157,10 @@ func getRoutes(router *gin.Engine, cfg *cfg.Config, users *UsersAccounts, mdlsUs
authorized.GET(cfg.Routes["tracking"].Path, func(c *gin.Context) { authorized.GET(cfg.Routes["tracking"].Path, func(c *gin.Context) {
get_tracking_handle(c, routenv ) get_tracking_handle(c, routenv )
}) })
authorized.POST(cfg.Routes["post_tracking"].Path, func(c *gin.Context) { router.POST(cfg.Routes["post_tracking"].Path, func(c *gin.Context) {
post_tracking_handle(c, routenv )
})
authorized.POST(cfg.Routes["post_tracking_auth"].Path, func(c *gin.Context) {
post_tracking_handle(c, routenv ) post_tracking_handle(c, routenv )
}) })
authorized.GET(cfg.Routes["config"].Path, func(c *gin.Context) { authorized.GET(cfg.Routes["config"].Path, func(c *gin.Context) {