Class: AuthenticationController

Inherits:
MVCLI::Controller
  • Object
show all
Defined in:
app/controllers/authentication_controller.rb

Instance Method Summary collapse

Instance Method Details

#loginObject



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'app/controllers/authentication_controller.rb', line 11

def 
   = 
  username = .name
  password = .password

  uri = URI.parse(configuration.auth_endpoint)
  connection = Excon.new(uri.to_s)
  
  headers = {'Content-Type' => 'application/json'}
  body = {auth: {passwordCredentials: {username: username, password: password}}}

  response = connection.post headers: headers, body: body.to_json, path: '/v2.0/tokens'

  #TODO check the status code of the request
   = Map(JSON.parse response.body)

  #Test if it's authenticated
  fail "User could not be authenticated" unless [:access]

  headers.merge!({'X-Auth-Token' => .access.token.id})
  response = connection.get headers: headers, path: "/v2.0/users/#{.access.user.id}/OS-KSADM/credentials/RAX-KSKEY:apiKeyCredentials"

  user_credentials = Map(JSON.parse response.body)

  configuration.username = username
  configuration.api_key = user_credentials["RAX-KSKEY:apiKeyCredentials"].apiKey
  configuration.region = .region
  configuration.save
  
  
end

#logoutObject



43
44
45
# File 'app/controllers/authentication_controller.rb', line 43

def logout
  configuration.delete
end