Class: Middleware::RubyGem::Auth::Authentication

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

Instance Method Summary collapse

Instance Method Details

#authObject



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/middleware/auth/authentication.rb', line 22

def auth
  mw_api_key = ENV["MW_API_KEY"]
  if mw_api_key == nil
    MwLogger.warn("Profiling is not initialized as MW_API_KEY is not provided")
    return false
  end

  url = URI(ENV["MW_AUTH_URL"] || "https://app.middleware.io/api/v1/auth")

  https = Net::HTTP.new(url.host, url.port)
  https.use_ssl = true

  request = Net::HTTP::Post.new(url)
  request["Content-Type"] = "application/x-www-form-urlencoded"
  request["Authorization"] = "Bearer #{mw_api_key}"

  response = https.request(request)

  body = JSON.parse(response.body)
  success_value = body['success']

  if success_value
    @response = body
  else
    MwLogger.warn("Profiling is not initialized as authentication is failed")
  end

  success_value
end

#get_responseObject



19
20
21
# File 'lib/middleware/auth/authentication.rb', line 19

def get_response
  @response
end