1 // 2 // Copyright 2021, Patrick Webster 3 // 4 // Licensed under the Apache License, Version 2.0 (the "License"); 5 // you may not use this file except in compliance with the License. 6 // You may obtain a copy of the License at 7 // 8 // http://www.apache.org/licenses/LICENSE-2.0 9 // 10 // Unless required by applicable law or agreed to in writing, software 11 // distributed under the License is distributed on an "AS IS" BASIS, 12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 // See the License for the specific language governing permissions and 14 // limitations under the License. 15 // 16 17 package gitlab 18 19 import ( 20 "fmt" 21 "net/http" 22 "time" 23 ) 24 25 // KeysService handles communication with the 26 // keys related methods of the GitLab API. 27 // 28 // GitLab API docs: 29 // https://docs.gitlab.com/ee/api/keys.html 30 type KeysService struct { 31 client *Client 32 } 33 34 // Key represents a GitLab user's SSH key. 35 // 36 // GitLab API docs: 37 // https://docs.gitlab.com/ee/api/keys.html 38 type Key struct { 39 ID int `json:"id"` 40 Title string `json:"title"` 41 Key string `json:"key"` 42 CreatedAt *time.Time `json:"created_at"` 43 User User `json:"user"` 44 } 45 46 // GetKeyWithUser gets a single key by id along with the associated 47 // user information. 48 // 49 // GitLab API docs: 50 // https://docs.gitlab.com/ee/api/keys.html#get-ssh-key-with-user-by-id-of-an-ssh-key 51 func (s *KeysService) GetKeyWithUser(key int, options ...RequestOptionFunc) (*Key, *Response, error) { 52 u := fmt.Sprintf("keys/%d", key) 53 54 req, err := s.client.NewRequest(http.MethodGet, u, nil, options) 55 if err != nil { 56 return nil, nil, err 57 } 58 59 k := new(Key) 60 resp, err := s.client.Do(req, k) 61 if err != nil { 62 return nil, resp, err 63 } 64 65 return k, resp, nil 66 } 67 68 // GetKeyByFingerprintOptions represents the available GetKeyByFingerprint() 69 // options. 70 // 71 // GitLab API docs: 72 // https://docs.gitlab.com/ee/api/keys.html#get-user-by-fingerprint-of-ssh-key 73 // https://docs.gitlab.com/ee/api/keys.html#get-user-by-deploy-key-fingerprint 74 type GetKeyByFingerprintOptions struct { 75 Fingerprint string `url:"fingerprint" json:"fingerprint"` 76 } 77 78 // GetKeyByFingerprint gets a specific SSH key or deploy key by fingerprint 79 // along with the associated user information. 80 // 81 // GitLab API docs: 82 // https://docs.gitlab.com/ee/api/keys.html#get-user-by-fingerprint-of-ssh-key 83 // https://docs.gitlab.com/ee/api/keys.html#get-user-by-deploy-key-fingerprint 84 func (s *KeysService) GetKeyByFingerprint(opt *GetKeyByFingerprintOptions, options ...RequestOptionFunc) (*Key, *Response, error) { 85 req, err := s.client.NewRequest(http.MethodGet, "keys", opt, options) 86 if err != nil { 87 return nil, nil, err 88 } 89 90 k := new(Key) 91 resp, err := s.client.Do(req, k) 92 if err != nil { 93 return nil, resp, err 94 } 95 96 return k, resp, nil 97 } 98