jws.go 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. /*
  2. Copyright 2016 The Kubernetes Authors.
  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. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package bootstrap
  14. import (
  15. "fmt"
  16. "strings"
  17. jose "gopkg.in/square/go-jose.v2"
  18. )
  19. // computeDetachedSig takes content and token details and computes a detached
  20. // JWS signature. This is described in Appendix F of RFC 7515. Basically, this
  21. // is a regular JWS with the content part of the signature elided.
  22. func computeDetachedSig(content, tokenID, tokenSecret string) (string, error) {
  23. jwk := &jose.JSONWebKey{
  24. Key: []byte(tokenSecret),
  25. KeyID: tokenID,
  26. }
  27. opts := &jose.SignerOptions{
  28. // Since this is a symmetric key, go-jose doesn't automatically include
  29. // the KeyID as part of the protected header. We have to pass it here
  30. // explicitly.
  31. ExtraHeaders: map[jose.HeaderKey]interface{}{
  32. "kid": tokenID,
  33. },
  34. }
  35. signer, err := jose.NewSigner(jose.SigningKey{Algorithm: jose.HS256, Key: jwk}, opts)
  36. if err != nil {
  37. return "", fmt.Errorf("can't make a HS256 signer from the given token: %v", err)
  38. }
  39. jws, err := signer.Sign([]byte(content))
  40. if err != nil {
  41. return "", fmt.Errorf("can't HS256-sign the given token: %v", err)
  42. }
  43. fullSig, err := jws.CompactSerialize()
  44. if err != nil {
  45. return "", fmt.Errorf("can't serialize the given token: %v", err)
  46. }
  47. return stripContent(fullSig)
  48. }
  49. // stripContent will remove the content part of a compact JWS
  50. //
  51. // The `go-jose` library doesn't support generating signatures with "detached"
  52. // content. To make up for this we take the full compact signature, break it
  53. // apart and put it back together without the content section.
  54. func stripContent(fullSig string) (string, error) {
  55. parts := strings.Split(fullSig, ".")
  56. if len(parts) != 3 {
  57. return "", fmt.Errorf("compact JWS format must have three parts")
  58. }
  59. return parts[0] + ".." + parts[2], nil
  60. }
  61. // DetachedTokenIsValid checks whether a given detached JWS-encoded token matches JWS output of the given content and token
  62. func DetachedTokenIsValid(detachedToken, content, tokenID, tokenSecret string) bool {
  63. newToken, err := computeDetachedSig(content, tokenID, tokenSecret)
  64. if err != nil {
  65. return false
  66. }
  67. return detachedToken == newToken
  68. }