users_keys.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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. package github
  6. import (
  7. "context"
  8. "fmt"
  9. )
  10. // Key represents a public SSH key used to authenticate a user or deploy script.
  11. type Key struct {
  12. ID *int64 `json:"id,omitempty"`
  13. Key *string `json:"key,omitempty"`
  14. URL *string `json:"url,omitempty"`
  15. Title *string `json:"title,omitempty"`
  16. ReadOnly *bool `json:"read_only,omitempty"`
  17. }
  18. func (k Key) String() string {
  19. return Stringify(k)
  20. }
  21. // ListKeys lists the verified public keys for a user. Passing the empty
  22. // string will fetch keys for the authenticated user.
  23. //
  24. // GitHub API docs: https://developer.github.com/v3/users/keys/#list-public-keys-for-a-user
  25. func (s *UsersService) ListKeys(ctx context.Context, user string, opt *ListOptions) ([]*Key, *Response, error) {
  26. var u string
  27. if user != "" {
  28. u = fmt.Sprintf("users/%v/keys", user)
  29. } else {
  30. u = "user/keys"
  31. }
  32. u, err := addOptions(u, opt)
  33. if err != nil {
  34. return nil, nil, err
  35. }
  36. req, err := s.client.NewRequest("GET", u, nil)
  37. if err != nil {
  38. return nil, nil, err
  39. }
  40. var keys []*Key
  41. resp, err := s.client.Do(ctx, req, &keys)
  42. if err != nil {
  43. return nil, resp, err
  44. }
  45. return keys, resp, nil
  46. }
  47. // GetKey fetches a single public key.
  48. //
  49. // GitHub API docs: https://developer.github.com/v3/users/keys/#get-a-single-public-key
  50. func (s *UsersService) GetKey(ctx context.Context, id int64) (*Key, *Response, error) {
  51. u := fmt.Sprintf("user/keys/%v", id)
  52. req, err := s.client.NewRequest("GET", u, nil)
  53. if err != nil {
  54. return nil, nil, err
  55. }
  56. key := new(Key)
  57. resp, err := s.client.Do(ctx, req, key)
  58. if err != nil {
  59. return nil, resp, err
  60. }
  61. return key, resp, nil
  62. }
  63. // CreateKey adds a public key for the authenticated user.
  64. //
  65. // GitHub API docs: https://developer.github.com/v3/users/keys/#create-a-public-key
  66. func (s *UsersService) CreateKey(ctx context.Context, key *Key) (*Key, *Response, error) {
  67. u := "user/keys"
  68. req, err := s.client.NewRequest("POST", u, key)
  69. if err != nil {
  70. return nil, nil, err
  71. }
  72. k := new(Key)
  73. resp, err := s.client.Do(ctx, req, k)
  74. if err != nil {
  75. return nil, resp, err
  76. }
  77. return k, resp, nil
  78. }
  79. // DeleteKey deletes a public key.
  80. //
  81. // GitHub API docs: https://developer.github.com/v3/users/keys/#delete-a-public-key
  82. func (s *UsersService) DeleteKey(ctx context.Context, id int64) (*Response, error) {
  83. u := fmt.Sprintf("user/keys/%v", id)
  84. req, err := s.client.NewRequest("DELETE", u, nil)
  85. if err != nil {
  86. return nil, err
  87. }
  88. return s.client.Do(ctx, req, nil)
  89. }