github.com/google/go-github/v33@v33.0.0/github/users_keys.go (about) 1 // Copyright 2013 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 // Key represents a public SSH key used to authenticate a user or deploy script. 14 type Key struct { 15 ID *int64 `json:"id,omitempty"` 16 Key *string `json:"key,omitempty"` 17 URL *string `json:"url,omitempty"` 18 Title *string `json:"title,omitempty"` 19 ReadOnly *bool `json:"read_only,omitempty"` 20 CreatedAt *Timestamp `json:"created_at,omitempty"` 21 } 22 23 func (k Key) String() string { 24 return Stringify(k) 25 } 26 27 // ListKeys lists the verified public keys for a user. Passing the empty 28 // string will fetch keys for the authenticated user. 29 // 30 // GitHub API docs: https://docs.github.com/en/free-pro-team@latest/rest/reference/users/#list-public-ssh-keys-for-the-authenticated-user 31 // GitHub API docs: https://docs.github.com/en/free-pro-team@latest/rest/reference/users/#list-public-keys-for-a-user 32 func (s *UsersService) ListKeys(ctx context.Context, user string, opts *ListOptions) ([]*Key, *Response, error) { 33 var u string 34 if user != "" { 35 u = fmt.Sprintf("users/%v/keys", user) 36 } else { 37 u = "user/keys" 38 } 39 u, err := addOptions(u, opts) 40 if err != nil { 41 return nil, nil, err 42 } 43 44 req, err := s.client.NewRequest("GET", u, nil) 45 if err != nil { 46 return nil, nil, err 47 } 48 49 var keys []*Key 50 resp, err := s.client.Do(ctx, req, &keys) 51 if err != nil { 52 return nil, resp, err 53 } 54 55 return keys, resp, nil 56 } 57 58 // GetKey fetches a single public key. 59 // 60 // GitHub API docs: https://docs.github.com/en/free-pro-team@latest/rest/reference/users/#get-a-public-ssh-key-for-the-authenticated-user 61 func (s *UsersService) GetKey(ctx context.Context, id int64) (*Key, *Response, error) { 62 u := fmt.Sprintf("user/keys/%v", id) 63 64 req, err := s.client.NewRequest("GET", u, nil) 65 if err != nil { 66 return nil, nil, err 67 } 68 69 key := new(Key) 70 resp, err := s.client.Do(ctx, req, key) 71 if err != nil { 72 return nil, resp, err 73 } 74 75 return key, resp, nil 76 } 77 78 // CreateKey adds a public key for the authenticated user. 79 // 80 // GitHub API docs: https://docs.github.com/en/free-pro-team@latest/rest/reference/users/#create-a-public-ssh-key-for-the-authenticated-user 81 func (s *UsersService) CreateKey(ctx context.Context, key *Key) (*Key, *Response, error) { 82 u := "user/keys" 83 84 req, err := s.client.NewRequest("POST", u, key) 85 if err != nil { 86 return nil, nil, err 87 } 88 89 k := new(Key) 90 resp, err := s.client.Do(ctx, req, k) 91 if err != nil { 92 return nil, resp, err 93 } 94 95 return k, resp, nil 96 } 97 98 // DeleteKey deletes a public key. 99 // 100 // GitHub API docs: https://docs.github.com/en/free-pro-team@latest/rest/reference/users/#delete-a-public-ssh-key-for-the-authenticated-user 101 func (s *UsersService) DeleteKey(ctx context.Context, id int64) (*Response, error) { 102 u := fmt.Sprintf("user/keys/%v", id) 103 104 req, err := s.client.NewRequest("DELETE", u, nil) 105 if err != nil { 106 return nil, err 107 } 108 109 return s.client.Do(ctx, req, nil) 110 }