From c506e410b62363382842ed33f7f1b8d3834ef403 Mon Sep 17 00:00:00 2001 From: Liang Tao Date: Mon, 10 Mar 2014 11:35:34 +0800 Subject: [PATCH] =?UTF-8?q?=E5=88=A0=E9=99=A4=E8=BF=87=E6=9C=9F=E7=9A=84Au?= =?UTF-8?q?th=E5=87=BD=E6=95=B0=E5=92=8C=E7=9B=B8=E5=85=B3=E6=B5=8B?= =?UTF-8?q?=E8=AF=95=E7=94=A8=E4=BE=8B=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/qiniu/rs/auth.rb | 65 -------------------------------------- spec/qiniu/rs/auth_spec.rb | 58 ---------------------------------- 2 files changed, 123 deletions(-) delete mode 100755 spec/qiniu/rs/auth_spec.rb diff --git a/lib/qiniu/rs/auth.rb b/lib/qiniu/rs/auth.rb index 9955edb..787de0d 100755 --- a/lib/qiniu/rs/auth.rb +++ b/lib/qiniu/rs/auth.rb @@ -8,78 +8,13 @@ module Qiniu class << self include Utils - def exchange_by_password!(username, password) - @username = username - @password = password - post_data = { - :client_id => Config.settings[:client_id], - :grant_type => "password", - :username => username, - :password => password - } - code, data = http_request Config.settings[:auth_url], post_data - reset_token(data["access_token"], data["refresh_token"]) if Utils.is_response_ok?(code) - [code, data] - end - - def exchange_by_refresh_token!(refresh_token) - post_data = { - :client_id => Config.settings[:client_id], - :grant_type => "refresh_token", - :refresh_token => refresh_token - } - code, data = http_request Config.settings[:auth_url], post_data - reset_token(data["access_token"], data["refresh_token"]) if Utils.is_response_ok?(code) - [code, data] - end - - def reset_token(access_token, refresh_token) - @access_token = access_token - @refresh_token = refresh_token - end - - def call_with_logged_in(url, data, retry_times = 0) - raise MissingAccessToken if @access_token.nil? - code, data = http_request url, data, {:access_token => @access_token} - if code == 401 - raise MissingRefreshToken if @refresh_token.nil? - code, data = exchange_by_refresh_token!(@refresh_token) - if code == 401 - raise MissingUsernameOrPassword if (@username.nil? || @password.nil?) - code, data = exchange_by_password!(@username, @password) - end - if Utils.is_response_ok?(code) - retry_times += 1 - if Config.settings[:auto_reconnect] && retry_times < Config.settings[:max_retry_times] - return call_with_logged_in(url, data, retry_times) - end - end - end - [code, data] - end - def call_with_signature(url, data, retry_times = 0, options = {}) code, data = http_request url, data, options.merge({:qbox_signature_token => generate_qbox_signature(url, data, options[:mime])}) [code, data] end def request(url, data = nil, options = {}) - begin - if Config.settings[:access_key].empty? || Config.settings[:secret_key].empty? - code, data = Auth.call_with_logged_in(url, data) - else code, data = Auth.call_with_signature(url, data, 0, options) - end - rescue MissingAccessToken => e - Log.logger.error e - code, data = 401, {} - rescue MissingRefreshToken => e - Log.logger.error e - code, data = 401, {} - rescue MissingUsernameOrPassword => e - Log.logger.error e - code, data = 401, {} - end [code, data] end diff --git a/spec/qiniu/rs/auth_spec.rb b/spec/qiniu/rs/auth_spec.rb deleted file mode 100755 index e7b9d6a..0000000 --- a/spec/qiniu/rs/auth_spec.rb +++ /dev/null @@ -1,58 +0,0 @@ -# -*- encoding: utf-8 -*- - -require 'spec_helper' -require 'qiniu/rs/auth' - -module Qiniu - module RS - describe Auth do - - before :all do - @username = "qboxtest" - @password = "qboxtest123" - - code, data = Qiniu::RS::Auth.exchange_by_password!(@username, @password) - code.should == 200 - data.should be_an_instance_of(Hash) - data["access_token"].should_not be_empty - data["refresh_token"].should_not be_empty - data["refresh_token"].should_not be_empty - - @access_token = data["access_token"] - @refresh_token = data["refresh_token"] - puts data.inspect - end - - context ".exchange_by_password" do - it "should sign in failed when pass a non-existent username" do - code, data = Qiniu::RS::Auth.exchange_by_password!("a_non_existent_user@example.com", "password") - code.should == 401 - data["error_code"].should == 11 - data["error"].should == "failed_authentication" - puts data.inspect - end - - it "should sign in failed when pass a wrong password" do - code, data = Qiniu::RS::Auth.exchange_by_password!(@username, "a-wrong-password") - code.should == 401 - data["error_code"].should == 11 - data["error"].should == "failed_authentication" - puts data.inspect - end - end - - context ".exchange_by_refresh_token" do - it "should works" do - @refresh_token.should_not be_empty - code, data = Qiniu::RS::Auth.exchange_by_refresh_token!(@refresh_token) - code.should == 200 - data["access_token"].should_not be_empty - data["refresh_token"].should_not be_empty - data["expires_in"].should_not be_zero - puts data.inspect - end - end - - end - end -end