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