Class: CookieCredentials
- Inherits:
-
Credentials
- Object
- Credentials
- CookieCredentials
- 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
-
#autosave ⇒ Object
readonly
Returns the value of attribute autosave.
-
#cookiestore ⇒ String
Location of the file the cookie is persited on a harddrive.
Attributes inherited from Credentials
#baseurl, #connecturl, #username
Instance Method Summary collapse
-
#bake_cookies(header) ⇒ Object
Setup new cookies or update the existing jar.
-
#get_auth_header(request) ⇒ String
Get the auth header to send to the server.
-
#get_cookies ⇒ Object
Get the cookies in the format to send as header.
-
#initialize(connecturl, username, autosave = false) ⇒ CookieCredentials
constructor
A new instance of CookieCredentials.
-
#load_cookiejar ⇒ Object
Loads a cookiejar from disk.
-
#login(username, password) ⇒ Object
Login per username and password in case the cookie is invalid or missing Uses basic auth to authenticate.
-
#logout ⇒ Boolean
Invalidates the current cookie.
-
#set_cookies(header) ⇒ Object
Name alias for bake_cookies.
-
#store_cookiejar ⇒ Object
Writes the cookiejar to disk.
Constructor Details
#initialize(connecturl, username, autosave = false) ⇒ CookieCredentials
Returns a new instance of CookieCredentials.
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
#autosave ⇒ Object (readonly)
Returns the value of attribute autosave.
30 31 32 |
# File 'lib/jirarest2/cookie_credentials.rb', line 30 def autosave @autosave end |
#cookiestore ⇒ String
Location of the file the cookie is persited on a harddrive. Default is “~/.jirarest2.cookie
29 30 31 |
# File 'lib/jirarest2/cookie_credentials.rb', line 29 def @cookiestore end |
Instance Method Details
#bake_cookies(header) ⇒ Object
Setup new cookies or update the existing jar.
44 45 46 47 48 49 50 51 52 |
# File 'lib/jirarest2/cookie_credentials.rb', line 44 def (header) return if !header header.each do || next unless (pair = .gsub(/[;]*\s*Path=\S+[,]*/,'').split(/\=/)).length == 2 @cookiejar[pair.first] = pair.last end if @autosave return @cookiejar end |
#get_auth_header(request) ⇒ String
Get the auth header to send to the server
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 == "" then # This code should never be executed as the AuthenticationError above will catch more cases request["Cookie"] = "JSESSIONID=0" else request["Cookie"] = end end |
#get_cookies ⇒ Object
Get the cookies in the format to send as header
61 62 63 |
# File 'lib/jirarest2/cookie_credentials.rb', line 61 def @cookiejar.map { || * '=' }.join('; ') end |
#load_cookiejar ⇒ Object
Loads a cookiejar from disk
103 104 105 106 107 108 109 |
# File 'lib/jirarest2/cookie_credentials.rb', line 103 def storage = PStore.new(File.(@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
85 86 87 88 89 90 91 |
# File 'lib/jirarest2/cookie_credentials.rb', line 85 def login(username,password) pcred = PasswordCredentials.new(@connecturl,username,password) pconnect = Connect.new(pcred) result = pconnect.execute("Post","auth/latest/session",{"username" => username, "password" => password}) (result.header["set-cookie"]) # I already had them seperated into an array. return @cookiejar["JSESSIONID"] end |
#logout ⇒ Boolean
Invalidates the current cookie
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 if @autosave return true if ret == "204" end |
#set_cookies(header) ⇒ Object
Name alias for bake_cookies
56 57 58 |
# File 'lib/jirarest2/cookie_credentials.rb', line 56 def (header) return (header) end |
#store_cookiejar ⇒ Object
Writes the cookiejar to disk
112 113 114 115 116 117 |
# File 'lib/jirarest2/cookie_credentials.rb', line 112 def storage = PStore.new(File.(@cookiestore)) storage.transaction do storage["cookiejar"] = @cookiejar end end |