gitee.com/mysnapcore/mysnapd@v0.1.0/store/userinfo.go (about) 1 // -*- Mode: Go; indent-tabs-mode: t -*- 2 3 /* 4 * Copyright (C) 2016 Canonical Ltd 5 * 6 * This program is free software: you can redistribute it and/or modify 7 * it under the terms of the GNU General Public License version 3 as 8 * published by the Free Software Foundation. 9 * 10 * This program is distributed in the hope that it will be useful, 11 * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 * GNU General Public License for more details. 14 * 15 * You should have received a copy of the GNU General Public License 16 * along with this program. If not, see <http://www.gnu.org/licenses/>. 17 * 18 */ 19 20 package store 21 22 import ( 23 "encoding/json" 24 "fmt" 25 "net/http" 26 "net/url" 27 28 "gitee.com/mysnapcore/mysnapd/httputil" 29 ) 30 31 type keysReply struct { 32 Username string `json:"username"` 33 SSHKeys []string `json:"ssh_keys"` 34 OpenIDIdentifier string `json:"openid_identifier"` 35 } 36 37 type User struct { 38 Username string 39 SSHKeys []string 40 OpenIDIdentifier string 41 } 42 43 func (s *Store) UserInfo(email string) (userinfo *User, err error) { 44 var v keysReply 45 ssourl := fmt.Sprintf("%s/keys/%s", authURL(), url.QueryEscape(email)) 46 47 resp, err := httputil.RetryRequest(ssourl, func() (*http.Response, error) { 48 return s.client.Get(ssourl) 49 }, func(resp *http.Response) error { 50 if resp.StatusCode != 200 { 51 // we recheck the status 52 return nil 53 } 54 dec := json.NewDecoder(resp.Body) 55 if err := dec.Decode(&v); err != nil { 56 return fmt.Errorf("cannot unmarshal: %v", err) 57 } 58 return nil 59 }, defaultRetryStrategy) 60 61 if err != nil { 62 return nil, err 63 } 64 65 switch resp.StatusCode { 66 case 200: // good 67 case 404: 68 return nil, fmt.Errorf("cannot find user %q", email) 69 default: 70 return nil, respToError(resp, fmt.Sprintf("look up user %q", email)) 71 } 72 73 return &User{ 74 Username: v.Username, 75 SSHKeys: v.SSHKeys, 76 OpenIDIdentifier: v.OpenIDIdentifier, 77 }, nil 78 }