jwt.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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. "crypto/rsa"
  18. "io/ioutil"
  19. jwt "github.com/dgrijalva/jwt-go"
  20. )
  21. type tokenJWT struct {
  22. signMethod string
  23. signKey *rsa.PrivateKey
  24. verifyKey *rsa.PublicKey
  25. }
  26. func (t *tokenJWT) enable() {}
  27. func (t *tokenJWT) disable() {}
  28. func (t *tokenJWT) invalidateUser(string) {}
  29. func (t *tokenJWT) genTokenPrefix() (string, error) { return "", nil }
  30. func (t *tokenJWT) info(ctx context.Context, token string, rev uint64) (*AuthInfo, bool) {
  31. // rev isn't used in JWT, it is only used in simple token
  32. var (
  33. username string
  34. revision uint64
  35. )
  36. parsed, err := jwt.Parse(token, func(token *jwt.Token) (interface{}, error) {
  37. return t.verifyKey, nil
  38. })
  39. switch err.(type) {
  40. case nil:
  41. if !parsed.Valid {
  42. plog.Warningf("invalid jwt token: %s", token)
  43. return nil, false
  44. }
  45. claims := parsed.Claims.(jwt.MapClaims)
  46. username = claims["username"].(string)
  47. revision = uint64(claims["revision"].(float64))
  48. default:
  49. plog.Warningf("failed to parse jwt token: %s", err)
  50. return nil, false
  51. }
  52. return &AuthInfo{Username: username, Revision: revision}, true
  53. }
  54. func (t *tokenJWT) assign(ctx context.Context, username string, revision uint64) (string, error) {
  55. // Future work: let a jwt token include permission information would be useful for
  56. // permission checking in proxy side.
  57. tk := jwt.NewWithClaims(jwt.GetSigningMethod(t.signMethod),
  58. jwt.MapClaims{
  59. "username": username,
  60. "revision": revision,
  61. })
  62. token, err := tk.SignedString(t.signKey)
  63. if err != nil {
  64. plog.Debugf("failed to sign jwt token: %s", err)
  65. return "", err
  66. }
  67. plog.Debugf("jwt token: %s", token)
  68. return token, err
  69. }
  70. func prepareOpts(opts map[string]string) (jwtSignMethod, jwtPubKeyPath, jwtPrivKeyPath string, err error) {
  71. for k, v := range opts {
  72. switch k {
  73. case "sign-method":
  74. jwtSignMethod = v
  75. case "pub-key":
  76. jwtPubKeyPath = v
  77. case "priv-key":
  78. jwtPrivKeyPath = v
  79. default:
  80. plog.Errorf("unknown token specific option: %s", k)
  81. return "", "", "", ErrInvalidAuthOpts
  82. }
  83. }
  84. if len(jwtSignMethod) == 0 {
  85. return "", "", "", ErrInvalidAuthOpts
  86. }
  87. return jwtSignMethod, jwtPubKeyPath, jwtPrivKeyPath, nil
  88. }
  89. func newTokenProviderJWT(opts map[string]string) (*tokenJWT, error) {
  90. jwtSignMethod, jwtPubKeyPath, jwtPrivKeyPath, err := prepareOpts(opts)
  91. if err != nil {
  92. return nil, ErrInvalidAuthOpts
  93. }
  94. t := &tokenJWT{}
  95. t.signMethod = jwtSignMethod
  96. verifyBytes, err := ioutil.ReadFile(jwtPubKeyPath)
  97. if err != nil {
  98. plog.Errorf("failed to read public key (%s) for jwt: %s", jwtPubKeyPath, err)
  99. return nil, err
  100. }
  101. t.verifyKey, err = jwt.ParseRSAPublicKeyFromPEM(verifyBytes)
  102. if err != nil {
  103. plog.Errorf("failed to parse public key (%s): %s", jwtPubKeyPath, err)
  104. return nil, err
  105. }
  106. signBytes, err := ioutil.ReadFile(jwtPrivKeyPath)
  107. if err != nil {
  108. plog.Errorf("failed to read private key (%s) for jwt: %s", jwtPrivKeyPath, err)
  109. return nil, err
  110. }
  111. t.signKey, err = jwt.ParseRSAPrivateKeyFromPEM(signBytes)
  112. if err != nil {
  113. plog.Errorf("failed to parse private key (%s): %s", jwtPrivKeyPath, err)
  114. return nil, err
  115. }
  116. return t, nil
  117. }