2015-09-03 12:25:21 +02:00
package jira
import (
"fmt"
2016-05-27 12:01:54 +02:00
"net/http"
2015-09-03 12:25:21 +02:00
)
// AuthenticationService handles authentication for the JIRA instance / API.
//
// JIRA API docs: https://docs.atlassian.com/jira/REST/latest/#authentication
type AuthenticationService struct {
client * Client
}
// Session represents a Session JSON response by the JIRA API.
type Session struct {
2016-06-03 22:43:41 +02:00
Self string ` json:"self,omitempty" `
Name string ` json:"name,omitempty" `
Session struct {
2015-09-03 12:25:21 +02:00
Name string ` json:"name" `
Value string ` json:"value" `
} ` json:"session,omitempty" `
LoginInfo struct {
FailedLoginCount int ` json:"failedLoginCount" `
LoginCount int ` json:"loginCount" `
LastFailedLoginTime string ` json:"lastFailedLoginTime" `
PreviousLoginTime string ` json:"previousLoginTime" `
} ` json:"loginInfo" `
2016-06-03 22:43:41 +02:00
Cookies [ ] * http . Cookie
2015-09-03 12:25:21 +02:00
}
// AcquireSessionCookie creates a new session for a user in JIRA.
// Once a session has been successfully created it can be used to access any of JIRA's remote APIs and also the web UI by passing the appropriate HTTP Cookie header.
// The header will by automatically applied to every API request.
// Note that it is generally preferrable to use HTTP BASIC authentication with the REST API.
// However, this resource may be used to mimic the behaviour of JIRA's log-in page (e.g. to display log-in errors to a user).
//
2016-03-27 14:24:48 +02:00
// JIRA API docs: https://docs.atlassian.com/jira/REST/latest/#auth/1/session
2015-09-03 12:25:21 +02:00
func ( s * AuthenticationService ) AcquireSessionCookie ( username , password string ) ( bool , error ) {
apiEndpoint := "rest/auth/1/session"
body := struct {
Username string ` json:"username" `
Password string ` json:"password" `
} {
username ,
password ,
}
req , err := s . client . NewRequest ( "POST" , apiEndpoint , body )
if err != nil {
return false , err
}
session := new ( Session )
resp , err := s . client . Do ( req , session )
2016-06-03 22:43:41 +02:00
session . Cookies = resp . Cookies ( )
2016-05-27 12:01:54 +02:00
2016-04-23 15:23:15 +02:00
if err != nil {
2015-09-03 12:25:21 +02:00
return false , fmt . Errorf ( "Auth at JIRA instance failed (HTTP(S) request). %s" , err )
}
2016-04-23 15:23:15 +02:00
if resp != nil && resp . StatusCode != 200 {
return false , fmt . Errorf ( "Auth at JIRA instance failed (HTTP(S) request). Status code: %d" , resp . StatusCode )
}
2015-09-03 12:25:21 +02:00
s . client . session = session
return true , nil
}
2016-06-01 14:52:52 +02:00
// Authenticated reports if the current Client has an authenticated session with JIRA
func ( s * AuthenticationService ) Authenticated ( ) bool {
if s != nil {
return s . client . session != nil
}
return false
}
2015-09-03 12:25:21 +02:00
// TODO Missing API Call GET (Returns information about the currently authenticated user's session)
2016-03-27 14:24:48 +02:00
// See https://docs.atlassian.com/jira/REST/latest/#auth/1/session
2015-09-03 12:25:21 +02:00
// TODO Missing API Call DELETE (Logs the current user out of JIRA, destroying the existing session, if any.)
2016-03-27 14:24:48 +02:00
// See https://docs.atlassian.com/jira/REST/latest/#auth/1/session