Permalink
Browse files

broke the repo, importing.

  • Loading branch information...
1 parent 9e23cb0 commit bf297ba305c70f7da0fae5bd46430ad6fe8d8922 @jeffkreeftmeijer committed Apr 17, 2010
Showing with 334 additions and 0 deletions.
  1. +5 −0 .gitignore
  2. +20 −0 LICENCE
  3. +1 −0 README.textile
  4. +17 −0 guestlist.gemspec
  5. +57 −0 lib/guestlist.rb
  6. +65 −0 spec/guestlist/github_spec.rb
  7. +111 −0 spec/guestlist_spec.rb
  8. +30 −0 spec/spec_helper.rb
  9. +28 −0 spec/stubs/github_200.xml
View
@@ -0,0 +1,5 @@
+*.sw?
+.DS_Store
+pkg
+tmp
+*.gem
View
20 LICENCE
@@ -0,0 +1,20 @@
+Copyright (c) 2009 Jeff Kreeftmeijer
+
+Permission is hereby granted, free of charge, to any person obtaining
+a copy of this software and associated documentation files (the
+"Software"), to deal in the Software without restriction, including
+without limitation the rights to use, copy, modify, merge, publish,
+distribute, sublicense, and/or sell copies of the Software, and to
+permit persons to whom the Software is furnished to do so, subject to
+the following conditions:
+
+The above copyright notice and this permission notice shall be
+included in all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
+LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
+OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
+WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
View
@@ -0,0 +1 @@
+h1. Guestlist
View
@@ -0,0 +1,17 @@
+Gem::Specification.new do |gem|
+ gem.name = 'guestlist'
+ gem.version = '0.0.1'
+ gem.date = Date.today.to_s
+
+ gem.summary = "Simple authentication using Github credentials."
+ gem.description = "Simple authentication using Github credentials."
+
+ gem.authors = ['Jeff Kreeftmeijer']
+ gem.email = 'jeff@kreeftmeijer.nl'
+ gem.homepage = 'http://github.com/jeffkreeftmeijer/guestlist'
+
+ gem.files = Dir['{lib,spec}/**/*', 'README*', 'LICENSE*'] & `git ls-files -z`.split("\0")
+
+ gem.add_dependency('httparty', '>= 0.5.2')
+ gem.add_development_dependency('rspec', '>= 1.3.0')
+end
View
@@ -0,0 +1,57 @@
+require 'rubygems'
+require 'httparty'
+
+module Guestlist
+ module ClassMethods
+ def authenticate(login, token)
+ if user = find_by_login(login)
+ return user if user.encrypted_token == encrypt(token)
+ end
+
+
+ if response = Github.fetch(login, token)
+ if user
+ user.update_attributes(:encrypted_token => encrypt(token))
+ user
+ else
+ User.create!(
+ :login => login,
+ :encrypted_token => encrypt(token)
+ )
+ end
+ end
+ end
+
+ protected
+
+ def encrypt(string)
+ Digest::SHA1.hexdigest(string)
+ end
+ end
+
+ def self.included(base)
+ base.extend(ClassMethods)
+ end
+
+ class Github
+ class StatusError < StandardError; end
+
+ def self.fetch(login, token)
+ response = HTTParty.post(
+ "http://github.com/api/v2/json/user/show/#{login}",
+ {
+ :query => {
+ :login => login,
+ :token => token
+ }
+ }
+ )
+
+ case response.code
+ when 200; response
+ when 401; false
+ else; raise StatusError, response.code
+ end
+ end
+ end
+end
@@ -0,0 +1,65 @@
+require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
+
+describe Guestlist::Github do
+ describe '.fetch' do
+ before do
+ @ok = File.read(
+ File.expand_path(
+ File.dirname(__FILE__) + '/../stubs/github_200.xml'
+ )
+ )
+ end
+
+ it 'should call to Github via HTTParty' do
+ HTTParty.should_receive(:post).with(
+ 'http://github.com/api/v2/json/user/show/login',
+ :query => {
+ :login => 'login',
+ :token => 't0k3n'
+ }
+ ).and_return(
+ HTTParty::Response.new(
+ Crack::JSON.parse(@ok), @ok, 200, 'message'
+ )
+ )
+
+ Guestlist::Github.fetch('login', 't0k3n')
+ end
+
+ it 'should return a hash with user data' do
+ HTTParty.stub!(:post).and_return(
+ HTTParty::Response.new(
+ Crack::JSON.parse(@ok), @ok, 200, 'message'
+ )
+ )
+
+ result = Guestlist::Github.fetch('login', 't0k3n')
+ result.should be_instance_of Hash
+ result.length.should == 1
+ result['user']['name'].should == 'Bob'
+ result['user']['blog'].should == 'http://bobsblog.com'
+ end
+
+ it 'should return false when authentication fails' do
+ HTTParty.stub!(:post).and_return(
+ HTTParty::Response.new(
+ {}, '', 401, 'message'
+ )
+ )
+
+ Guestlist::Github.fetch('wronglogin', 't3k0n').should == false
+ end
+
+ it 'should raise an error when anything else happens' do
+ HTTParty.stub!(:post).and_return(
+ HTTParty::Response.new(
+ {}, '', 500, 'message'
+ )
+ )
+
+ lambda {
+ Guestlist::Github.fetch('login', 't0k3n')
+ }.should raise_error(Guestlist::Github::StatusError, 500)
+ end
+ end
+end
View
@@ -0,0 +1,111 @@
+require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
+
+describe Guestlist do
+ before do
+ @github_200 = Crack::JSON.parse(
+ File.read(
+ File.expand_path(
+ File.dirname(__FILE__) + '/stubs/github_200.xml'
+ )
+ )
+ )
+ end
+ describe '#authenticate' do
+ it 'should call find with the login' do
+ User.should_receive(:find_by_login).with('login')
+ User.authenticate('login', 'token')
+ end
+
+ describe 'when authenticating an existing user' do
+ describe 'with the correct token' do
+ it 'should return the user' do
+ User.authenticate('exists', 'token').should be_instance_of User
+ end
+
+ it 'should not try to fetch the user' do
+ Guestlist::Github.should_not_receive(:fetch)
+ User.authenticate('exists', 'token')
+ end
+
+ it 'should not create a new user' do
+ User.should_not_receive(:create!)
+ User.authenticate('exists', 'token')
+ end
+ end
+
+ describe 'with an incorrect token' do
+ it 'should not return the user' do
+ User.authenticate('exists', 'wrongtoken').should == nil
+ end
+
+ it 'should try to fetch the user' do
+ Guestlist::Github.should_receive(:fetch).with('exists', 'wrongtoken')
+ User.authenticate('exists', 'wrongtoken')
+ end
+
+ describe 'that seems to be correct after all' do
+ before do
+ Guestlist::Github.stub!(:fetch).and_return(@github_200)
+ end
+
+ it 'should return the user' do
+ User.authenticate('exists', 'wrongtoken').should be_instance_of User
+ end
+
+ it 'should not create a new user' do
+ User.should_not_receive(:create!)
+ User.authenticate('exists', 'wrongtoken')
+ end
+
+ it 'should update the user token' do
+ user = User.new
+ User.stub(:find_by_login).and_return(user)
+ user.should_receive(:update_attributes)
+ @user = User.authenticate('exists', 'wrongtoken')
+ end
+ end
+ end
+ end
+
+ describe 'when authenticating a user that does not exist yet' do
+ before do
+ User.stub(:find_by_login).and_return(nil)
+ Guestlist::Github.stub!(:fetch).and_return(@github_200)
+ end
+
+ it 'should try to fetch the user' do
+ Guestlist::Github.should_receive(:fetch).with('doesnt_exist_yet', 'token')
+ User.authenticate('doesnt_exist_yet', 'token')
+ end
+
+ it 'should create a new user' do
+ User.should_receive(:create!).with({
+ :login => 'doesnt_exist_yet',
+ :encrypted_token => 'ee977806d7286510da8b9a7492ba58e2484c0ecc'
+ })
+ User.authenticate('doesnt_exist_yet', 'token')
+ end
+ end
+
+ describe 'when authenticating a user that does not exist' do
+ before do
+ User.stub(:find_by_login).and_return(nil)
+ Guestlist::Github.stub!(:fetch).and_return(false)
+ end
+ it 'should return nil' do
+ User.should_receive(:find_by_login).and_return(nil)
+ User.authenticate('doesnt_exist', 'token').should == nil
+ end
+
+ it 'should try to fetch the user' do
+ Guestlist::Github.should_receive(:fetch).with('doesnt_exist', 'token')
+ User.authenticate('doesnt_exist', 'token')
+ end
+
+ it 'should not create a new user' do
+ User.should_not_receive(:create!)
+ User.authenticate('doesnt_exist', 'token')
+ end
+ end
+ end
+end
View
@@ -0,0 +1,30 @@
+$LOAD_PATH.unshift(File.dirname(__FILE__))
+$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
+require 'guestlist'
+require 'spec'
+require 'spec/autorun'
+
+require 'crack'
+
+Spec::Runner.configure do |config|
+end
+
+class User
+ include Guestlist
+
+ def self.find_by_login(*args)
+ return new
+ end
+
+ def self.create!(*args)
+ return new
+ end
+
+ def encrypted_token
+ Digest::SHA1.hexdigest('token')
+ end
+
+ def update_attributes(*args)
+ return true
+ end
+end
View
@@ -0,0 +1,28 @@
+{
+ "user": {
+ "plan": {
+ "name":"free",
+ "collaborators":0,
+ "space":307200,
+ "private_repos":0
+ },
+ "gravatar_id":"f03f4ce7b507aede346263d218228b6a",
+ "name":"Bob",
+ "company":"c0mp4ny",
+ "location":"l0c4ti0n",
+ "created_at":"2009/01/01 07:26:12 -0800",
+ "collaborators":0,
+ "disk_usage":21900,
+ "public_gist_count":24,
+ "public_repo_count":15,
+ "blog":"http://bobsblog.com",
+ "following_count":12,
+ "id":43621,
+ "private_gist_count":5,
+ "owned_private_repo_count":0,
+ "total_private_repo_count":0,
+ "followers_count":14,
+ "login":"login",
+ "email":"bob@email.com"
+ }
+}

0 comments on commit bf297ba

Please sign in to comment.