Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow import/export of session #398

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 13 additions & 1 deletion authentication.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,19 @@ type Session struct {
LastFailedLoginTime string `json:"lastFailedLoginTime"`
PreviousLoginTime string `json:"previousLoginTime"`
} `json:"loginInfo"`
Cookies []*http.Cookie
Cookies []*http.Cookie `json:"cookies"`
}

// ExportSession exports the current session so it can be reused later
func (s *AuthenticationService) ExportSession() []byte {
session, _ := json.Marshal(s.client.session)
return session
}

// ImportSession imports a session
func (s *AuthenticationService) ImportSession(session []byte) {
_ = json.Unmarshal(session, &s.client.session)
s.authType = authTypeSession
}

// AcquireSessionCookieWithContext creates a new session for a user in Jira.
Expand Down
44 changes: 44 additions & 0 deletions authentication_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,50 @@ func TestAuthenticationService_AcquireSessionCookie_Success(t *testing.T) {
}
}

func TestAuthenticationService_AcquireSessionCookieExport(t *testing.T) {
setup()
defer teardown()
testMux.HandleFunc("/rest/auth/1/session", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, "POST")
testRequestURL(t, r, "/rest/auth/1/session")
b, err := ioutil.ReadAll(r.Body)
if err != nil {
t.Errorf("Error in read body: %s", err)
}
if bytes.Index(b, []byte(`"username":"foo"`)) < 0 {
t.Error("No username found")
}
if bytes.Index(b, []byte(`"password":"bar"`)) < 0 {
t.Error("No password found")
}

fmt.Fprint(w, `{"session":{"name":"JSESSIONID","value":"12345678901234567890"},"loginInfo":{"failedLoginCount":10,"loginCount":127,"lastFailedLoginTime":"2016-03-16T04:22:35.386+0000","previousLoginTime":"2016-03-16T04:22:35.386+0000"}}`)
})

_, err := testClient.Authentication.AcquireSessionCookie("foo", "bar")
if err != nil {
t.Errorf("No error expected. Got %s", err)
}

sessionExport := testClient.Authentication.ExportSession()
expectedSessionExport := `{"session":{"name":"JSESSIONID","value":"12345678901234567890"},"loginInfo":{"failedLoginCount":10,"loginCount":127,"lastFailedLoginTime":"2016-03-16T04:22:35.386+0000","previousLoginTime":"2016-03-16T04:22:35.386+0000"},"cookies":[]}`
if string(sessionExport) != expectedSessionExport {
t.Errorf("Expected sessionExport %s. Got %s", expectedSessionExport, string(sessionExport))
}
}

func TestAuthenticationService_SessionImport(t *testing.T) {
setup()
defer teardown()

session := []byte(`{"session":{"name":"JSESSIONID","value":"12345678901234567890"},"loginInfo":{"failedLoginCount":10,"loginCount":127,"lastFailedLoginTime":"2016-03-16T04:22:35.386+0000","previousLoginTime":"2016-03-16T04:22:35.386+0000"},"cookies":[]}`)
testClient.Authentication.ImportSession(session)

if testClient.Authentication.authType != authTypeSession {
t.Errorf("Expected authType %d. Got %d", authTypeSession, testClient.Authentication.authType)
}
}

func TestAuthenticationService_SetBasicAuth(t *testing.T) {
setup()
defer teardown()
Expand Down