2019-04-09 11:38:58 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
module Gitlab
|
|
|
|
module ExternalAuthorization
|
|
|
|
class Response
|
|
|
|
include ::Gitlab::Utils::StrongMemoize
|
|
|
|
|
2020-07-27 17:09:16 -04:00
|
|
|
def initialize(response)
|
|
|
|
@response = response
|
2019-04-09 11:38:58 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def valid?
|
2020-07-27 17:09:16 -04:00
|
|
|
@response && [200, 401, 403].include?(@response.code)
|
2019-04-09 11:38:58 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def successful?
|
2020-07-27 17:09:16 -04:00
|
|
|
valid? && @response.code == 200
|
2019-04-09 11:38:58 -04:00
|
|
|
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-07-27 17:09:16 -04:00
|
|
|
Gitlab::Json.parse(@response.body)
|
2019-04-09 11:38:58 -04:00
|
|
|
rescue JSON::JSONError
|
|
|
|
# The JSON response is optional, so don't fail when it's missing
|
|
|
|
nil
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|