2019-04-25 13:06:53 -04:00
|
|
|
// Copyright 2017 Gitea 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"
|
|
|
|
)
|
|
|
|
|
|
|
|
// GPGKey a user GPG key to sign commit and tag in repository
|
|
|
|
type GPGKey struct {
|
|
|
|
ID int64 `json:"id"`
|
|
|
|
PrimaryKeyID string `json:"primary_key_id"`
|
|
|
|
KeyID string `json:"key_id"`
|
|
|
|
PublicKey string `json:"public_key"`
|
|
|
|
Emails []*GPGKeyEmail `json:"emails"`
|
|
|
|
SubsKey []*GPGKey `json:"subkeys"`
|
|
|
|
CanSign bool `json:"can_sign"`
|
|
|
|
CanEncryptComms bool `json:"can_encrypt_comms"`
|
|
|
|
CanEncryptStorage bool `json:"can_encrypt_storage"`
|
|
|
|
CanCertify bool `json:"can_certify"`
|
2019-10-13 11:13:33 -04:00
|
|
|
Created time.Time `json:"created_at,omitempty"`
|
|
|
|
Expires time.Time `json:"expires_at,omitempty"`
|
2019-04-25 13:06:53 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// GPGKeyEmail an email attached to a GPGKey
|
|
|
|
type GPGKeyEmail struct {
|
|
|
|
Email string `json:"email"`
|
|
|
|
Verified bool `json:"verified"`
|
|
|
|
}
|
|
|
|
|
2020-05-27 09:48:09 -04:00
|
|
|
// ListGPGKeysOptions options for listing a user's GPGKeys
|
|
|
|
type ListGPGKeysOptions struct {
|
|
|
|
ListOptions
|
|
|
|
}
|
|
|
|
|
2019-04-25 13:06:53 -04:00
|
|
|
// ListGPGKeys list all the GPG keys of the user
|
2020-09-15 22:01:41 -04:00
|
|
|
func (c *Client) ListGPGKeys(user string, opt ListGPGKeysOptions) ([]*GPGKey, *Response, error) {
|
2020-05-27 09:48:09 -04:00
|
|
|
opt.setDefaults()
|
|
|
|
keys := make([]*GPGKey, 0, opt.PageSize)
|
2020-09-15 22:01:41 -04:00
|
|
|
resp, err := c.getParsedResponse("GET", fmt.Sprintf("/users/%s/gpg_keys?%s", user, opt.getURLQuery().Encode()), nil, nil, &keys)
|
|
|
|
return keys, resp, err
|
2019-04-25 13:06:53 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// ListMyGPGKeys list all the GPG keys of current user
|
2020-09-15 22:01:41 -04:00
|
|
|
func (c *Client) ListMyGPGKeys(opt *ListGPGKeysOptions) ([]*GPGKey, *Response, error) {
|
2020-05-27 09:48:09 -04:00
|
|
|
opt.setDefaults()
|
|
|
|
keys := make([]*GPGKey, 0, opt.PageSize)
|
2020-09-15 22:01:41 -04:00
|
|
|
resp, err := c.getParsedResponse("GET", fmt.Sprintf("/user/gpg_keys?%s", opt.getURLQuery().Encode()), nil, nil, &keys)
|
|
|
|
return keys, resp, err
|
2019-04-25 13:06:53 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// GetGPGKey get current user's GPG key by key id
|
2020-09-15 22:01:41 -04:00
|
|
|
func (c *Client) GetGPGKey(keyID int64) (*GPGKey, *Response, error) {
|
2019-04-25 13:06:53 -04:00
|
|
|
key := new(GPGKey)
|
2020-09-15 22:01:41 -04:00
|
|
|
resp, err := c.getParsedResponse("GET", fmt.Sprintf("/user/gpg_keys/%d", keyID), nil, nil, &key)
|
|
|
|
return key, resp, err
|
2019-04-25 13:06:53 -04:00
|
|
|
}
|
|
|
|
|
2019-10-13 11:13:33 -04:00
|
|
|
// CreateGPGKeyOption options create user GPG key
|
|
|
|
type CreateGPGKeyOption struct {
|
|
|
|
// An armored GPG key to add
|
|
|
|
//
|
|
|
|
ArmoredKey string `json:"armored_public_key"`
|
|
|
|
}
|
|
|
|
|
2019-04-25 13:06:53 -04:00
|
|
|
// CreateGPGKey create GPG key with options
|
2020-09-15 22:01:41 -04:00
|
|
|
func (c *Client) CreateGPGKey(opt CreateGPGKeyOption) (*GPGKey, *Response, error) {
|
2019-04-25 13:06:53 -04:00
|
|
|
body, err := json.Marshal(&opt)
|
|
|
|
if err != nil {
|
2020-09-15 22:01:41 -04:00
|
|
|
return nil, nil, err
|
2019-04-25 13:06:53 -04:00
|
|
|
}
|
|
|
|
key := new(GPGKey)
|
2020-09-15 22:01:41 -04:00
|
|
|
resp, err := c.getParsedResponse("POST", "/user/gpg_keys", jsonHeader, bytes.NewReader(body), key)
|
|
|
|
return key, resp, err
|
2019-04-25 13:06:53 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// DeleteGPGKey delete GPG key with key id
|
2020-09-15 22:01:41 -04:00
|
|
|
func (c *Client) DeleteGPGKey(keyID int64) (*Response, error) {
|
|
|
|
_, resp, err := c.getResponse("DELETE", fmt.Sprintf("/user/gpg_keys/%d", keyID), nil, nil)
|
|
|
|
return resp, err
|
2019-04-25 13:06:53 -04:00
|
|
|
}
|