prevent panic when sse token is not found

This commit is contained in:
Jamie Curnow 2023-05-29 15:18:18 +10:00
parent 4dd6fd06f4
commit 3301800f42
No known key found for this signature in database
GPG Key ID: FFBB624C43388E9E
2 changed files with 12 additions and 2 deletions

View File

@ -29,7 +29,7 @@ func DecodeAuth() func(http.Handler) http.Handler {
} }
tokenAuth := jwtauth.New("RS256", privateKey, publicKey) tokenAuth := jwtauth.New("RS256", privateKey, publicKey)
return jwtauth.Verify(tokenAuth, jwtauth.TokenFromHeader) return jwtauth.Verify(tokenAuth, jwtauth.TokenFromHeader, jwtauth.TokenFromQuery)
} }
// Enforce is a authentication middleware to enforce access from the // Enforce is a authentication middleware to enforce access from the

View File

@ -14,13 +14,23 @@ import (
func SSEAuth(next http.Handler) http.Handler { func SSEAuth(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
ctx := r.Context() ctx := r.Context()
token, claims, err := jwtauth.FromContext(ctx) token, claims, err := jwtauth.FromContext(ctx)
if err != nil { if err != nil {
h.ResultErrorJSON(w, r, http.StatusUnauthorized, err.Error(), nil) h.ResultErrorJSON(w, r, http.StatusUnauthorized, err.Error(), nil)
return return
} }
if token == nil {
h.ResultErrorJSON(w, r, http.StatusUnauthorized, "No token given", nil)
return
}
if claims != nil {
h.ResultErrorJSON(w, r, http.StatusUnauthorized, "Unauthorised", nil)
return
}
userID := uint(claims["uid"].(float64)) userID := uint(claims["uid"].(float64))
_, enabled := user.IsEnabled(userID) _, enabled := user.IsEnabled(userID)
if token == nil || !token.Valid || !enabled || !claims.VerifyIssuer("sse", true) { if token == nil || !token.Valid || !enabled || !claims.VerifyIssuer("sse", true) {