From 739613d9dccf2cd92ac13004b4d2cc7d5f3e7cd2 Mon Sep 17 00:00:00 2001 From: "Suriya.S" Date: Sat, 1 Aug 2020 11:08:30 +0700 Subject: [PATCH] Add Bearer token authen --- go.mod | 2 ++ main.go | 6 ++++++ plugin.go | 6 ++++++ 3 files changed, 14 insertions(+) diff --git a/go.mod b/go.mod index d3e57ca..edbd7e7 100644 --- a/go.mod +++ b/go.mod @@ -1,5 +1,7 @@ module github.com/drone-plugins/drone-webhook +go 1.14 + require ( bou.ke/monkey v1.0.1 // indirect github.com/aymerick/raymond v2.0.2+incompatible // indirect diff --git a/main.go b/main.go index 2995738..9eef863 100644 --- a/main.go +++ b/main.go @@ -34,6 +34,11 @@ func main() { Usage: "password for basic auth", EnvVar: "PLUGIN_PASSWORD,WEBHOOK_PASSWORD", }, + cli.StringFlag{ + Name: "token", + Usage: "token for Bearer token auth", + EnvVar: "PLUGIN_TOKEN,WEBHOOK_TOKEN", + }, cli.StringFlag{ Name: "content-type", Usage: "content type", @@ -200,6 +205,7 @@ func run(c *cli.Context) error { Method: c.String("method"), Username: c.String("username"), Password: c.String("password"), + Token: c.String("token"), ContentType: c.String("content-type"), Template: c.String("template"), Headers: c.StringSlice("headers"), diff --git a/plugin.go b/plugin.go index 0746429..9aa6e0b 100644 --- a/plugin.go +++ b/plugin.go @@ -43,6 +43,7 @@ type ( Method string Username string Password string + Token string ContentType string Template string Headers []string @@ -141,6 +142,11 @@ func (p Plugin) Exec() error { req.SetBasicAuth(p.Config.Username, p.Config.Password) } + if p.Config.Token != "" { + req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", p.Config.Token)) + } + + client := http.DefaultClient if p.Config.SkipVerify {