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