Skip to content

Commit 32a57fd

Browse files
authored
Merge pull request #580 from andygrunwald/issue-579
Cloud/Authentication: Removed `BearerAuthTransport`, Removed `PATAuthTransport`, Rename `BasicAuthTransport.Password` to `BasicAuthTransport.APIToken`
2 parents ff38af3 + d86ceae commit 32a57fd

File tree

14 files changed

+111
-182
lines changed

14 files changed

+111
-182
lines changed

CHANGELOG.md

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -326,6 +326,60 @@ After:
326326
client.Project.GetAll(ctx, &GetQueryOptions{})
327327
```
328328

329+
#### Cloud/Authentication: `BearerAuthTransport` removed, `PATAuthTransport` removed
330+
331+
If you used `BearerAuthTransport` or `PATAuthTransport` for authentication, please replace it with `BasicAuthTransport`.
332+
333+
Before:
334+
335+
```go
336+
tp := jira.BearerAuthTransport{
337+
Token: "token",
338+
}
339+
client, err := jira.NewClient("https://...", tp.Client())
340+
```
341+
342+
or
343+
344+
```go
345+
tp := jira.PATAuthTransport{
346+
Token: "token",
347+
}
348+
client, err := jira.NewClient("https://...", tp.Client())
349+
```
350+
351+
After:
352+
353+
```go
354+
tp := jira.BasicAuthTransport{
355+
Username: "username",
356+
APIToken: "token",
357+
}
358+
client, err := jira.NewClient("https://...", tp.Client())
359+
```
360+
361+
#### Cloud/Authentication: `BasicAuthTransport.Password` was renamed to `BasicAuthTransport.APIToken`
362+
363+
Before:
364+
365+
```go
366+
tp := jira.BasicAuthTransport{
367+
Username: "username",
368+
Password: "token",
369+
}
370+
client, err := jira.NewClient("https://...", tp.Client())
371+
```
372+
373+
After:
374+
375+
```go
376+
tp := jira.BasicAuthTransport{
377+
Username: "username",
378+
APIToken: "token",
379+
}
380+
client, err := jira.NewClient("https://...", tp.Client())
381+
```
382+
329383
### Breaking changes
330384

331385
* Jira On-Premise and Jira Cloud have now different clients, because the API differs
@@ -338,6 +392,9 @@ client.Project.GetAll(ctx, &GetQueryOptions{})
338392
* `Issue.Update` has been removed and `Issue.UpdateWithOptions` has been renamed to `Issue.Update`
339393
* `Issue.GetCreateMeta` has been removed and `Issue.GetCreateMetaWithOptions` has been renamed to `Issue.GetCreateMeta`
340394
* `Project.GetList` has been removed and `Project.ListWithOptions` has been renamed to `Project.GetAll`
395+
* Cloud/Authentication: Removed `BearerAuthTransport`, because it was a (kind of) duplicate of `BasicAuthTransport`
396+
* Cloud/Authentication: Removed `PATAuthTransport`, because it was a (kind of) duplicate of `BasicAuthTransport`
397+
* Cloud/Authentication: `BasicAuthTransport.Password` was renamed to `BasicAuthTransport.APIToken`
341398

342399
### Features
343400

README.md

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -100,20 +100,21 @@ For convenience, capability for basic and cookie-based authentication is include
100100

101101
Token-based authentication uses the basic authentication scheme, with a user-generated API token in place of a user's password. You can generate a token for your user [here](https://id.atlassian.com/manage-profile/security/api-tokens). Additional information about Atlassian Cloud API tokens can be found [here](https://support.atlassian.com/atlassian-account/docs/manage-api-tokens-for-your-atlassian-account/).
102102

103-
A more thorough, [runnable example](cloud/examples/basicauth/main.go) is provided in the examples directory.
103+
A more thorough, [runnable example](cloud/examples/basic_auth/main.go) is provided in the examples directory.
104104

105105
```go
106106
func main() {
107107
tp := jira.BasicAuthTransport{
108-
Username: "username",
109-
Password: "token",
108+
Username: "<username>",
109+
APIToken: "<api-token>",
110110
}
111111

112-
client, err := jira.NewClient(tp.Client(), "https://my.jira.com")
112+
client, err := jira.NewClient("https://my.jira.com", tp.Client())
113113

114-
u, _, err := client.User.Get("some_user")
114+
u, _, err = client.User.GetCurrentUser(context.Background())
115115

116-
fmt.Printf("\nEmail: %v\nSuccess!\n", u.EmailAddress)
116+
fmt.Printf("Email: %v\n", u.EmailAddress)
117+
fmt.Println("Success!")
117118
}
118119
```
119120

cloud/auth_transport_basic.go renamed to cloud/auth_transport_basic_auth.go

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,22 +3,25 @@ package cloud
33
import "net/http"
44

55
// BasicAuthTransport is an http.RoundTripper that authenticates all requests
6-
// using HTTP Basic Authentication with the provided username and password.
6+
// using HTTP Basic Authentication with the provided username and a Personal API Token.
7+
//
8+
// Jira docs: https://support.atlassian.com/atlassian-account/docs/manage-api-tokens-for-your-atlassian-account/
9+
// Create a token: https://id.atlassian.com/manage-profile/security/api-tokens
710
type BasicAuthTransport struct {
811
Username string
9-
Password string
12+
APIToken string
1013

1114
// Transport is the underlying HTTP transport to use when making requests.
1215
// It will default to http.DefaultTransport if nil.
1316
Transport http.RoundTripper
1417
}
1518

1619
// RoundTrip implements the RoundTripper interface. We just add the
17-
// basic auth and return the RoundTripper for this transport type.
20+
// basic auth information and return the RoundTripper for this transport type.
1821
func (t *BasicAuthTransport) RoundTrip(req *http.Request) (*http.Response, error) {
1922
req2 := cloneRequest(req) // per RoundTripper contract
2023

21-
req2.SetBasicAuth(t.Username, t.Password)
24+
req2.SetBasicAuth(t.Username, t.APIToken)
2225
return t.transport().RoundTrip(req2)
2326
}
2427

cloud/auth_transport_basic_test.go renamed to cloud/auth_transport_basic_auth_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ func TestBasicAuthTransport(t *testing.T) {
1010
setup()
1111
defer teardown()
1212

13-
username, password := "username", "password"
13+
username, apiToken := "username", "api_token"
1414

1515
testMux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
1616
u, p, ok := r.BasicAuth()
@@ -20,14 +20,14 @@ func TestBasicAuthTransport(t *testing.T) {
2020
if u != username {
2121
t.Errorf("request contained basic auth username %q, want %q", u, username)
2222
}
23-
if p != password {
24-
t.Errorf("request contained basic auth password %q, want %q", p, password)
23+
if p != apiToken {
24+
t.Errorf("request contained basic auth password %q, want %q", p, apiToken)
2525
}
2626
})
2727

2828
tp := &BasicAuthTransport{
2929
Username: username,
30-
Password: password,
30+
APIToken: apiToken,
3131
}
3232

3333
basicAuthClient, _ := NewClient(testServer.URL, tp.Client())

cloud/auth_transport_bearer.go

Lines changed: 0 additions & 41 deletions
This file was deleted.

cloud/auth_transport_personal_access_token.go

Lines changed: 0 additions & 43 deletions
This file was deleted.

cloud/auth_transport_personal_access_token_test.go

Lines changed: 0 additions & 30 deletions
This file was deleted.

cloud/examples/addlabel/main.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ func main() {
3636

3737
tp := jira.BasicAuthTransport{
3838
Username: strings.TrimSpace(username),
39-
Password: strings.TrimSpace(password),
39+
APIToken: strings.TrimSpace(password),
4040
}
4141

4242
client, err := jira.NewClient(strings.TrimSpace(jiraURL), tp.Client())

cloud/examples/basic_auth/main.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package main
2+
3+
import (
4+
"context"
5+
"fmt"
6+
7+
jira "github.com/andygrunwald/go-jira/v2/cloud"
8+
)
9+
10+
func main() {
11+
jiraURL := "https://go-jira-opensource.atlassian.net/"
12+
13+
// Jira docs: https://support.atlassian.com/atlassian-account/docs/manage-api-tokens-for-your-atlassian-account/
14+
// Create a new API token: https://id.atlassian.com/manage-profile/security/api-tokens
15+
tp := jira.BasicAuthTransport{
16+
Username: "<username>",
17+
APIToken: "<api-token>",
18+
}
19+
client, err := jira.NewClient(jiraURL, tp.Client())
20+
if err != nil {
21+
panic(err)
22+
}
23+
24+
u, _, err := client.User.GetSelf(context.Background())
25+
if err != nil {
26+
panic(err)
27+
}
28+
29+
fmt.Printf("Email: %v\n", u.EmailAddress)
30+
fmt.Println("Success!")
31+
}

cloud/examples/basicauth/main.go

Lines changed: 0 additions & 49 deletions
This file was deleted.

0 commit comments

Comments
 (0)