Class: Cloud::Auth

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

Class Method Summary (collapse)

Class Method Details

+ (Object) login(user, password)



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/cloud/auth.rb', line 33

def self.(user, password)
  begin
    # POST (cloud url)/api/v1/login
    # puts "login against #{Settings.cloud_settings[:helix_cloud_url]}/api/v1/login"
    uri = URI("#{Settings.cloud_settings[:helix_cloud_url]}/api/v1/login")
    # return the session token for this user
    req = Net::HTTP::Post.new(uri)
    req['Content-Type'] = 'application/json'
    req.set_form_data('user[name]' => user, 'user[password]' => password)
    res = Net::HTTP.start(uri.hostname, uri.port) { |http|
      http.request(req)
    }

    # the body contains the required cookie
    return (res.code != '200') ? nil : res.body
  rescue Exception => e
    # puts e.message
  end
end

+ (Object) logout(env)



53
54
55
56
57
58
# File 'lib/cloud/auth.rb', line 53

def self.logout(env)
  # POST (cloud url)/api/v1/logout
  uri = URI("#{HWSSettings.cloud_settings[:helix_cloud_url]}/api/v1/login")
  # set the session token somehow
  res = Net::HTTP.post(uri)
end

+ (Boolean) valid_session?(env, auth)

Returns:

  • (Boolean)


8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/cloud/auth.rb', line 8

def self.valid_session?(env, auth)
  # if the session is not valid, throw an exception
  # GET (cloud url)/api/v1/user
  uri = URI("#{Settings.cloud_settings[:helix_cloud_url]}/api/v1/user")
  # auth.credentials.first == username
  # auth.credentials.last == session token
  req = Net::HTTP::Get.new(uri)
  req['Cookie'] = auth.credentials.last
  req['Content-Type'] = 'application/json'

  res = Net::HTTP.start(uri.hostname, uri.port) {|http|
    http.request(req)
  }

  # make sure the username matches?
  return false if res.code != '200' || JSON(res.body)['user'] != auth.credentials.first

  env['AUTH_CREDENTIALS'] = auth.credentials
  env['p4.user'] = JSON(res.body)['user']
  env['p4'] = nil

  return true
end