From 5002af4e72169db4b700411b7fe5e16c4620b097 Mon Sep 17 00:00:00 2001 From: Andy Grunwald Date: Wed, 19 Oct 2022 21:16:31 +0200 Subject: [PATCH] Onpremise/Auth: Update README and examples for Bearer Auth Kudos goes to @stxphcodes for his work in https://github.com/andygrunwald/go-jira/pull/469 --- README.md | 17 ++++++++++++--- onpremise/examples/bearerauth/main.go | 30 +++++++++++++++++++++++++++ 2 files changed, 44 insertions(+), 3 deletions(-) create mode 100644 onpremise/examples/bearerauth/main.go diff --git a/README.md b/README.md index d11bfb57..f669301e 100644 --- a/README.md +++ b/README.md @@ -29,7 +29,7 @@ Latest stable release: [v1.16.0](https://github.com/andygrunwald/go-jira/release ## Features -* Authentication (HTTP Basic, OAuth, Session Cookie) +* Authentication (HTTP Basic, OAuth, Session Cookie, Bearer (for PATs)) * Create and retrieve issues * Create and retrieve issue transitions (status updates) * Call every API endpoint of the Jira, even if it is not directly implemented in this library @@ -92,7 +92,7 @@ func main() { ### Authentication The `go-jira` library does not handle most authentication directly. Instead, authentication should be handled within -an `http.Client`. That client can then be passed into the `NewClient` function when creating a jira client. +an `http.Client`. That client can then be passed into the `NewClient` function when creating a jira client. For convenience, capability for basic and cookie-based authentication is included in the main library. @@ -118,11 +118,20 @@ func main() { } ``` +#### Bearer - Personal Access Tokens (self-hosted Jira) + +For **self-hosted Jira** (v8.14 and later), Personal Access Tokens (PATs) were introduced. +Similar to the API tokens, PATs are a safe alternative to using username and password for authentication with scripts and integrations. +PATs use the Bearer authentication scheme. +Read more about Jira PATs [here](https://confluence.atlassian.com/enterprise/using-personal-access-tokens-1026032365.html). + +See [examples/bearerauth](onpremise/examples/bearerauth/main.go) for how to use the Bearer authentication scheme with Jira in Go. + #### Basic (self-hosted Jira) Password-based API authentication works for self-hosted Jira **only**, and has been [deprecated for users of Atlassian Cloud](https://developer.atlassian.com/cloud/jira/platform/deprecation-notice-basic-auth-and-cookie-based-auth/). -The above token authentication example may be used, substituting a user's password for a generated token. +Depending on your version of Jira, either of the above token authentication examples may be used, substituting a user's password for a generated token. #### Authenticate with OAuth @@ -223,6 +232,7 @@ func main() { fmt.Printf("Status after transition: %+v\n", issue.Fields.Status.Name) } ``` + ### Get all the issues for JQL with Pagination Jira API has limit on maxResults it can return. You may have a usecase where you need to get all issues for given JQL. @@ -354,6 +364,7 @@ You can read more about them at https://blog.developer.atlassian.com/cloud-ecosy ## Releasing Install [standard-version](https://github.com/conventional-changelog/standard-version) + ```bash npm i -g standard-version ``` diff --git a/onpremise/examples/bearerauth/main.go b/onpremise/examples/bearerauth/main.go new file mode 100644 index 00000000..13054e30 --- /dev/null +++ b/onpremise/examples/bearerauth/main.go @@ -0,0 +1,30 @@ +package main + +import ( + "context" + "fmt" + + jira "github.com/andygrunwald/go-jira/onpremise" +) + +func main() { + jiraURL := "" + + // See "Using Personal Access Tokens" + // https://confluence.atlassian.com/enterprise/using-personal-access-tokens-1026032365.html + tp := jira.BearerAuthTransport{ + Token: "", + } + client, err := jira.NewClient(jiraURL, tp.Client()) + if err != nil { + panic(err) + } + + u, _, err := client.User.GetCurrentUser(context.Background()) + if err != nil { + panic(err) + } + + fmt.Printf("Email: %v\n", u.EmailAddress) + fmt.Println("Success!") +}