Skip to content

Commit

Permalink
Onpremise/Auth: Update README and examples for Bearer Auth
Browse files Browse the repository at this point in the history
Kudos goes to @stxphcodes for his work in
#469
  • Loading branch information
andygrunwald committed Oct 19, 2022
1 parent b835d6c commit 5002af4
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 3 deletions.
17 changes: 14 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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.

Expand All @@ -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

Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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
```
Expand Down
30 changes: 30 additions & 0 deletions onpremise/examples/bearerauth/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package main

import (
"context"
"fmt"

jira "github.com/andygrunwald/go-jira/onpremise"
)

func main() {
jiraURL := "<your-jira-instance>"

// See "Using Personal Access Tokens"
// https://confluence.atlassian.com/enterprise/using-personal-access-tokens-1026032365.html
tp := jira.BearerAuthTransport{
Token: "<persona-access-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!")
}

0 comments on commit 5002af4

Please sign in to comment.