Merge pull request #1084 from trheyi/main
Enhance hello world endpoints with query string and post payload logging
This commit is contained in:
commit
9e2ec76b11
2 changed files with 92 additions and 18 deletions
|
|
@ -1,6 +1,7 @@
|
||||||
package hello
|
package hello
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"io"
|
||||||
"net/http"
|
"net/http"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
|
@ -23,29 +24,53 @@ func Attach(group *gin.RouterGroup, oauth types.OAuth) {
|
||||||
|
|
||||||
func helloWorldPublic(c *gin.Context) {
|
func helloWorldPublic(c *gin.Context) {
|
||||||
serverTime := time.Now().Format(time.RFC3339)
|
serverTime := time.Now().Format(time.RFC3339)
|
||||||
|
|
||||||
|
// Get query string as raw string
|
||||||
|
queryString := c.Request.URL.RawQuery
|
||||||
|
|
||||||
|
// Get post payload
|
||||||
|
var postPayload string
|
||||||
|
if body, err := io.ReadAll(c.Request.Body); err == nil {
|
||||||
|
postPayload = string(body)
|
||||||
|
}
|
||||||
|
|
||||||
c.JSON(http.StatusOK, gin.H{
|
c.JSON(http.StatusOK, gin.H{
|
||||||
"MESSAGE": "HELLO, WORLD",
|
"MESSAGE": "HELLO, WORLD",
|
||||||
"SERVER_TIME": serverTime,
|
"SERVER_TIME": serverTime,
|
||||||
"VERSION": share.VERSION,
|
"VERSION": share.VERSION,
|
||||||
"PRVERSION": share.PRVERSION,
|
"PRVERSION": share.PRVERSION,
|
||||||
"CUI": share.CUI,
|
"CUI": share.CUI,
|
||||||
"PRCUI": share.PRCUI,
|
"PRCUI": share.PRCUI,
|
||||||
"APP": share.App.Name,
|
"APP": share.App.Name,
|
||||||
"APP_VERSION": share.App.Version,
|
"APP_VERSION": share.App.Version,
|
||||||
|
"QUERYSTRING": queryString,
|
||||||
|
"POST_PAYLOAD": postPayload,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
// helloWorldHello is the handler for the hello world endpoint
|
// helloWorldHello is the handler for the hello world endpoint
|
||||||
func helloWorldProtected(c *gin.Context) {
|
func helloWorldProtected(c *gin.Context) {
|
||||||
serverTime := time.Now().Format(time.RFC3339)
|
serverTime := time.Now().Format(time.RFC3339)
|
||||||
|
|
||||||
|
// Get query string as raw string
|
||||||
|
queryString := c.Request.URL.RawQuery
|
||||||
|
|
||||||
|
// Get post payload
|
||||||
|
var postPayload string
|
||||||
|
if body, err := io.ReadAll(c.Request.Body); err == nil {
|
||||||
|
postPayload = string(body)
|
||||||
|
}
|
||||||
|
|
||||||
c.JSON(http.StatusOK, gin.H{
|
c.JSON(http.StatusOK, gin.H{
|
||||||
"MESSAGE": "HELLO, WORLD",
|
"MESSAGE": "HELLO, WORLD",
|
||||||
"SERVER_TIME": serverTime,
|
"SERVER_TIME": serverTime,
|
||||||
"VERSION": share.VERSION,
|
"VERSION": share.VERSION,
|
||||||
"PRVERSION": share.PRVERSION,
|
"PRVERSION": share.PRVERSION,
|
||||||
"CUI": share.CUI,
|
"CUI": share.CUI,
|
||||||
"PRCUI": share.PRCUI,
|
"PRCUI": share.PRCUI,
|
||||||
"APP": share.App.Name,
|
"APP": share.App.Name,
|
||||||
"APP_VERSION": share.App.Version,
|
"APP_VERSION": share.App.Version,
|
||||||
|
"QUERYSTRING": queryString,
|
||||||
|
"POST_PAYLOAD": postPayload,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -3,14 +3,16 @@ package oauth
|
||||||
import (
|
import (
|
||||||
"net/http"
|
"net/http"
|
||||||
"strings"
|
"strings"
|
||||||
|
"time"
|
||||||
|
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
|
"github.com/yaoapp/yao/openapi/oauth/types"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Guard is the OAuth guard middleware
|
// Guard is the OAuth guard middleware
|
||||||
func (s *Service) Guard(c *gin.Context) {
|
func (s *Service) Guard(c *gin.Context) {
|
||||||
// Get the token from the request
|
// Get the token from the request
|
||||||
token := c.GetHeader("Authorization")
|
token := s.getAccessToken(c)
|
||||||
|
|
||||||
// Validate the token
|
// Validate the token
|
||||||
if token == "" {
|
if token == "" {
|
||||||
|
|
@ -20,11 +22,58 @@ func (s *Service) Guard(c *gin.Context) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Validate the token
|
// Validate the token
|
||||||
_, err := s.VerifyToken(strings.TrimPrefix(token, "Bearer "))
|
claims, err := s.VerifyToken(token)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
c.JSON(http.StatusUnauthorized, gin.H{"error": "Invalid token"})
|
c.JSON(http.StatusUnauthorized, gin.H{"error": "Invalid token"})
|
||||||
c.Abort()
|
c.Abort()
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Auto refresh the token
|
||||||
|
if claims.ExpiresAt.Before(time.Now()) {
|
||||||
|
s.tryAutoRefreshToken(c, claims)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *Service) tryAutoRefreshToken(c *gin.Context, _ *types.TokenClaims) {
|
||||||
|
refreshToken := s.getRefreshToken(c)
|
||||||
|
if refreshToken == "" {
|
||||||
|
c.JSON(http.StatusUnauthorized, gin.H{"error": "Unauthorized"})
|
||||||
|
c.Abort()
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// Verify the refresh token
|
||||||
|
_, err := s.VerifyToken(refreshToken)
|
||||||
|
if err != nil {
|
||||||
|
c.JSON(http.StatusUnauthorized, gin.H{"error": "Invalid token"})
|
||||||
|
c.Abort()
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// @Todo: Auto refresh the token
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *Service) getAccessToken(c *gin.Context) string {
|
||||||
|
token := c.GetHeader("Authorization")
|
||||||
|
if token == "" {
|
||||||
|
cookie, err := c.Cookie("__Host-access_token")
|
||||||
|
if err != nil {
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
token = cookie
|
||||||
|
}
|
||||||
|
return strings.TrimPrefix(token, "Bearer ")
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *Service) getRefreshToken(c *gin.Context) string {
|
||||||
|
token := c.GetHeader("Authorization")
|
||||||
|
if token == "" {
|
||||||
|
cookie, err := c.Cookie("__Host-refresh_token")
|
||||||
|
if err != nil {
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
token = cookie
|
||||||
|
}
|
||||||
|
return strings.TrimPrefix(token, "Bearer ")
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue