|
| 1 | +// Copyright 2017 Gitea 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 | + |
| 5 | +package gitea |
| 6 | + |
| 7 | +import ( |
| 8 | + "bytes" |
| 9 | + "encoding/json" |
| 10 | + "fmt" |
| 11 | + "time" |
| 12 | +) |
| 13 | + |
| 14 | +// GPGKey a user GPG key to sign commit and tag in repository |
| 15 | +type GPGKey struct { |
| 16 | + ID int64 `json:"id"` |
| 17 | + PrimaryKeyID string `json:"primary_key_id"` |
| 18 | + KeyID string `json:"key_id"` |
| 19 | + PublicKey string `json:"public_key"` |
| 20 | + Emails []*GPGKeyEmail `json:"emails"` |
| 21 | + SubsKey []*GPGKey `json:"subkeys"` |
| 22 | + CanSign bool `json:"can_sign"` |
| 23 | + CanEncryptComms bool `json:"can_encrypt_comms"` |
| 24 | + CanEncryptStorage bool `json:"can_encrypt_storage"` |
| 25 | + CanCertify bool `json:"can_certify"` |
| 26 | + Created time.Time `json:"created_at,omitempty"` |
| 27 | + Expires time.Time `json:"expires_at,omitempty"` |
| 28 | +} |
| 29 | + |
| 30 | +// GPGKeyEmail a email attache to a GPGKey |
| 31 | +type GPGKeyEmail struct { |
| 32 | + Email string `json:"email"` |
| 33 | + Verified bool `json:"verified"` |
| 34 | +} |
| 35 | + |
| 36 | +// CreateGPGKeyOption options create user GPG key |
| 37 | +type CreateGPGKeyOption struct { |
| 38 | + ArmoredKey string `json:"armored_public_key" binding:"Required"` |
| 39 | +} |
| 40 | + |
| 41 | +// ListMyGPGKeys list all the GPG keys of current user |
| 42 | +func (c *Client) ListMyGPGKeys() ([]*GPGKey, error) { |
| 43 | + keys := make([]*GPGKey, 0, 10) |
| 44 | + return keys, c.getParsedResponse("GET", "/user/gpg_keys", nil, nil, &keys) |
| 45 | +} |
| 46 | + |
| 47 | +// GetGPGKey get current user's GPG key by key id |
| 48 | +func (c *Client) GetGPGKey(keyID int64) (*GPGKey, error) { |
| 49 | + key := new(GPGKey) |
| 50 | + return key, c.getParsedResponse("GET", fmt.Sprintf("/user/gpg_keys/%d", keyID), nil, nil, &key) |
| 51 | +} |
| 52 | + |
| 53 | +// CreateGPGKey create GPG key with options |
| 54 | +func (c *Client) CreateGPGKey(opt CreateGPGKeyOption) (*GPGKey, error) { |
| 55 | + body, err := json.Marshal(&opt) |
| 56 | + if err != nil { |
| 57 | + return nil, err |
| 58 | + } |
| 59 | + key := new(GPGKey) |
| 60 | + return key, c.getParsedResponse("POST", "/user/gpg_keys", jsonHeader, bytes.NewReader(body), key) |
| 61 | +} |
| 62 | + |
| 63 | +// DeleteGPGKey delete GPG key with key id |
| 64 | +func (c *Client) DeleteGPGKey(keyID int64) error { |
| 65 | + _, err := c.getResponse("DELETE", fmt.Sprintf("/user/gpg_keys/%d", keyID), nil, nil) |
| 66 | + return err |
| 67 | +} |
0 commit comments