2019-04-09 15:38:58 +00:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
module Gitlab
|
|
|
|
module ExternalAuthorization
|
|
|
|
class Response
|
|
|
|
include ::Gitlab::Utils::StrongMemoize
|
|
|
|
|
|
|
|
def initialize(excon_response)
|
|
|
|
@excon_response = excon_response
|
|
|
|
end
|
|
|
|
|
|
|
|
def valid?
|
|
|
|
@excon_response && [200, 401, 403].include?(@excon_response.status)
|
|
|
|
end
|
|
|
|
|
|
|
|
def successful?
|
|
|
|
valid? && @excon_response.status == 200
|
|
|
|
end
|
|
|
|
|
|
|
|
def reason
|
|
|
|
parsed_response['reason'] if parsed_response
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def parsed_response
|
|
|
|
strong_memoize(:parsed_response) { parse_response! }
|
|
|
|
end
|
|
|
|
|
|
|
|
def parse_response!
|
2020-05-01 00:09:59 +00:00
|
|
|
Gitlab::Json.parse(@excon_response.body)
|
2019-04-09 15:38:58 +00:00
|
|
|
rescue JSON::JSONError
|
|
|
|
# The JSON response is optional, so don't fail when it's missing
|
|
|
|
nil
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|