Permalink
Browse files

Add validate_credentials method

Just for @jbarnette
  • Loading branch information...
1 parent ae64322 commit 6cb40b8b48bb4f0b688c010338e5b409724eff8c @pengwynn pengwynn committed Nov 28, 2012
Showing with 28 additions and 1 deletion.
  1. +5 −1 lib/octokit/client/authorizations.rb
  2. +14 −0 lib/octokit/client/users.rb
  3. +9 −0 spec/octokit/client/users_spec.rb
View
6 lib/octokit/client/authorizations.rb
@@ -96,7 +96,11 @@ def delete_authorization(number, option={})
request(:delete, "authorizations/#{number}").status == 204
end
-
+ # Check scopes for a token
+ #
+ # @param token [String] GitHub OAuth token
+ # @return [Array<String>] OAuth scopes
+ # @see http://developer.github.com/v3/oauth/#scopes
def scopes(token=nil)
request(:get, "user", :access_token => token).
headers[:x_oauth_scopes].
View
14 lib/octokit/client/users.rb
@@ -28,6 +28,20 @@ def user(user=nil)
end
end
+ # Validate user username and password
+ #
+ # @param options [Hash] User credentials
+ # @option options [String] :login GitHub login
+ # @option options [String] :password GitHub password
+ # @return [Boolean] True if credentials are valid
+ def validate_credentials(options = {})
+ begin
+ !self.class.new(options).user.nil?
+ rescue Octokit::Unauthorized
+ false
+ end
+ end
+
# Update the authenticated user
#
# @param options [Hash] A customizable set of options.
View
9 spec/octokit/client/users_spec.rb
@@ -48,6 +48,15 @@
end
+ describe ".validate_credentials" do
+ it "validates username and password" do
+ stub_get("https://sferik:foobar@api.github.com/user").
+ to_return(json_response("user.json"))
+
+ expect(Octokit.validate_credentials(:login => 'sferik', :password => 'foobar')).to eq(true)
+ end
+ end
+
describe ".update_user" do
context "with a location passed" do

0 comments on commit 6cb40b8

Please sign in to comment.