You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

user_key.go 1.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. // Copyright 2015 The Gogs Authors. All rights reserved.
  2. // Use of this source code is governed by a MIT-style
  3. // license that can be found in the LICENSE file.
  4. package gitea
  5. import (
  6. "bytes"
  7. "encoding/json"
  8. "fmt"
  9. "time"
  10. )
  11. // PublicKey publickey is a user key to push code to repository
  12. type PublicKey struct {
  13. ID int64 `json:"id"`
  14. Key string `json:"key"`
  15. URL string `json:"url,omitempty"`
  16. Title string `json:"title,omitempty"`
  17. Fingerprint string `json:"fingerprint,omitempty"`
  18. // swagger:strfmt date-time
  19. Created time.Time `json:"created_at,omitempty"`
  20. Owner *User `json:"user,omitempty"`
  21. ReadOnly bool `json:"read_only,omitempty"`
  22. KeyType string `json:"key_type,omitempty"`
  23. }
  24. // ListPublicKeys list all the public keys of the user
  25. func (c *Client) ListPublicKeys(user string) ([]*PublicKey, error) {
  26. keys := make([]*PublicKey, 0, 10)
  27. return keys, c.getParsedResponse("GET", fmt.Sprintf("/users/%s/keys", user), nil, nil, &keys)
  28. }
  29. // ListMyPublicKeys list all the public keys of current user
  30. func (c *Client) ListMyPublicKeys() ([]*PublicKey, error) {
  31. keys := make([]*PublicKey, 0, 10)
  32. return keys, c.getParsedResponse("GET", "/user/keys", nil, nil, &keys)
  33. }
  34. // GetPublicKey get current user's public key by key id
  35. func (c *Client) GetPublicKey(keyID int64) (*PublicKey, error) {
  36. key := new(PublicKey)
  37. return key, c.getParsedResponse("GET", fmt.Sprintf("/user/keys/%d", keyID), nil, nil, &key)
  38. }
  39. // CreatePublicKey create public key with options
  40. func (c *Client) CreatePublicKey(opt CreateKeyOption) (*PublicKey, error) {
  41. body, err := json.Marshal(&opt)
  42. if err != nil {
  43. return nil, err
  44. }
  45. key := new(PublicKey)
  46. return key, c.getParsedResponse("POST", "/user/keys", jsonHeader, bytes.NewReader(body), key)
  47. }
  48. // DeletePublicKey delete public key with key id
  49. func (c *Client) DeletePublicKey(keyID int64) error {
  50. _, err := c.getResponse("DELETE", fmt.Sprintf("/user/keys/%d", keyID), nil, nil)
  51. return err
  52. }