jwt_test.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. // Copyright 2017 The etcd Authors
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package auth
  15. import (
  16. "context"
  17. "fmt"
  18. "testing"
  19. "go.uber.org/zap"
  20. )
  21. const (
  22. jwtPubKey = "../integration/fixtures/server.crt"
  23. jwtPrivKey = "../integration/fixtures/server.key.insecure"
  24. )
  25. func TestJWTInfo(t *testing.T) {
  26. opts := map[string]string{
  27. "pub-key": jwtPubKey,
  28. "priv-key": jwtPrivKey,
  29. "sign-method": "RS256",
  30. }
  31. jwt, err := newTokenProviderJWT(zap.NewExample(), opts)
  32. if err != nil {
  33. t.Fatal(err)
  34. }
  35. token, aerr := jwt.assign(context.TODO(), "abc", 123)
  36. if aerr != nil {
  37. t.Fatal(err)
  38. }
  39. ai, ok := jwt.info(context.TODO(), token, 123)
  40. if !ok {
  41. t.Fatalf("failed to authenticate with token %s", token)
  42. }
  43. if ai.Revision != 123 {
  44. t.Fatalf("expected revision 123, got %d", ai.Revision)
  45. }
  46. ai, ok = jwt.info(context.TODO(), "aaa", 120)
  47. if ok || ai != nil {
  48. t.Fatalf("expected aaa to fail to authenticate, got %+v", ai)
  49. }
  50. }
  51. func TestJWTBad(t *testing.T) {
  52. opts := map[string]string{
  53. "pub-key": jwtPubKey,
  54. "priv-key": jwtPrivKey,
  55. "sign-method": "RS256",
  56. }
  57. // private key instead of public key
  58. opts["pub-key"] = jwtPrivKey
  59. if _, err := newTokenProviderJWT(zap.NewExample(), opts); err == nil {
  60. t.Fatalf("expected failure on missing public key")
  61. }
  62. opts["pub-key"] = jwtPubKey
  63. // public key instead of private key
  64. opts["priv-key"] = jwtPubKey
  65. if _, err := newTokenProviderJWT(zap.NewExample(), opts); err == nil {
  66. t.Fatalf("expected failure on missing public key")
  67. }
  68. opts["priv-key"] = jwtPrivKey
  69. // missing signing option
  70. delete(opts, "sign-method")
  71. if _, err := newTokenProviderJWT(zap.NewExample(), opts); err == nil {
  72. t.Fatal("expected error on missing option")
  73. }
  74. opts["sign-method"] = "RS256"
  75. // bad file for pubkey
  76. opts["pub-key"] = "whatever"
  77. if _, err := newTokenProviderJWT(zap.NewExample(), opts); err == nil {
  78. t.Fatalf("expected failure on missing public key")
  79. }
  80. opts["pub-key"] = jwtPubKey
  81. // bad file for private key
  82. opts["priv-key"] = "whatever"
  83. if _, err := newTokenProviderJWT(zap.NewExample(), opts); err == nil {
  84. t.Fatalf("expeceted failure on missing private key")
  85. }
  86. opts["priv-key"] = jwtPrivKey
  87. }
  88. // testJWTOpts is useful for passing to NewTokenProvider which requires a string.
  89. func testJWTOpts() string {
  90. return fmt.Sprintf("%s,pub-key=%s,priv-key=%s,sign-method=RS256", tokenTypeJWT, jwtPubKey, jwtPrivKey)
  91. }