Class: CookieCredentials

Inherits:
Credentials show all
Defined in:
lib/jirarest2/cookie_credentials.rb

Overview

Cookies as credential for the server login uses basic auth to log in and then cookies are used

Instance Attribute Summary collapse

Attributes inherited from Credentials

#baseurl, #connecturl, #username

Instance Method Summary collapse

Constructor Details

#initialize(connecturl, username, autosave = false) ⇒ CookieCredentials

Returns a new instance of CookieCredentials.

Parameters:

  • connecturl (String)

    URL to JIRA(tm) instance

  • username (String)

    Username to connect to the server

  • autosave (Boolean) (defaults to: false)

    Save the cookie on the harddisk whenever something happens?



35
36
37
38
39
40
# File 'lib/jirarest2/cookie_credentials.rb', line 35

def initialize(connecturl, username, autosave = false )
  super(connecturl,username)
  @cookiejar = {}
  @autosave = autosave
  @cookiestore = "~/.jirarest2.cookie"
end

Instance Attribute Details

#autosaveObject (readonly)

Returns the value of attribute autosave.



30
31
32
# File 'lib/jirarest2/cookie_credentials.rb', line 30

def autosave
  @autosave
end

#cookiestoreString

Location of the file the cookie is persited on a harddrive. Default is “~/.jirarest2.cookie

Returns:

  • (String)

    Location of the cookie on the harddrive



29
30
31
# File 'lib/jirarest2/cookie_credentials.rb', line 29

def cookiestore
  @cookiestore
end

Instance Method Details

#bake_cookies(header) ⇒ Object

Setup new cookies or update the existing jar.

Parameters:

  • header (Array)

    Header after a request



44
45
46
47
48
49
50
51
52
# File 'lib/jirarest2/cookie_credentials.rb', line 44

def bake_cookies(header)
  return if !header
  header.each do |cookie|
    next unless (pair = cookie.gsub(/[;]*\s*Path=\S+[,]*/,'').split(/\=/)).length == 2
    @cookiejar[pair.first] = pair.last
  end
  store_cookiejar if @autosave    
  return @cookiejar
end

#get_auth_header(request) ⇒ String

Get the auth header to send to the server

Parameters:

  • request (Net:::HTTP::Post, Net:::HTTP::Put, Net:::HTTP::Get, Net:::HTTP::Delete)

    Request object

Returns:

  • (String)

    Header-Line

Raises:



69
70
71
72
73
74
75
76
77
78
79
# File 'lib/jirarest2/cookie_credentials.rb', line 69

def get_auth_header(request)
  if @cookiejar["JSESSIONID"].nil? then
    raise Jirarest2::CookieAuthenticationError, "No valid cookies"
  end
  if get_cookies == "" then
    # This code should never be executed as the AuthenticationError above will catch more cases
    request["Cookie"] = "JSESSIONID=0" 
  else
    request["Cookie"] = get_cookies 
  end
end

#get_cookiesObject

Get the cookies in the format to send as header



61
62
63
# File 'lib/jirarest2/cookie_credentials.rb', line 61

def get_cookies
  @cookiejar.map { |cookie| cookie * '=' }.join('; ')
end

#load_cookiejarObject

Loads a cookiejar from disk



103
104
105
106
107
108
109
# File 'lib/jirarest2/cookie_credentials.rb', line 103

def load_cookiejar
  storage = PStore.new(File.expand_path(@cookiestore))
  storage.transaction do
    @cookiejar = storage["cookiejar"]
  end
  @cookiejar = {} if @cookiejar.nil? # Fix a not so nice feature of PStore if it doesn't find content in the file
end

#login(username, password) ⇒ Object

Login per username and password in case the cookie is invalid or missing Uses basic auth to authenticate

Parameters:

  • username (String)

    Username to use for login

  • password (String)

    Password to use for login



85
86
87
88
89
90
91
# File 'lib/jirarest2/cookie_credentials.rb', line 85

def (username,password)
  pcred = PasswordCredentials.new(@connecturl,username,password)
  pconnect = Connect.new(pcred)
  result = pconnect.execute("Post","auth/latest/session",{"username" => username, "password" => password})
  bake_cookies(result.header["set-cookie"]) # I already had them seperated into an array.
  return @cookiejar["JSESSIONID"]
end

#logoutBoolean

Invalidates the current cookie

Returns:

  • (Boolean)

    true if successful



95
96
97
98
99
100
# File 'lib/jirarest2/cookie_credentials.rb', line 95

def logout
    con = Connect.new(self)
    ret = con.execute("Delete","auth/latest/session","").code
    store_cookiejar if @autosave
    return true if ret == "204"
end

#set_cookies(header) ⇒ Object

Name alias for bake_cookies

Parameters:

  • header (String)

    Header after a request



56
57
58
# File 'lib/jirarest2/cookie_credentials.rb', line 56

def set_cookies(header)
  return bake_cookies(header)
end

#store_cookiejarObject

Writes the cookiejar to disk



112
113
114
115
116
117
# File 'lib/jirarest2/cookie_credentials.rb', line 112

def store_cookiejar
  storage = PStore.new(File.expand_path(@cookiestore))
  storage.transaction do
    storage["cookiejar"] = @cookiejar 
  end
end