github.com/jchengjr77/canaveral@v1.0.1-0.20200715160102-ea9245d1a2fb/gh/ghcredhandler.go (about) 1 package github 2 3 import ( 4 "encoding/json" 5 "errors" 6 "fmt" 7 "io/ioutil" 8 "net/http" 9 "strings" 10 "syscall" 11 12 "github.com/jchengjr77/canaveral/lib" 13 "github.com/jchengjr77/canaveral/nativestore" 14 15 "golang.org/x/crypto/ssh/terminal" 16 ) 17 18 type goodResponse struct { 19 Login string 20 } 21 22 // Checks that user auth token corresponds to a valid auth token and matches 23 // the username passed in 24 func verifyCreds(usr, secret string) error { 25 var goodFill []goodResponse 26 27 usrURL := url + "/user" 28 request, reqErr := http.NewRequest("GET", usrURL, nil) 29 if reqErr != nil { 30 return reqErr 31 } 32 request.Header.Set("Authorization", "token "+secret) 33 34 response, respErr := http.DefaultClient.Do(request) 35 if respErr != nil { 36 return respErr 37 } 38 defer response.Body.Close() 39 40 responseJSONData, readErr := ioutil.ReadAll(response.Body) 41 if readErr != nil { 42 return readErr 43 } 44 45 start := make([]byte, 1) 46 last := make([]byte, 1) 47 start[0] = '[' 48 last[0] = ']' 49 toDecode := append(start, append(responseJSONData, last...)...) 50 51 err := json.Unmarshal(toDecode, &goodFill) 52 if goodFill[0].Login == "" { 53 return errors.New("Failed to authenticate token") 54 } else if err == nil { 55 if strings.ToLower(usr) == strings.ToLower(goodFill[0].Login) { 56 return nil 57 } 58 return errors.New("Token didn't correspond to username") 59 } 60 return err 61 } 62 63 // GHAddWrapper wraps the addGHCredsHandler function, taking in a username 64 // and securely reading the personal auth token 65 func GHAddWrapper() error { 66 fmt.Print("Enter username: ") 67 var username string 68 fmt.Scan(&username) 69 fmt.Print("Enter Personal Auth Token: ") 70 byteToken, err := terminal.ReadPassword(int(syscall.Stdin)) 71 if err == nil { 72 token := string(byteToken) 73 fmt.Print("\r\n") 74 verifyErr := verifyCreds(username, token) 75 if verifyErr == nil { 76 return addGHCredsHandler(username, token) 77 } 78 return verifyErr 79 80 } 81 return err 82 } 83 84 // addGHCredsHandler takes in a github username and password and stores them 85 // ? Implement a no-password version of this perhaps? 86 // * tested 87 func addGHCredsHandler(username, secret string) error { 88 if username == "" { 89 fmt.Println("A github username is required. Please provide one.") 90 return nil 91 } else if secret == "" { 92 fmt.Println("A github personal auth token is required. Please provide one.") 93 return nil 94 } else { 95 fmt.Printf("Adding github account: %s\n", username) 96 return nativestore.SetCreds(label, url, username, secret) 97 } 98 } 99 100 // RemGHCredsHandler removes github credentials from native storage 101 // * wraps a tested function 102 func RemGHCredsHandler() error { 103 fmt.Println("Removing github from canaveral.") 104 return nativestore.DeleteCreds(label, url) 105 } 106 107 // Checks whether or not the user has github credentials set 108 // * wraps a tested function 109 func ghCredsExist() bool { 110 fmt.Println("Checking whether or not github credentials have been added.") 111 _, _, err := nativestore.FetchCreds(label, url) 112 return (err == nil) 113 } 114 115 // PrintGHUser prints the natively stored github username to the commandline 116 // * wraps a tested function 117 func PrintGHUser() { 118 if ghCredsExist() { 119 usr, _, err := nativestore.FetchCreds(label, url) 120 lib.Check(err) 121 fmt.Println(usr) 122 return 123 } 124 fmt.Println("-no github username stored-") 125 }