Skip to content

Instantly share code, notes, and snippets.

@bluebaroncanada
Created November 13, 2016 18:23
Show Gist options
  • Save bluebaroncanada/69de695e9a002fb2ddb7dd5a08fc001d to your computer and use it in GitHub Desktop.
Save bluebaroncanada/69de695e9a002fb2ddb7dd5a08fc001d to your computer and use it in GitHub Desktop.
require 'openssl'
require 'net/http'
require 'json'
class Jira
JIRA_URI = URI.parse 'https://jira.gaul.csd.uwo.ca'
HTTP = Net::HTTP.new JIRA_URI.host, JIRA_URI.port
def initialize
HTTP.use_ssl = JIRA_URI.scheme == 'https'
HTTP.verify_mode=OpenSSL::SSL::VERIFY_NONE
end
def login username, password
# For some reason authentication is happening on the front controller
# This means that any request will log you in even if it's a 301(Moved Permanently)
login_request = Net::HTTP::Post.new '/'
# For some reason this requires 'login' => 'Log In'. It's likely the way the JIRA admin implemented the UWO auth
login_request.set_form_data 'os_username' => username, 'os_password' => password, 'login' => 'Log In'
login_response = HTTP.request login_request
# The x-seraph-loginreason will be in the format "OUT, OK"|"OUT, <LOGIN_FAILURE_REASON>"
login_disposition = login_response.header['x-seraph-loginreason'][5..-1]
if login_disposition != 'OK'
@logged_in = false
raise(login_disposition)
else
@logged_in = true
@logged_in_cookie = parse_set_cookies login_response.get_fields 'set-cookie'
end
end
def parse_set_cookies raw_cookies_array
cookies_array = Array.new
raw_cookies_array.each { | cookie |
# split the current cookie on ; (This is probably very bad)
# Then check specifically for the extra crowd.token_key(obvioulsy very bad)
if cookie.split('; ')[0] != "crowd.token_key=\"\""
#Take just the first part of the cookie and do no further processing (Probably bad)
cookies_array.push cookie.split('; ')[0]
end
}
# Join the resultant data into one cookie string
cookies_array.join '; '
end
def is_logged_in?
@logged_in
end
private
@logged_in_cookie = ''
@logged_in = false
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment