Skip to content

Commit

Permalink
Reset cached agent when auth info changes
Browse files Browse the repository at this point in the history
  • Loading branch information
pengwynn committed Apr 16, 2014
1 parent 3785124 commit b66ad36
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 1 deletion.
44 changes: 44 additions & 0 deletions lib/octokit/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -268,8 +268,52 @@ def as_app(key = client_id, secret = client_secret, &block)
yield app_client if block_given?
end

# Set username for authentication
#
# @param value [String] GitHub username
def login=(value)
reset_agent
@login = value
end

# Set password for authentication
#
# @param value [String] GitHub password
def password=(value)
reset_agent
@password = value
end

# Set OAuth access token for authentication
#
# @param value [String] 40 character GitHub OAuth access token
def access_token=(value)
reset_agent
@access_token = value
end

# Set OAuth app client_id
#
# @param value [String] 20 character GitHub OAuth app client_id
def client_id=(value)
reset_agent
@client_id = value
end

# Set OAuth app client_secret
#
# @param value [String] 40 character GitHub OAuth app client_secret
def client_secret=(value)
reset_agent
@client_secret = value
end

private

def reset_agent
@agent = nil
end

def request(method, path, data, options = {})
if data.is_a?(Hash)
options[:query] = data.delete(:query) || {}
Expand Down
6 changes: 5 additions & 1 deletion spec/octokit/client_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -435,6 +435,7 @@

Octokit.reset!
Octokit.configure do |config|
config.access_token = 'a' * 40
config.client_id = @client_id
config.client_secret = @client_secret
config.per_page = 50
Expand Down Expand Up @@ -479,8 +480,11 @@
end

it "makes authenticated requests" do
stub_get github_url("/user")

client = Octokit.client
app_client = client.as_app do |c|
client.get "/user"
client.as_app do |c|
c.get "/"
end

Expand Down

0 comments on commit b66ad36

Please sign in to comment.