4
0

method.go 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. package provisioner
  2. import (
  3. "context"
  4. )
  5. // Method indicates the action to action that we will perform, it's used as part
  6. // of the context in the call to authorize. It defaults to Sing.
  7. type Method int
  8. // The key to save the Method in the context.
  9. type methodKey struct{}
  10. const (
  11. // SignMethod is the method used to sign X.509 certificates.
  12. SignMethod Method = iota
  13. // SignIdentityMethod is the method used to sign X.509 identity certificates.
  14. SignIdentityMethod
  15. // RevokeMethod is the method used to revoke X.509 certificates.
  16. RevokeMethod
  17. // RenewMethod is the method used to renew X.509 certificates.
  18. RenewMethod
  19. // SSHSignMethod is the method used to sign SSH certificates.
  20. SSHSignMethod
  21. // SSHRenewMethod is the method used to renew SSH certificates.
  22. SSHRenewMethod
  23. // SSHRevokeMethod is the method used to revoke SSH certificates.
  24. SSHRevokeMethod
  25. // SSHRekeyMethod is the method used to rekey SSH certificates.
  26. SSHRekeyMethod
  27. )
  28. // String returns a string representation of the context method.
  29. func (m Method) String() string {
  30. switch m {
  31. case SignMethod:
  32. return "sign-method"
  33. case SignIdentityMethod:
  34. return "sign-identity-method"
  35. case RevokeMethod:
  36. return "revoke-method"
  37. case RenewMethod:
  38. return "renew-method"
  39. case SSHSignMethod:
  40. return "ssh-sign-method"
  41. case SSHRenewMethod:
  42. return "ssh-renew-method"
  43. case SSHRevokeMethod:
  44. return "ssh-revoke-method"
  45. case SSHRekeyMethod:
  46. return "ssh-rekey-method"
  47. default:
  48. return "unknown"
  49. }
  50. }
  51. // NewContextWithMethod creates a new context from ctx and attaches method to
  52. // it.
  53. func NewContextWithMethod(ctx context.Context, method Method) context.Context {
  54. return context.WithValue(ctx, methodKey{}, method)
  55. }
  56. // MethodFromContext returns the Method saved in ctx.
  57. func MethodFromContext(ctx context.Context) Method {
  58. m, _ := ctx.Value(methodKey{}).(Method)
  59. return m
  60. }
  61. type tokenKey struct{}
  62. // NewContextWithToken creates a new context with the given token.
  63. func NewContextWithToken(ctx context.Context, token string) context.Context {
  64. return context.WithValue(ctx, tokenKey{}, token)
  65. }
  66. // TokenFromContext returns the token stored in the given context.
  67. func TokenFromContext(ctx context.Context) (string, bool) {
  68. token, ok := ctx.Value(tokenKey{}).(string)
  69. return token, ok
  70. }
  71. // The key to save the certTypeKey in the context.
  72. type certTypeKey struct{}
  73. // NewContextWithCertType creates a new context with the given CertType.
  74. func NewContextWithCertType(ctx context.Context, certType string) context.Context {
  75. return context.WithValue(ctx, certTypeKey{}, certType)
  76. }
  77. // CertTypeFromContext returns the certType stored in the given context.
  78. func CertTypeFromContext(ctx context.Context) (string, bool) {
  79. certType, ok := ctx.Value(certTypeKey{}).(string)
  80. return certType, ok
  81. }