You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

users_gpg_keys.go 3.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. // Copyright 2016 The go-github AUTHORS. All rights reserved.
  2. //
  3. // Use of this source code is governed by a BSD-style
  4. // license that can be found in the LICENSE file.
  5. package github
  6. import (
  7. "context"
  8. "fmt"
  9. "time"
  10. )
  11. // GPGKey represents a GitHub user's public GPG key used to verify GPG signed commits and tags.
  12. //
  13. // https://developer.github.com/changes/2016-04-04-git-signing-api-preview/
  14. type GPGKey struct {
  15. ID *int64 `json:"id,omitempty"`
  16. PrimaryKeyID *int64 `json:"primary_key_id,omitempty"`
  17. KeyID *string `json:"key_id,omitempty"`
  18. PublicKey *string `json:"public_key,omitempty"`
  19. Emails []GPGEmail `json:"emails,omitempty"`
  20. Subkeys []GPGKey `json:"subkeys,omitempty"`
  21. CanSign *bool `json:"can_sign,omitempty"`
  22. CanEncryptComms *bool `json:"can_encrypt_comms,omitempty"`
  23. CanEncryptStorage *bool `json:"can_encrypt_storage,omitempty"`
  24. CanCertify *bool `json:"can_certify,omitempty"`
  25. CreatedAt *time.Time `json:"created_at,omitempty"`
  26. ExpiresAt *time.Time `json:"expires_at,omitempty"`
  27. }
  28. // String stringifies a GPGKey.
  29. func (k GPGKey) String() string {
  30. return Stringify(k)
  31. }
  32. // GPGEmail represents an email address associated to a GPG key.
  33. type GPGEmail struct {
  34. Email *string `json:"email,omitempty"`
  35. Verified *bool `json:"verified,omitempty"`
  36. }
  37. // ListGPGKeys lists the public GPG keys for a user. Passing the empty
  38. // string will fetch keys for the authenticated user. It requires authentication
  39. // via Basic Auth or via OAuth with at least read:gpg_key scope.
  40. //
  41. // GitHub API docs: https://developer.github.com/v3/users/gpg_keys/#list-gpg-keys-for-a-user
  42. func (s *UsersService) ListGPGKeys(ctx context.Context, user string, opt *ListOptions) ([]*GPGKey, *Response, error) {
  43. var u string
  44. if user != "" {
  45. u = fmt.Sprintf("users/%v/gpg_keys", user)
  46. } else {
  47. u = "user/gpg_keys"
  48. }
  49. u, err := addOptions(u, opt)
  50. if err != nil {
  51. return nil, nil, err
  52. }
  53. req, err := s.client.NewRequest("GET", u, nil)
  54. if err != nil {
  55. return nil, nil, err
  56. }
  57. var keys []*GPGKey
  58. resp, err := s.client.Do(ctx, req, &keys)
  59. if err != nil {
  60. return nil, resp, err
  61. }
  62. return keys, resp, nil
  63. }
  64. // GetGPGKey gets extended details for a single GPG key. It requires authentication
  65. // via Basic Auth or via OAuth with at least read:gpg_key scope.
  66. //
  67. // GitHub API docs: https://developer.github.com/v3/users/gpg_keys/#get-a-single-gpg-key
  68. func (s *UsersService) GetGPGKey(ctx context.Context, id int64) (*GPGKey, *Response, error) {
  69. u := fmt.Sprintf("user/gpg_keys/%v", id)
  70. req, err := s.client.NewRequest("GET", u, nil)
  71. if err != nil {
  72. return nil, nil, err
  73. }
  74. key := &GPGKey{}
  75. resp, err := s.client.Do(ctx, req, key)
  76. if err != nil {
  77. return nil, resp, err
  78. }
  79. return key, resp, nil
  80. }
  81. // CreateGPGKey creates a GPG key. It requires authenticatation via Basic Auth
  82. // or OAuth with at least write:gpg_key scope.
  83. //
  84. // GitHub API docs: https://developer.github.com/v3/users/gpg_keys/#create-a-gpg-key
  85. func (s *UsersService) CreateGPGKey(ctx context.Context, armoredPublicKey string) (*GPGKey, *Response, error) {
  86. gpgKey := &struct {
  87. ArmoredPublicKey string `json:"armored_public_key"`
  88. }{ArmoredPublicKey: armoredPublicKey}
  89. req, err := s.client.NewRequest("POST", "user/gpg_keys", gpgKey)
  90. if err != nil {
  91. return nil, nil, err
  92. }
  93. key := &GPGKey{}
  94. resp, err := s.client.Do(ctx, req, key)
  95. if err != nil {
  96. return nil, resp, err
  97. }
  98. return key, resp, nil
  99. }
  100. // DeleteGPGKey deletes a GPG key. It requires authentication via Basic Auth or
  101. // via OAuth with at least admin:gpg_key scope.
  102. //
  103. // GitHub API docs: https://developer.github.com/v3/users/gpg_keys/#delete-a-gpg-key
  104. func (s *UsersService) DeleteGPGKey(ctx context.Context, id int64) (*Response, error) {
  105. u := fmt.Sprintf("user/gpg_keys/%v", id)
  106. req, err := s.client.NewRequest("DELETE", u, nil)
  107. if err != nil {
  108. return nil, err
  109. }
  110. return s.client.Do(ctx, req, nil)
  111. }