jwt_test.go 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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. "testing"
  18. )
  19. const (
  20. jwtPubKey = "../../integration/fixtures/server.crt"
  21. jwtPrivKey = "../../integration/fixtures/server.key.insecure"
  22. )
  23. func TestJWTInfo(t *testing.T) {
  24. opts := map[string]string{
  25. "pub-key": jwtPubKey,
  26. "priv-key": jwtPrivKey,
  27. "sign-method": "RS256",
  28. }
  29. jwt, err := newTokenProviderJWT(opts)
  30. if err != nil {
  31. t.Fatal(err)
  32. }
  33. token, aerr := jwt.assign(context.TODO(), "abc", 123)
  34. if aerr != nil {
  35. t.Fatal(err)
  36. }
  37. ai, ok := jwt.info(context.TODO(), token, 123)
  38. if !ok {
  39. t.Fatalf("failed to authenticate with token %s", token)
  40. }
  41. if ai.Revision != 123 {
  42. t.Fatalf("expected revision 123, got %d", ai.Revision)
  43. }
  44. ai, ok = jwt.info(context.TODO(), "aaa", 120)
  45. if ok || ai != nil {
  46. t.Fatalf("expected aaa to fail to authenticate, got %+v", ai)
  47. }
  48. }
  49. func TestJWTBad(t *testing.T) {
  50. opts := map[string]string{
  51. "pub-key": jwtPubKey,
  52. "priv-key": jwtPrivKey,
  53. "sign-method": "RS256",
  54. }
  55. // private key instead of public key
  56. opts["pub-key"] = jwtPrivKey
  57. if _, err := newTokenProviderJWT(opts); err == nil {
  58. t.Fatalf("expected failure on missing public key")
  59. }
  60. opts["pub-key"] = jwtPubKey
  61. // public key instead of private key
  62. opts["priv-key"] = jwtPubKey
  63. if _, err := newTokenProviderJWT(opts); err == nil {
  64. t.Fatalf("expected failure on missing public key")
  65. }
  66. opts["priv-key"] = jwtPrivKey
  67. // missing signing option
  68. delete(opts, "sign-method")
  69. if _, err := newTokenProviderJWT(opts); err == nil {
  70. t.Fatal("expected error on missing option")
  71. }
  72. opts["sign-method"] = "RS256"
  73. // bad file for pubkey
  74. opts["pub-key"] = "whatever"
  75. if _, err := newTokenProviderJWT(opts); err == nil {
  76. t.Fatalf("expected failure on missing public key")
  77. }
  78. opts["pub-key"] = jwtPubKey
  79. // bad file for private key
  80. opts["priv-key"] = "whatever"
  81. if _, err := newTokenProviderJWT(opts); err == nil {
  82. t.Fatalf("expeceted failure on missing private key")
  83. }
  84. opts["priv-key"] = jwtPrivKey
  85. }