restapi_auth_jwt_test.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. package httpservers
  2. import (
  3. "context"
  4. "crypto/rand"
  5. "crypto/rsa"
  6. "crypto/x509"
  7. "encoding/pem"
  8. "fmt"
  9. config "github.com/OliveTin/OliveTin/internal/config"
  10. "github.com/golang-jwt/jwt/v4"
  11. "github.com/stretchr/testify/assert"
  12. "io"
  13. "net/http"
  14. "os"
  15. "testing"
  16. "time"
  17. )
  18. func createKeys(t *testing.T) (*rsa.PrivateKey, string) {
  19. tmpFile, _ := os.CreateTemp(os.TempDir(), "olivetin-jwt-")
  20. fmt.Println("Created File: " + tmpFile.Name())
  21. privateKey, _ := rsa.GenerateKey(rand.Reader, 2048)
  22. pubKey := &privateKey.PublicKey
  23. // https://stackoverflow.com/questions/13555085/save-and-load-crypto-rsa-privatekey-to-and-from-the-disk
  24. pkixPubKey, _ := x509.MarshalPKIXPublicKey(pubKey)
  25. pubPem := pem.EncodeToMemory(
  26. &pem.Block{
  27. Type: "RSA PUBLIC KEY",
  28. Bytes: pkixPubKey,
  29. },
  30. )
  31. if err := os.WriteFile(tmpFile.Name(), pubPem, 0755); err != nil {
  32. t.Fatalf("error when dumping pubKey: %s \n", err)
  33. }
  34. return privateKey, tmpFile.Name()
  35. }
  36. func testJwkValidation(t *testing.T, expire int64, expectCode int) {
  37. privateKey, publicKeyPath := createKeys(t)
  38. defer os.Remove(publicKeyPath)
  39. cfg := config.DefaultConfig()
  40. cfg.AuthJwtPubKeyPath = publicKeyPath
  41. cfg.AuthJwtClaimUsername = "sub"
  42. cfg.AuthJwtClaimUserGroup = "olivetinGroup"
  43. cfg.AuthJwtCookieName = "authorization_token"
  44. SetGlobalRestConfig(cfg) // ugly, setting global var, we should pass configs as params to modules... :/
  45. token := jwt.New(jwt.SigningMethodRS256)
  46. claims := token.Claims.(jwt.MapClaims)
  47. claims["nbf"] = time.Now().Unix() - 1000
  48. claims["exp"] = time.Now().Unix() + expire
  49. claims["sub"] = "test"
  50. claims["olivetinGroup"] = "test"
  51. tokenStr, _ := token.SignedString(privateKey)
  52. mux := newMux()
  53. mux.HandlePath("GET", "/", func(w http.ResponseWriter, r *http.Request, pathParams map[string]string) {
  54. username, usergroup := parseJwtCookie(r)
  55. if username == "" {
  56. w.WriteHeader(403)
  57. }
  58. w.Write([]byte(fmt.Sprintf("username=%v, usergroup=%v", username, usergroup)))
  59. })
  60. srv := setupTestingServer(mux, t)
  61. req, client := newReq("")
  62. req.AddCookie(&http.Cookie{
  63. Name: "authorization_token",
  64. Value: tokenStr,
  65. MaxAge: 300,
  66. })
  67. res, err := client.Do(req)
  68. if err != nil {
  69. t.Fatalf("Client err: %+v", err)
  70. } else {
  71. defer res.Body.Close()
  72. assert.Equal(t, expectCode, res.StatusCode)
  73. body, _ := io.ReadAll(res.Body)
  74. fmt.Println(string(body))
  75. }
  76. srv.Shutdown(context.TODO())
  77. }
  78. func TestJWTSignatureVerificationSucceeds(t *testing.T) {
  79. testJwkValidation(t, 1000, 200)
  80. }
  81. func TestJWTSignatureVerificationFails(t *testing.T) {
  82. testJwkValidation(t, -500, 403)
  83. }