jwt_test.go 2.7 KB

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