2019-07-07 11:18:12 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
module Gitlab
|
|
|
|
module ExternalAuthorization
|
|
|
|
class Response
|
|
|
|
include ::Gitlab::Utils::StrongMemoize
|
|
|
|
|
2020-10-24 23:57:45 +05:30
|
|
|
def initialize(response)
|
|
|
|
@response = response
|
2019-07-07 11:18:12 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
def valid?
|
2020-10-24 23:57:45 +05:30
|
|
|
@response && [200, 401, 403].include?(@response.code)
|
2019-07-07 11:18:12 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
def successful?
|
2020-10-24 23:57:45 +05:30
|
|
|
valid? && @response.code == 200
|
2019-07-07 11:18:12 +05:30
|
|
|
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-10-24 23:57:45 +05:30
|
|
|
Gitlab::Json.parse(@response.body)
|
2019-07-07 11:18:12 +05:30
|
|
|
rescue JSON::JSONError
|
|
|
|
# The JSON response is optional, so don't fail when it's missing
|
|
|
|
nil
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|