2016-11-07 08:53:13 -05:00
|
|
|
// Copyright 2015 The Gogs Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a MIT-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
package gitea
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
2016-11-29 03:09:17 -05:00
|
|
|
// PublicKey publickey is a user key to push code to repository
|
2016-11-07 08:53:13 -05:00
|
|
|
type PublicKey struct {
|
2017-11-28 10:21:39 -05:00
|
|
|
ID int64 `json:"id"`
|
|
|
|
Key string `json:"key"`
|
|
|
|
URL string `json:"url,omitempty"`
|
|
|
|
Title string `json:"title,omitempty"`
|
|
|
|
Fingerprint string `json:"fingerprint,omitempty"`
|
2017-11-13 02:02:25 -05:00
|
|
|
// swagger:strfmt date-time
|
2018-10-28 17:03:02 -05:00
|
|
|
Created time.Time `json:"created_at,omitempty"`
|
|
|
|
Owner *User `json:"user,omitempty"`
|
|
|
|
ReadOnly bool `json:"read_only,omitempty"`
|
|
|
|
KeyType string `json:"key_type,omitempty"`
|
2016-11-07 08:53:13 -05:00
|
|
|
}
|
|
|
|
|
2016-11-29 03:09:17 -05:00
|
|
|
// ListPublicKeys list all the public keys of the user
|
2016-11-07 08:53:13 -05:00
|
|
|
func (c *Client) ListPublicKeys(user string) ([]*PublicKey, error) {
|
|
|
|
keys := make([]*PublicKey, 0, 10)
|
|
|
|
return keys, c.getParsedResponse("GET", fmt.Sprintf("/users/%s/keys", user), nil, nil, &keys)
|
|
|
|
}
|
|
|
|
|
2016-11-29 03:09:17 -05:00
|
|
|
// ListMyPublicKeys list all the public keys of current user
|
2016-11-07 08:53:13 -05:00
|
|
|
func (c *Client) ListMyPublicKeys() ([]*PublicKey, error) {
|
|
|
|
keys := make([]*PublicKey, 0, 10)
|
|
|
|
return keys, c.getParsedResponse("GET", "/user/keys", nil, nil, &keys)
|
|
|
|
}
|
|
|
|
|
2016-11-29 03:09:17 -05:00
|
|
|
// GetPublicKey get current user's public key by key id
|
2016-11-07 08:53:13 -05:00
|
|
|
func (c *Client) GetPublicKey(keyID int64) (*PublicKey, error) {
|
|
|
|
key := new(PublicKey)
|
|
|
|
return key, c.getParsedResponse("GET", fmt.Sprintf("/user/keys/%d", keyID), nil, nil, &key)
|
|
|
|
}
|
|
|
|
|
2016-11-29 03:09:17 -05:00
|
|
|
// CreatePublicKey create public key with options
|
2016-11-07 08:53:13 -05:00
|
|
|
func (c *Client) CreatePublicKey(opt CreateKeyOption) (*PublicKey, error) {
|
|
|
|
body, err := json.Marshal(&opt)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
key := new(PublicKey)
|
|
|
|
return key, c.getParsedResponse("POST", "/user/keys", jsonHeader, bytes.NewReader(body), key)
|
|
|
|
}
|
|
|
|
|
2016-11-29 03:09:17 -05:00
|
|
|
// DeletePublicKey delete public key with key id
|
2016-11-07 08:53:13 -05:00
|
|
|
func (c *Client) DeletePublicKey(keyID int64) error {
|
|
|
|
_, err := c.getResponse("DELETE", fmt.Sprintf("/user/keys/%d", keyID), nil, nil)
|
|
|
|
return err
|
|
|
|
}
|