Skip to content
This repository has been archived by the owner on Jul 14, 2022. It is now read-only.

Commit

Permalink
✨Added Support for Basic Auth
Browse files Browse the repository at this point in the history
  • Loading branch information
athul committed Jan 21, 2020
1 parent 6238bb1 commit 0db5a44
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 19 deletions.
9 changes: 8 additions & 1 deletion cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,12 @@ func main() {
},
}
bauthFlags := []cli.Flag{
cli.StringFlag{
Name: "url",
Value: "https://reqres.in/api/users",
Usage: "The URL/Endpoint you want to check",
Required: true,
},
cli.StringFlag{
Name: "u",
Value: "Username",
Expand Down Expand Up @@ -56,9 +62,10 @@ func main() {
},
{
Name: "bauth",
Usage: "Send Request with Basic Auth",
Usage: "Send GET Request with Basic Auth(Don't Use get command with this)",
Flags: bauthFlags,
Action: func(c *cli.Context) error {
mets.Getbasic(c)
return nil
},
},
Expand Down
42 changes: 24 additions & 18 deletions methods/get.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package methods

import (
"encoding/base64"
"fmt"
"log"
"net/http"
Expand Down Expand Up @@ -29,7 +30,7 @@ func Getreq(c *cli.Context) error {
return nil
}

//Getwtoken send a get request with the Token for Auth
//Getwtoken send a get request with the Token for Authorization Header
func Getwtoken(c *cli.Context) error {
var url = c.String("url")
var bearer = "Bearer " + c.String("token")
Expand All @@ -49,23 +50,28 @@ func Getwtoken(c *cli.Context) error {
return nil
}

//Getbasic send a request with Baic Auth
//Getbasic helps you send a request with Basic Auth as Authorization Method
func Getbasic(c *cli.Context) error {
un := c.String("u")
pw := c.String("p")
url := c.String("url")
req, err := http.NewRequest("GET", url, nil)
req.Header.Add("Authorization", "Basic "+basicAuth(un, pw))
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
log.Println("Error on response.\n[ERRO] -", err)
}
s := Formatresp(resp)
if s != "" {
fmt.Printf("\n\n %s", s)
} else {
fmt.Print(resp)
}

return nil
}

// Dummy Code
/* func basicAuth() string {
var username string = "foo"
var passwd string = "bar"
client := &http.Client{}
req, err := http.NewRequest("GET", "mydomain.com", nil)
req.SetBasicAuth(username, passwd)
resp, err := client.Do(req)
if err != nil{
log.Fatal(err)
}
bodyText, err := ioutil.ReadAll(resp.Body)
s := string(bodyText)
return s
} */
func basicAuth(username, password string) string {
auth := username + ":" + password
return base64.StdEncoding.EncodeToString([]byte(auth))
}

0 comments on commit 0db5a44

Please sign in to comment.