mirror of
https://github.com/wahyd4/ruby-sdk.git
synced 2026-08-29 06:36:01 +10:00
删除过期的Auth函数和相关测试用例。
This commit is contained in:
@@ -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
|
||||
|
||||
|
||||
@@ -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
|
||||
Reference in New Issue
Block a user