jwt_handler.go 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. // Copyright 2022 The go-ethereum Authors
  2. // This file is part of the go-ethereum library.
  3. //
  4. // The go-ethereum library is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU Lesser General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // The go-ethereum library is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU Lesser General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU Lesser General Public License
  15. // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
  16. package node
  17. import (
  18. "net/http"
  19. "strings"
  20. "time"
  21. "github.com/golang-jwt/jwt/v4"
  22. )
  23. const jwtExpiryTimeout = 60 * time.Second
  24. type jwtHandler struct {
  25. keyFunc func(token *jwt.Token) (interface{}, error)
  26. next http.Handler
  27. }
  28. // newJWTHandler creates a http.Handler with jwt authentication support.
  29. func newJWTHandler(secret []byte, next http.Handler) http.Handler {
  30. return &jwtHandler{
  31. keyFunc: func(token *jwt.Token) (interface{}, error) {
  32. return secret, nil
  33. },
  34. next: next,
  35. }
  36. }
  37. // ServeHTTP implements http.Handler
  38. func (handler *jwtHandler) ServeHTTP(out http.ResponseWriter, r *http.Request) {
  39. var (
  40. strToken string
  41. claims jwt.RegisteredClaims
  42. )
  43. if auth := r.Header.Get("Authorization"); strings.HasPrefix(auth, "Bearer ") {
  44. strToken = strings.TrimPrefix(auth, "Bearer ")
  45. }
  46. if len(strToken) == 0 {
  47. http.Error(out, "missing token", http.StatusForbidden)
  48. return
  49. }
  50. // We explicitly set only HS256 allowed, and also disables the
  51. // claim-check: the RegisteredClaims internally requires 'iat' to
  52. // be no later than 'now', but we allow for a bit of drift.
  53. token, err := jwt.ParseWithClaims(strToken, &claims, handler.keyFunc,
  54. jwt.WithValidMethods([]string{"HS256"}),
  55. jwt.WithoutClaimsValidation())
  56. switch {
  57. case err != nil:
  58. http.Error(out, err.Error(), http.StatusForbidden)
  59. case !token.Valid:
  60. http.Error(out, "invalid token", http.StatusForbidden)
  61. case !claims.VerifyExpiresAt(time.Now(), false): // optional
  62. http.Error(out, "token is expired", http.StatusForbidden)
  63. case claims.IssuedAt == nil:
  64. http.Error(out, "missing issued-at", http.StatusForbidden)
  65. case time.Since(claims.IssuedAt.Time) > jwtExpiryTimeout:
  66. http.Error(out, "stale token", http.StatusForbidden)
  67. case time.Until(claims.IssuedAt.Time) > jwtExpiryTimeout:
  68. http.Error(out, "future token", http.StatusForbidden)
  69. default:
  70. handler.next.ServeHTTP(out, r)
  71. }
  72. }