1
0
mirror of https://github.com/interviewstreet/go-jira.git synced 2025-04-15 11:46:33 +02:00

Added basic auth support to the library

This commit is contained in:
Florian Krauthan 2017-02-08 14:37:57 -08:00
parent 8a4b1aca33
commit cf2bcefedb
3 changed files with 101 additions and 17 deletions

View File

@ -70,9 +70,41 @@ func main() {
} }
``` ```
### Authenticate with session cookie ### Authenticate with jira
Some actions require an authenticated user. Some actions require an authenticated user.
#### Authenticate with basic auth
Here is an example with a basic auth authentification.
```go
package main
import (
"fmt"
"github.com/andygrunwald/go-jira"
)
func main() {
jiraClient, err := jira.NewClient(nil, "https://your.jira-instance.com/")
if err != nil {
panic(err)
}
jiraClient.Authentication.SetBasicAuth("username", "password")
issue, _, err := jiraClient.Issue.Get("SYS-5156", nil)
if err != nil {
panic(err)
}
fmt.Printf("%s: %+v\n", issue.Key, issue.Fields.Summary)
}
```
#### Authenticate with session cookie
Here is an example with a session cookie authentification. Here is an example with a session cookie authentification.
```go ```go

View File

@ -7,11 +7,27 @@ import (
"net/http" "net/http"
) )
const (
// HTTP Basic Authentication
authTypeBasic = 1
// HTTP Session Authentication
authTypeSession = 2
)
// AuthenticationService handles authentication for the JIRA instance / API. // AuthenticationService handles authentication for the JIRA instance / API.
// //
// JIRA API docs: https://docs.atlassian.com/jira/REST/latest/#authentication // JIRA API docs: https://docs.atlassian.com/jira/REST/latest/#authentication
type AuthenticationService struct { type AuthenticationService struct {
client *Client client *Client
// Authentication type
authType int
// Basic auth username
username string
// Basic auth password
password string
} }
// Session represents a Session JSON response by the JIRA API. // Session represents a Session JSON response by the JIRA API.
@ -68,14 +84,26 @@ func (s *AuthenticationService) AcquireSessionCookie(username, password string)
} }
s.client.session = session s.client.session = session
s.authType = authTypeSession
return true, nil return true, nil
} }
// Authenticated reports if the current Client has an authenticated session with JIRA func (s *AuthenticationService) SetBasicAuth(username, password string) {
s.username = username;
s.password = password;
s.authType = authTypeBasic;
}
// Authenticated reports if the current Client has authentication details for JIRA
func (s *AuthenticationService) Authenticated() bool { func (s *AuthenticationService) Authenticated() bool {
if s != nil { if s != nil {
if s.authType == authTypeSession {
return s.client.session != nil return s.client.session != nil
} else if s.authType == authTypeBasic {
return s.username != ""
}
} }
return false return false
} }
@ -84,7 +112,7 @@ func (s *AuthenticationService) Authenticated() bool {
// //
// JIRA API docs: https://docs.atlassian.com/jira/REST/latest/#auth/1/session // JIRA API docs: https://docs.atlassian.com/jira/REST/latest/#auth/1/session
func (s *AuthenticationService) Logout() error { func (s *AuthenticationService) Logout() error {
if s.client.session == nil { if s.authType != authTypeSession || s.client.session == nil {
return fmt.Errorf("No user is authenticated yet.") return fmt.Errorf("No user is authenticated yet.")
} }
@ -116,7 +144,7 @@ func (s *AuthenticationService) GetCurrentUser() (*Session, error) {
if s == nil { if s == nil {
return nil, fmt.Errorf("AUthenticaiton Service is not instantiated") return nil, fmt.Errorf("AUthenticaiton Service is not instantiated")
} }
if s.client.session == nil { if s.authType != authTypeSession || s.client.session == nil {
return nil, fmt.Errorf("No user is authenticated yet") return nil, fmt.Errorf("No user is authenticated yet")
} }

24
jira.go
View File

@ -84,12 +84,20 @@ func (c *Client) NewRawRequest(method, urlStr string, body io.Reader) (*http.Req
req.Header.Set("Content-Type", "application/json") req.Header.Set("Content-Type", "application/json")
// Set authentication information
if c.Authentication.authType == authTypeSession {
// Set session cookie if there is one // Set session cookie if there is one
if c.session != nil { if c.session != nil {
for _, cookie := range c.session.Cookies { for _, cookie := range c.session.Cookies {
req.AddCookie(cookie) req.AddCookie(cookie)
} }
} }
} else if c.Authentication.authType == authTypeBasic {
// Set basic auth information
if c.Authentication.username != "" {
req.SetBasicAuth(c.Authentication.username, c.Authentication.password)
}
}
return req, nil return req, nil
} }
@ -122,12 +130,20 @@ func (c *Client) NewRequest(method, urlStr string, body interface{}) (*http.Requ
req.Header.Set("Content-Type", "application/json") req.Header.Set("Content-Type", "application/json")
// Set authentication information
if c.Authentication.authType == authTypeSession {
// Set session cookie if there is one // Set session cookie if there is one
if c.session != nil { if c.session != nil {
for _, cookie := range c.session.Cookies { for _, cookie := range c.session.Cookies {
req.AddCookie(cookie) req.AddCookie(cookie)
} }
} }
} else if c.Authentication.authType == authTypeBasic {
// Set basic auth information
if c.Authentication.username != "" {
req.SetBasicAuth(c.Authentication.username, c.Authentication.password)
}
}
return req, nil return req, nil
} }
@ -174,12 +190,20 @@ func (c *Client) NewMultiPartRequest(method, urlStr string, buf *bytes.Buffer) (
// Set required headers // Set required headers
req.Header.Set("X-Atlassian-Token", "nocheck") req.Header.Set("X-Atlassian-Token", "nocheck")
// Set authentication information
if c.Authentication.authType == authTypeSession {
// Set session cookie if there is one // Set session cookie if there is one
if c.session != nil { if c.session != nil {
for _, cookie := range c.session.Cookies { for _, cookie := range c.session.Cookies {
req.AddCookie(cookie) req.AddCookie(cookie)
} }
} }
} else if c.Authentication.authType == authTypeBasic {
// Set basic auth information
if c.Authentication.username != "" {
req.SetBasicAuth(c.Authentication.username, c.Authentication.password)
}
}
return req, nil return req, nil
} }