Browse Source

Fixed #16

dependabot/npm_and_yarn/web/prismjs-1.21.0
Max Schmitt 8 years ago
parent
commit
e7d44b67a8
  1. 28
      handlers/auth.go

28
handlers/auth.go

@ -4,12 +4,14 @@ import (
"encoding/json" "encoding/json"
"fmt" "fmt"
"io/ioutil" "io/ioutil"
"log"
"net/http" "net/http"
"time" "time"
jwt "github.com/dgrijalva/jwt-go" jwt "github.com/dgrijalva/jwt-go"
"github.com/gin-gonic/contrib/sessions" "github.com/gin-gonic/contrib/sessions"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
"github.com/pkg/errors"
"golang.org/x/oauth2" "golang.org/x/oauth2"
"golang.org/x/oauth2/google" "golang.org/x/oauth2/google"
) )
@ -67,26 +69,34 @@ func (h *Handler) handleGoogleRedirect(c *gin.Context) {
} }
func (h *Handler) authMiddleware(c *gin.Context) { func (h *Handler) authMiddleware(c *gin.Context) {
authError := func() error {
authHeader := c.GetHeader("Authorization") authHeader := c.GetHeader("Authorization")
if authHeader == "" { if authHeader == "" {
c.AbortWithStatusJSON(http.StatusForbidden, gin.H{ return errors.New("'Authorization' header not set")
"error": "'Authorization' header not set",
})
return
} }
token, err := jwt.ParseWithClaims(authHeader, &jwtClaims{}, func(token *jwt.Token) (interface{}, error) { token, err := jwt.ParseWithClaims(authHeader, &jwtClaims{}, func(token *jwt.Token) (interface{}, error) {
return h.config.Secret, nil return h.config.Secret, nil
}) })
if err != nil { if err != nil {
c.AbortWithStatusJSON(http.StatusForbidden, gin.H{ return fmt.Errorf("could not parse token: %v", err)
"error": fmt.Sprintf("could not parse token: %v", err),
})
return
} }
if !token.Valid { if !token.Valid {
return errors.New("token is not valid")
}
return nil
}()
if authError != nil {
if h.config.EnableDebugMode {
c.AbortWithStatusJSON(http.StatusForbidden, gin.H{
"error": fmt.Sprintf("token is not valid: %v", authError),
})
log.Printf("Authentication middleware failed: %v\n", authError)
} else {
c.AbortWithStatusJSON(http.StatusForbidden, gin.H{ c.AbortWithStatusJSON(http.StatusForbidden, gin.H{
"error": "token is not valid", "error": "authentication failed",
}) })
}
return return
} }
c.Next() c.Next()

Loading…
Cancel
Save