Class: Casual::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/casual.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config) ⇒ Client

Returns a new instance of Client.



8
9
10
11
12
13
14
# File 'lib/casual.rb', line 8

def initialize(config)
  @hostname     = config[:hostname]
  @path         = config[:path]
  @https        = config[:https]
  @port         = config[:port] || 443
  @callback_url = config[:callback_url]
end

Instance Attribute Details

#callback_urlObject

Returns the value of attribute callback_url.



6
7
8
# File 'lib/casual.rb', line 6

def callback_url
  @callback_url
end

#hostnameObject

Returns the value of attribute hostname.



6
7
8
# File 'lib/casual.rb', line 6

def hostname
  @hostname
end

#httpsObject

Returns the value of attribute https.



6
7
8
# File 'lib/casual.rb', line 6

def https
  @https
end

#pathObject

Returns the value of attribute path.



6
7
8
# File 'lib/casual.rb', line 6

def path
  @path
end

#portObject

Returns the value of attribute port.



6
7
8
# File 'lib/casual.rb', line 6

def port
  @port
end

Instance Method Details

#acquire_ticket(login_page) ⇒ Object



37
38
39
40
# File 'lib/casual.rb', line 37

def acquire_ticket()
  ticket = Hpricot(.body).search('input[@name=lt]').first
  ticket ? ticket['value'] : nil
end

#authenticate(username, password) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/casual.rb', line 20

def authenticate(username,password)
   = connection.get("/#{no_slash_path}/login")
  headers = { 'Cookie' => .response["set-cookie"] }
  ticket = acquire_ticket()
  params = "username=#{username}&password=#{password}&lt=#{ticket}"
  params << '&_eventId=submit&submit=LOGIN'

  status,response =
      connection.post("/#{no_slash_path}/login", params, headers)

  if response =~ /JA-SIG/
    (response =~ /Log In Successful/) ? username : nil
  else
    status.code == '200' ? username : nil
  end
end

#authenticate_ticket(ticket) ⇒ Object



48
49
50
51
52
53
# File 'lib/casual.rb', line 48

def authenticate_ticket(ticket)
  connection.
    get("/#{no_slash_path}/serviceValidate?service=#{callback_url}" +
        "&ticket=#{ticket}").
    body
end

#authorization_urlObject



16
17
18
# File 'lib/casual.rb', line 16

def authorization_url
  "#{server_url}/login?service=#{callback_url}"
end

#connectionObject



42
43
44
45
46
# File 'lib/casual.rb', line 42

def connection
  http = Net::HTTP.new(hostname, port)
  http.use_ssl = @https
  http
end

#no_slash_pathObject



69
70
71
# File 'lib/casual.rb', line 69

def no_slash_path
  path[0] == 47 ? path[1..path.size] : path
end

#protocolObject



65
66
67
# File 'lib/casual.rb', line 65

def protocol
  https ? 'http' : 'https'
end

#server_urlObject



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

def server_url
  "#{protocol}://#{hostname}:#{port}/#{no_slash_path}"
end

#user_login(ticket) ⇒ Object



55
56
57
58
59
# File 'lib/casual.rb', line 55

def (ticket)
  user = Hpricot::XML(authenticate_ticket(ticket)).
            search('//cas:authenticationSuccess //cas:user').text
  user.strip != '' ? user : nil
end