...

Source file src/github.com/google/go-github/v33/github/users_gpg_keys.go

Documentation: github.com/google/go-github/v33/github

     1  // Copyright 2016 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  
     6  package github
     7  
     8  import (
     9  	"context"
    10  	"fmt"
    11  	"time"
    12  )
    13  
    14  // GPGKey represents a GitHub user's public GPG key used to verify GPG signed commits and tags.
    15  //
    16  // https://developer.github.com/changes/2016-04-04-git-signing-api-preview/
    17  type GPGKey struct {
    18  	ID                *int64      `json:"id,omitempty"`
    19  	PrimaryKeyID      *int64      `json:"primary_key_id,omitempty"`
    20  	KeyID             *string     `json:"key_id,omitempty"`
    21  	PublicKey         *string     `json:"public_key,omitempty"`
    22  	Emails            []*GPGEmail `json:"emails,omitempty"`
    23  	Subkeys           []*GPGKey   `json:"subkeys,omitempty"`
    24  	CanSign           *bool       `json:"can_sign,omitempty"`
    25  	CanEncryptComms   *bool       `json:"can_encrypt_comms,omitempty"`
    26  	CanEncryptStorage *bool       `json:"can_encrypt_storage,omitempty"`
    27  	CanCertify        *bool       `json:"can_certify,omitempty"`
    28  	CreatedAt         *time.Time  `json:"created_at,omitempty"`
    29  	ExpiresAt         *time.Time  `json:"expires_at,omitempty"`
    30  }
    31  
    32  // String stringifies a GPGKey.
    33  func (k GPGKey) String() string {
    34  	return Stringify(k)
    35  }
    36  
    37  // GPGEmail represents an email address associated to a GPG key.
    38  type GPGEmail struct {
    39  	Email    *string `json:"email,omitempty"`
    40  	Verified *bool   `json:"verified,omitempty"`
    41  }
    42  
    43  // ListGPGKeys lists the public GPG keys for a user. Passing the empty
    44  // string will fetch keys for the authenticated user. It requires authentication
    45  // via Basic Auth or via OAuth with at least read:gpg_key scope.
    46  //
    47  // GitHub API docs: https://docs.github.com/en/free-pro-team@latest/rest/reference/users/#list-gpg-keys-for-the-authenticated-user
    48  // GitHub API docs: https://docs.github.com/en/free-pro-team@latest/rest/reference/users/#list-gpg-keys-for-a-user
    49  func (s *UsersService) ListGPGKeys(ctx context.Context, user string, opts *ListOptions) ([]*GPGKey, *Response, error) {
    50  	var u string
    51  	if user != "" {
    52  		u = fmt.Sprintf("users/%v/gpg_keys", user)
    53  	} else {
    54  		u = "user/gpg_keys"
    55  	}
    56  	u, err := addOptions(u, opts)
    57  	if err != nil {
    58  		return nil, nil, err
    59  	}
    60  
    61  	req, err := s.client.NewRequest("GET", u, nil)
    62  	if err != nil {
    63  		return nil, nil, err
    64  	}
    65  
    66  	var keys []*GPGKey
    67  	resp, err := s.client.Do(ctx, req, &keys)
    68  	if err != nil {
    69  		return nil, resp, err
    70  	}
    71  
    72  	return keys, resp, nil
    73  }
    74  
    75  // GetGPGKey gets extended details for a single GPG key. It requires authentication
    76  // via Basic Auth or via OAuth with at least read:gpg_key scope.
    77  //
    78  // GitHub API docs: https://docs.github.com/en/free-pro-team@latest/rest/reference/users/#get-a-gpg-key-for-the-authenticated-user
    79  func (s *UsersService) GetGPGKey(ctx context.Context, id int64) (*GPGKey, *Response, error) {
    80  	u := fmt.Sprintf("user/gpg_keys/%v", id)
    81  	req, err := s.client.NewRequest("GET", u, nil)
    82  	if err != nil {
    83  		return nil, nil, err
    84  	}
    85  
    86  	key := &GPGKey{}
    87  	resp, err := s.client.Do(ctx, req, key)
    88  	if err != nil {
    89  		return nil, resp, err
    90  	}
    91  
    92  	return key, resp, nil
    93  }
    94  
    95  // CreateGPGKey creates a GPG key. It requires authenticatation via Basic Auth
    96  // or OAuth with at least write:gpg_key scope.
    97  //
    98  // GitHub API docs: https://docs.github.com/en/free-pro-team@latest/rest/reference/users/#create-a-gpg-key
    99  func (s *UsersService) CreateGPGKey(ctx context.Context, armoredPublicKey string) (*GPGKey, *Response, error) {
   100  	gpgKey := &struct {
   101  		ArmoredPublicKey string `json:"armored_public_key"`
   102  	}{ArmoredPublicKey: armoredPublicKey}
   103  	req, err := s.client.NewRequest("POST", "user/gpg_keys", gpgKey)
   104  	if err != nil {
   105  		return nil, nil, err
   106  	}
   107  
   108  	key := &GPGKey{}
   109  	resp, err := s.client.Do(ctx, req, key)
   110  	if err != nil {
   111  		return nil, resp, err
   112  	}
   113  
   114  	return key, resp, nil
   115  }
   116  
   117  // DeleteGPGKey deletes a GPG key. It requires authentication via Basic Auth or
   118  // via OAuth with at least admin:gpg_key scope.
   119  //
   120  // GitHub API docs: https://docs.github.com/en/free-pro-team@latest/rest/reference/users/#delete-a-gpg-key-for-the-authenticated-user
   121  func (s *UsersService) DeleteGPGKey(ctx context.Context, id int64) (*Response, error) {
   122  	u := fmt.Sprintf("user/gpg_keys/%v", id)
   123  	req, err := s.client.NewRequest("DELETE", u, nil)
   124  	if err != nil {
   125  		return nil, err
   126  	}
   127  
   128  	return s.client.Do(ctx, req, nil)
   129  }
   130  

View as plain text