From 739613d9dccf2cd92ac13004b4d2cc7d5f3e7cd2 Mon Sep 17 00:00:00 2001 From: "Suriya.S" Date: Sat, 1 Aug 2020 11:08:30 +0700 Subject: [PATCH 1/3] 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 { From 07a4386c3d1bf0cbd3058a82efede6c4fe036744 Mon Sep 17 00:00:00 2001 From: "Suriya.S" Date: Sat, 1 Aug 2020 11:10:34 +0700 Subject: [PATCH 2/3] Remove go version in go.mod --- go.mod | 2 -- 1 file changed, 2 deletions(-) diff --git a/go.mod b/go.mod index edbd7e7..d3e57ca 100644 --- a/go.mod +++ b/go.mod @@ -1,7 +1,5 @@ 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 From b2d83444d10dea154be871a77cf7fd8f7fd66153 Mon Sep 17 00:00:00 2001 From: "Suriya.S" Date: Sun, 2 Aug 2020 00:35:29 +0700 Subject: [PATCH 3/3] Change token to token_value and add token_type for support other token type --- main.go | 19 +++++++++++++------ plugin.go | 8 ++++---- 2 files changed, 17 insertions(+), 10 deletions(-) diff --git a/main.go b/main.go index 9eef863..45ff3dc 100644 --- a/main.go +++ b/main.go @@ -34,11 +34,17 @@ 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: "token-value", + Usage: "token value", + EnvVar: "PLUGIN_TOKEN_VALUE,WEBHOOK_TOKEN_VALUE", + }, + cli.StringFlag{ + Name: "token-type", + Usage: "type of token", + EnvVar: "PLUGIN_TOKEN_TYPE,WEBHOOK_TOKEN_TYPE", + Value: "Bearer", + }, cli.StringFlag{ Name: "content-type", Usage: "content type", @@ -205,7 +211,8 @@ func run(c *cli.Context) error { Method: c.String("method"), Username: c.String("username"), Password: c.String("password"), - Token: c.String("token"), + TokenValue: c.String("token-value"), + TokenType: c.String("token-type"), ContentType: c.String("content-type"), Template: c.String("template"), Headers: c.StringSlice("headers"), diff --git a/plugin.go b/plugin.go index 9aa6e0b..a74d7f5 100644 --- a/plugin.go +++ b/plugin.go @@ -43,7 +43,8 @@ type ( Method string Username string Password string - Token string + TokenValue string + TokenType string ContentType string Template string Headers []string @@ -142,11 +143,10 @@ 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)) + if p.Config.TokenValue != "" { + req.Header.Set("Authorization", fmt.Sprintf("%s %s", p.Config.TokenType, p.Config.TokenValue)) } - client := http.DefaultClient if p.Config.SkipVerify {