Skip to content

Commit

Permalink
Improve auth interceptor and fix typo
Browse files Browse the repository at this point in the history
  • Loading branch information
wengchaoxi committed Apr 4, 2024
1 parent c819426 commit 0e49e4b
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 9 deletions.
13 changes: 6 additions & 7 deletions service/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,29 +4,28 @@ import (
"net/http"
"net/http/httputil"
"net/url"
"strings"
"time"

"github.com/gin-gonic/gin"
_ "github.com/joho/godotenv/autoload"
)

const (
AUTH_PROXY_TOKEN_NAME = "auth_proxy_token"
// AUTH_PROXY_ORIGIN_URL = "auth-proxy-origin-url"
)

func (s *Service) authInterrupter() gin.HandlerFunc {
func (s *Service) authInterceptor() gin.HandlerFunc {
return func(c *gin.Context) {
cookieToken, _ := c.Cookie(AUTH_PROXY_TOKEN_NAME)
if !s.opts.TokenManager.IsValidToken(cookieToken) {
reqUrl := c.Request.URL.String()
if strings.HasPrefix(reqUrl, AUTH_PROXY_ENDPOINT) {
reqPath := c.Request.URL.Path
if reqPath == AUTH_PROXY_ENDPOINT {
c.Next()
} else {
location := AUTH_PROXY_ENDPOINT
if reqUrl != "/" {
location = location + "?from=" + url.QueryEscape(reqUrl)
reqUrlStr := c.Request.URL.String()
if reqUrlStr != "/" {
location = location + "?from=" + url.QueryEscape(reqUrlStr)
}
c.Redirect(http.StatusTemporaryRedirect, location)
c.Abort()
Expand Down
4 changes: 2 additions & 2 deletions service/routers.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,10 @@ func New(opts *ServiceOptions) *Service {
}

func (s *Service) Init(engine *gin.Engine) {
auth := engine.Group(AUTH_PROXY_ENDPOINT, s.authInterrupter())
auth := engine.Group(AUTH_PROXY_ENDPOINT, s.authInterceptor())
{
auth.Static("/", "./web")
auth.POST("/", s.authHandler)
}
engine.NoRoute(s.authInterrupter(), s.proxyHandler)
engine.NoRoute(s.authInterceptor(), s.proxyHandler)
}

0 comments on commit 0e49e4b

Please sign in to comment.