mirror of
https://github.com/interviewstreet/go-jira.git
synced 2024-12-02 08:51:43 +02:00
67 lines
2.3 KiB
Go
67 lines
2.3 KiB
Go
package jira
|
|
|
|
import (
|
|
"fmt"
|
|
)
|
|
|
|
// 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 {
|
|
Self string `json:"self,omitempty"`
|
|
Name string `json:"name,omitempty"`
|
|
Session struct {
|
|
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"`
|
|
}
|
|
|
|
// 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).
|
|
//
|
|
// JIRA API docs: https://docs.atlassian.com/jira/REST/latest/#auth/1/session
|
|
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)
|
|
if resp.StatusCode != 200 || err != nil {
|
|
return false, fmt.Errorf("Auth at JIRA instance failed (HTTP(S) request). %s", err)
|
|
}
|
|
|
|
s.client.session = session
|
|
|
|
return true, nil
|
|
}
|
|
|
|
// TODO Missing API Call GET (Returns information about the currently authenticated user's session)
|
|
// See https://docs.atlassian.com/jira/REST/latest/#auth/1/session
|
|
// TODO Missing API Call DELETE (Logs the current user out of JIRA, destroying the existing session, if any.)
|
|
// See https://docs.atlassian.com/jira/REST/latest/#auth/1/session
|