users_gpg_keys.go 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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. // TODO: remove custom Accept header when this API fully launches.
  58. req.Header.Set("Accept", mediaTypeGitSigningPreview)
  59. var keys []*GPGKey
  60. resp, err := s.client.Do(ctx, req, &keys)
  61. if err != nil {
  62. return nil, resp, err
  63. }
  64. return keys, resp, nil
  65. }
  66. // GetGPGKey gets extended details for a single GPG key. It requires authentication
  67. // via Basic Auth or via OAuth with at least read:gpg_key scope.
  68. //
  69. // GitHub API docs: https://developer.github.com/v3/users/gpg_keys/#get-a-single-gpg-key
  70. func (s *UsersService) GetGPGKey(ctx context.Context, id int64) (*GPGKey, *Response, error) {
  71. u := fmt.Sprintf("user/gpg_keys/%v", id)
  72. req, err := s.client.NewRequest("GET", u, nil)
  73. if err != nil {
  74. return nil, nil, err
  75. }
  76. // TODO: remove custom Accept header when this API fully launches.
  77. req.Header.Set("Accept", mediaTypeGitSigningPreview)
  78. key := &GPGKey{}
  79. resp, err := s.client.Do(ctx, req, key)
  80. if err != nil {
  81. return nil, resp, err
  82. }
  83. return key, resp, nil
  84. }
  85. // CreateGPGKey creates a GPG key. It requires authenticatation via Basic Auth
  86. // or OAuth with at least write:gpg_key scope.
  87. //
  88. // GitHub API docs: https://developer.github.com/v3/users/gpg_keys/#create-a-gpg-key
  89. func (s *UsersService) CreateGPGKey(ctx context.Context, armoredPublicKey string) (*GPGKey, *Response, error) {
  90. gpgKey := &struct {
  91. ArmoredPublicKey string `json:"armored_public_key"`
  92. }{ArmoredPublicKey: armoredPublicKey}
  93. req, err := s.client.NewRequest("POST", "user/gpg_keys", gpgKey)
  94. if err != nil {
  95. return nil, nil, err
  96. }
  97. // TODO: remove custom Accept header when this API fully launches.
  98. req.Header.Set("Accept", mediaTypeGitSigningPreview)
  99. key := &GPGKey{}
  100. resp, err := s.client.Do(ctx, req, key)
  101. if err != nil {
  102. return nil, resp, err
  103. }
  104. return key, resp, nil
  105. }
  106. // DeleteGPGKey deletes a GPG key. It requires authentication via Basic Auth or
  107. // via OAuth with at least admin:gpg_key scope.
  108. //
  109. // GitHub API docs: https://developer.github.com/v3/users/gpg_keys/#delete-a-gpg-key
  110. func (s *UsersService) DeleteGPGKey(ctx context.Context, id int64) (*Response, error) {
  111. u := fmt.Sprintf("user/gpg_keys/%v", id)
  112. req, err := s.client.NewRequest("DELETE", u, nil)
  113. if err != nil {
  114. return nil, err
  115. }
  116. // TODO: remove custom Accept header when this API fully launches.
  117. req.Header.Set("Accept", mediaTypeGitSigningPreview)
  118. return s.client.Do(ctx, req, nil)
  119. }