lib/sp/job/broker.rb in sp-job-0.1.17 vs lib/sp/job/broker.rb in sp-job-0.2.2

- old
+ new

@@ -17,17 +17,10 @@ # # Helper class that defined an 'i18n' message. # class I18N - private - - @key - @args - - public - attr_accessor :key attr_accessor :args def initialize (key:, args:) @key = key @@ -96,18 +89,10 @@ # # # class OAuth2 - private - - @service_id = nil - @client = nil - @redis = nil - - public - def initialize (service_id:, config:, redis: nil) @service_id = service_id @client = ::SP::Job::BrokerOAuth2Client.new( protocol: config[:protocol], host: config[:host], @@ -129,22 +114,22 @@ def authorize (scope: nil, fields: nil) # obtain an 'authorization code' ac_response = @client.get_authorization_code( a_redirect_uri = nil, a_scope = scope - ) + ) # got a valid 'authorization code'? if ac_response[:oauth2].has_key?(:code) # got fields? if nil != fields # prepare redis arguments: field value, [field value, ...] array = [] fields.each do |k,v| array << k.to_s array << v end - $redis.hmset("#{@service_id}:oauth:authorization_code:#{ac_response[:oauth2][:code]}", + @redis.hmset("#{@service_id}:oauth:authorization_code:#{ac_response[:oauth2][:code]}", array, 'patched_by', 'toconline-session' ) end # exchange it for a 'access' and a 'refresh' token @@ -210,11 +195,11 @@ at_response = @client.refresh_access_token( a_refresh_token = refresh_token, a_scope = nil # keep current scope ) if at_response[:oauth2].has_key?(:error) - return at_response + raise ::SP::Job::Broker::InternalError.new(i18n: nil, internal: at_response[:oauth2][:error]) end # prepare redis arguments: field value, [field value, ...] array = [] fields.each do |k,v| array << k.to_s @@ -293,32 +278,85 @@ :response => nil } end # + # Obtain an 'access' and a 'refresh' token. + # + # @param args check the authorize method o OAuth2 class + # @return hash with response content type and status code + # + def authorize (args) + call do + finalized(response: oauth2.authorize(args)) + end + @output + end + + # + # Refresh an access token. + # + # @param args check the refresh method o OAuth2 class + # @return hash with response content type and status code + # + def refresh (args) + call do + finalized(response: oauth2.refresh(args)) + end + @output + end + + # + # Patch a pair of tokens, by generating new ones + # + # @param args check the patch methods o OAuth2 class + # @return hash with response content type and status code + # + def patch (args) + call do + finalized(response: oauth2.patch(args)) + end + @output + end + + # + # Remove a pair of tokens from redis. + # + # @param args check the dispose method o OAuth2 class + # @return hash with response content type and status code + # + def dispose (args) + call do + finalized(response: oauth2.dispose(args)) + end + @output + end + + # # Finalize the job response. # # @param response # @param content_type # @param status_code # - # @return + # @return hash with response content type and status code # def finalized (response:, content_type: 'application/json', status_code: 200) @output[:response] = response @output[:content_type] = content_type @output[:status_code] = status_code @output end # # Perform an OAuth2 request, catch errors - # and convertem them to a common result hash. + # and convert them to a common result hash. # # @param callback # def call(*callback) + @output = {} begin @output = yield rescue ::SP::Job::BrokerOAuth2Client::InvalidEmailOrPassword => invalid_password @output[:status_code] = 403 @output[:content_type], @output[:response] = Error.new(i18n: nil, code: @output[:status_code], @@ -355,10 +393,10 @@ @output[:content_type], @output[:response] = broker_internal_error.content_type_and_body() rescue Error => broker_error @output[:status_code] = broker_error.code @output[:content_type], @output[:response] = broker_error.content_type_and_body() rescue Exception => e - internal_error = InternalError.new(i18n: nil, internal: nil) + internal_error = InternalError.new(i18n: nil, internal: e.message) @output[:status_code] = internal_error.code @output[:content_type], @output[:response] = internal_error.content_type_and_body() end @output end