Skip to content
This repository has been archived by the owner on Feb 25, 2022. It is now read-only.

Commit

Permalink
Added token_auth and custom authorization with tests
Browse files Browse the repository at this point in the history
  • Loading branch information
coneybeare committed Jan 4, 2013
1 parent 22b00a9 commit 77baf32
Show file tree
Hide file tree
Showing 5 changed files with 200 additions and 2 deletions.
53 changes: 53 additions & 0 deletions README.md
Expand Up @@ -73,6 +73,59 @@ A path can include "`:id`" anywhere in it, which is replaced by the instance's `
m.id # => 1
m.legs # => Requests "/millepieds/1/legs"

## Authorization

Remotely is setup to allow basic auth, token auth, or custom authentication schemes.

**Basic Auth**

`basic_auth` accepts 2 String params: `username` and `password`

# 'Authorization' header => "Basic dXNlcjpwYXNzd29yZA=="

Remotely.configure do
app :legsapp do
url "http://somanylegs.com/api/v1"
basic_auth "username", "password"
end
end

**Token Auth**

`token_auth` accepts a String param for the `token`, and an optional Hash of token options

# 'Authorization' header => "Token token=\"abcdef\", foo=\"bar\""

Remotely.configure do
app :legsapp do
url "http://somanylegs.com/api/v1"
token_auth "abcdef", {:foo => 'bar'}
end
end

**Custom Authorization**

`authorization` accepts a String param for the `type`, and either a String or Hash `token`. A String value is taken literally, and a Hash is encoded into comma separated key/value pairs.

# 'Authorization' header => "Bearer mF_9.B5f-4.1JqM"

Remotely.configure do
app :legsapp do
url "http://somanylegs.com/api/v1"
authorization 'Bearer', 'mF_9.B5f-4.1JqM'
end
end
 

# 'Authorization' header => "OAuth token=\"abcdef\", foo=\"bar\""

Remotely.configure do
app :legsapp do
url "http://somanylegs.com/api/v1"
authorization 'OAuth', {:token => 'abcdef', :foo => 'bar'}
end
end

## Fetched Objects

Remote associations are Remotely::Model objects. Whatever data the API returns, becomes the attributes of the Model.
Expand Down
28 changes: 26 additions & 2 deletions lib/remotely/application.rb
Expand Up @@ -25,6 +25,28 @@ def basic_auth(user=nil, password=nil)
return @basic_auth unless user && password
@basic_auth = [user, password]
end

# Set or get the Authorization header.
# - As seen here: https://github.com/lostisland/faraday/blob/master/lib/faraday/connection.rb#L204
#
# @param [String] token - The String token.
# @param [Hash] options - Optional Hash of extra token options.
#
def token_auth(token=nil, options={})
return @token_auth unless token
@token_auth = [token, options]
end

# Set or get a custom Authorization header.
# - As seen here: https://github.com/lostisland/faraday/blob/master/lib/faraday/connection.rb#L227
#
# @param [String] type - The String authorization type.
# @param [String|Hash] token - The String or Hash token. A String value is taken literally, and a Hash is encoded into comma separated key/value pairs.
#
def authorization(type=nil, token=nil)
return @authorization unless type && token
@authorization = [type, token]
end

# Connection to the application (with BasicAuth if it was set).
#
Expand All @@ -35,8 +57,10 @@ def connection
b.request :url_encoded
b.adapter :net_http
end

@connection.basic_auth(*@basic_auth) if @basic_auth

@connection.basic_auth(*@basic_auth) if @basic_auth
@connection.token_auth(*@token_auth) if @token_auth
@connection.authorization(*@authorization) if @authorization
@connection
end

Expand Down
31 changes: 31 additions & 0 deletions spec/remotely/application_spec.rb
Expand Up @@ -15,6 +15,21 @@
app = Remotely::Application.new(:name) { basic_auth "user", "pass" }
app.basic_auth.should == ["user", "pass"]
end

it "sets token auth credentials" do
app = Remotely::Application.new(:name) { token_auth "token", {:foo => :bar} }
app.token_auth.should == ["token", {:foo => :bar}]
end

it "sets custom authorization credentials as a string" do
app = Remotely::Application.new(:name) { authorization "OAuth", "token=foo" }
app.authorization.should == ["OAuth", "token=foo"]
end

it "sets custom authorization credentials as a hash" do
app = Remotely::Application.new(:name) { authorization "OAuth", {:token => :foo} }
app.authorization.should == ["OAuth", {:token => :foo}]
end

it "has a connection to the app" do
app = Remotely::Application.new(:name) { url "http://example.com" }
Expand All @@ -28,4 +43,20 @@
end
app.connection.headers["authorization"].should_not be_nil
end

it "has a connection with token auth to the app" do
app = Remotely::Application.new(:name) do
url "http://example.com"
token_auth "token", {:foo => :bar}
end
app.connection.headers["authorization"].should_not be_nil
end

it "has a connection with custom authorization to the app" do
app = Remotely::Application.new(:name) do
url "http://example.com"
authorization "OAuth", {:token => :foo}
end
app.connection.headers["authorization"].should_not be_nil
end
end
75 changes: 75 additions & 0 deletions spec/remotely/model_spec.rb
Expand Up @@ -279,6 +279,81 @@
a_request(:get, "#{app}/adventures/1").with(headers: {})
end
end

context "token auth" do
before do
Remotely.configure do
app :adventure_app do
url "http://localhost:3000"
token_auth "token", {:foo => :bar}
end
end
end

after do
Remotely.reset!
end

it "sends Authorization headers when token auth is configured" do
Adventure.find(1)
a_request(:get, "#{app}/adventures/1").with(headers: {'Authorization' => "Token token=token foo=bar"})
end

it "doesn't send Authorization headers when token auth is not configured" do
Adventure.find(1)
a_request(:get, "#{app}/adventures/1").with(headers: {})
end
end

context "custom authorization as a string" do
before do
Remotely.configure do
app :adventure_app do
url "http://localhost:3000"
authorization "OAuth", "token=foo"
end
end
end

after do
Remotely.reset!
end

it "sends Authorization headers when custom authorization is configured" do
Adventure.find(1)
a_request(:get, "#{app}/adventures/1").with(headers: {'Authorization' => "OAuth token=foo"})
end

it "doesn't send Authorization headers when custom authorization is not configured" do
Adventure.find(1)
a_request(:get, "#{app}/adventures/1").with(headers: {})
end
end

context "custom authorization as a hash" do
before do
Remotely.configure do
app :adventure_app do
url "http://localhost:3000"
authorization "OAuth", {:token => :foo}
end
end
end

after do
Remotely.reset!
end

it "sends Authorization headers when custom authorization is configured" do
Adventure.find(1)
a_request(:get, "#{app}/adventures/1").with(headers: {'Authorization' => "OAuth token=foo"})
end

it "doesn't send Authorization headers when custom authorization is not configured" do
Adventure.find(1)
a_request(:get, "#{app}/adventures/1").with(headers: {})
end
end

it "sets the app it belongs to" do
Adventure.app.name.should == :adventure_app
Expand Down
15 changes: 15 additions & 0 deletions spec/remotely_spec.rb
Expand Up @@ -25,4 +25,19 @@
Remotely.configure { app(:appname) { basic_auth "user", "pass" }}
Remotely.apps[:appname].basic_auth.should == ["user", "pass"]
end

it "saves the token auth credentials" do
Remotely.configure { app(:appname) { token_auth "token", {:foo => :bar} }}
Remotely.apps[:appname].token_auth.should == ["token", {:foo => :bar}]
end

it "saves the authorization credentials as a string" do
Remotely.configure { app(:appname) { authorization "OAuth", "token=foo" }}
Remotely.apps[:appname].authorization.should == [ "OAuth", "token=foo" ]
end

it "saves the authorization credentials as a hash" do
Remotely.configure { app(:appname) { authorization "OAuth", {:token => :foo} }}
Remotely.apps[:appname].authorization.should == [ "OAuth", {:token => :foo}]
end
end

0 comments on commit 77baf32

Please sign in to comment.