auth.go 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. // Package auth implements an interface for providing CFSSL
  2. // authentication. This is meant to authenticate a client CFSSL to a
  3. // remote CFSSL in order to prevent unauthorised use of the signature
  4. // capabilities. This package provides both the interface and a
  5. // standard HMAC-based implementation.
  6. package auth
  7. import (
  8. "crypto/hmac"
  9. "crypto/sha256"
  10. "encoding/hex"
  11. "fmt"
  12. "io/ioutil"
  13. "os"
  14. "strings"
  15. )
  16. // An AuthenticatedRequest contains a request and authentication
  17. // token. The Provider may determine whether to validate the timestamp
  18. // and remote address.
  19. type AuthenticatedRequest struct {
  20. // An Authenticator decides whether to use this field.
  21. Timestamp int64 `json:"timestamp,omitempty"`
  22. RemoteAddress []byte `json:"remote_address,omitempty"`
  23. Token []byte `json:"token"`
  24. Request []byte `json:"request"`
  25. }
  26. // A Provider can generate tokens from a request and verify a
  27. // request. The handling of additional authentication data (such as
  28. // the IP address) is handled by the concrete type, as is any
  29. // serialisation and state-keeping.
  30. type Provider interface {
  31. Token(req []byte) (token []byte, err error)
  32. Verify(aReq *AuthenticatedRequest) bool
  33. }
  34. // Standard implements an HMAC-SHA-256 authentication provider. It may
  35. // be supplied additional data at creation time that will be used as
  36. // request || additional-data with the HMAC.
  37. type Standard struct {
  38. key []byte
  39. ad []byte
  40. }
  41. // New generates a new standard authentication provider from the key
  42. // and additional data. The additional data will be used when
  43. // generating a new token.
  44. func New(key string, ad []byte) (*Standard, error) {
  45. if splitKey := strings.SplitN(key, ":", 2); len(splitKey) == 2 {
  46. switch splitKey[0] {
  47. case "env":
  48. key = os.Getenv(splitKey[1])
  49. case "file":
  50. data, err := ioutil.ReadFile(splitKey[1])
  51. if err != nil {
  52. return nil, err
  53. }
  54. key = string(data)
  55. default:
  56. return nil, fmt.Errorf("unknown key prefix: %s", splitKey[0])
  57. }
  58. }
  59. keyBytes, err := hex.DecodeString(key)
  60. if err != nil {
  61. return nil, err
  62. }
  63. return &Standard{keyBytes, ad}, nil
  64. }
  65. // Token generates a new authentication token from the request.
  66. func (p Standard) Token(req []byte) (token []byte, err error) {
  67. h := hmac.New(sha256.New, p.key)
  68. h.Write(req)
  69. h.Write(p.ad)
  70. return h.Sum(nil), nil
  71. }
  72. // Verify determines whether an authenticated request is valid.
  73. func (p Standard) Verify(ad *AuthenticatedRequest) bool {
  74. if ad == nil {
  75. return false
  76. }
  77. // Standard token generation returns no error.
  78. token, _ := p.Token(ad.Request)
  79. if len(ad.Token) != len(token) {
  80. return false
  81. }
  82. return hmac.Equal(token, ad.Token)
  83. }