Skip to content

Commit

Permalink
Refactored OauthUser and BasicUser into modules (how they should have…
Browse files Browse the repository at this point in the history
… been from the beginning) and started spec'ing out BasicUser.
  • Loading branch information
Michael Bleigh committed Mar 19, 2009
1 parent 46d4329 commit 52bb18e
Show file tree
Hide file tree
Showing 11 changed files with 104 additions and 32 deletions.
18 changes: 16 additions & 2 deletions app/models/twitter_auth/basic_user.rb
@@ -1,6 +1,20 @@
module TwitterAuth
class BasicUser < TwitterAuth::GenericUser
attr_protected :cryped_password, :salt
module BasicUser
def self.included(base)
base.class_eval do
attr_protected :crypted_password, :salt
end
end

def password=(new_password)
encrypted = TwitterAuth::Cryptify.encrypt(new_password)
self.crypted_password = encrypted[:encrypted_data]
self.salt = encrypted[:salt]
end

def password
TwitterAuth::Cryptify.decrypt(self.crypted_password, self.salt)
end
end
end

6 changes: 6 additions & 0 deletions app/models/twitter_auth/generic_user.rb
Expand Up @@ -52,5 +52,11 @@ def update_twitter_attributes(hash)
assign_twitter_attributes(hash)
save
end

if TwitterAuth.oauth?
include TwitterAuth::OauthUser
else
include TwitterAuth::BasicUser
end
end
end
42 changes: 25 additions & 17 deletions app/models/twitter_auth/oauth_user.rb
@@ -1,26 +1,34 @@
module TwitterAuth
class OauthUser < TwitterAuth::GenericUser
attr_protected :access_token, :access_secret
module OauthUser
def self.included(base)
base.class_eval do
attr_protected :access_token, :access_secret
end

def self.identify_or_create_from_access_token(token, secret=nil)
raise ArgumentError, 'Must authenticate with an OAuth::AccessToken or the string access token and secret.' unless (token && secret) || token.is_a?(OAuth::AccessToken)
base.extend TwitterAuth::OauthUser::ClassMethods
end

module ClassMethods
def identify_or_create_from_access_token(token, secret=nil)
raise ArgumentError, 'Must authenticate with an OAuth::AccessToken or the string access token and secret.' unless (token && secret) || token.is_a?(OAuth::AccessToken)

user_info = JSON.parse(token.get('/account/verify_credentials.json').body)
user_info = JSON.parse(token.get('/account/verify_credentials.json').body)

if user = User.find_by_login(user_info['screen_name'])
user.update_twitter_attributes(user_info)
user
else
User.create_from_twitter_hash_and_token(user_info, token)
if user = User.find_by_login(user_info['screen_name'])
user.update_twitter_attributes(user_info)
user
else
User.create_from_twitter_hash_and_token(user_info, token)
end
end
end

def self.create_from_twitter_hash_and_token(user_info, access_token)
user = User.new_from_twitter_hash(user_info)
user.access_token = access_token.token
user.access_secret = access_token.secret
user.save
user
def create_from_twitter_hash_and_token(user_info, access_token)
user = User.new_from_twitter_hash(user_info)
user.access_token = access_token.token
user.access_secret = access_token.secret
user.save
user
end
end

def token
Expand Down
2 changes: 1 addition & 1 deletion generators/twitter_auth/templates/user.rb
@@ -1,4 +1,4 @@
class User < TwitterAuth::<%= options[:oauth] ? "Oauth" : "Basic" %>User
class User < TwitterAuth::GenericUser
# Extend and define your user model as you see fit.
# All of the authentication logic is handled by the
# parent TwitterAuth user class.
Expand Down
2 changes: 2 additions & 0 deletions lib/twitter_auth.rb
Expand Up @@ -28,6 +28,8 @@ def self.strategy
strat = config['strategy']
raise ArgumentError, 'Invalid TwitterAuth Strategy: Valid strategies are oauth and basic.' unless %w(oauth basic).include?(strat)
strat.to_sym
rescue Errno::ENOENT
:oauth
end

def self.oauth?
Expand Down
1 change: 1 addition & 0 deletions rails/init.rb
@@ -1,6 +1,7 @@
# Gem Dependencies
config.gem 'oauth'
config.gem 'ezcrypto'
config.gem 'httparty'

require 'json'
require 'twitter_auth'
Expand Down
3 changes: 1 addition & 2 deletions spec/fixtures/factories.rb
Expand Up @@ -11,8 +11,7 @@

Factory.define(:twitter_basic_user, :class => User) do |u|
u.login 'tweetkid'
u.crypted_password 'fixthislater'
u.salt 'ohsosalty'
u.password 'test'

u.name 'Tweet Kid'
u.description 'Twitter Man\'s trusty sidekick.'
Expand Down
40 changes: 40 additions & 0 deletions spec/models/twitter_auth/basic_user_spec.rb
@@ -0,0 +1,40 @@
require File.dirname(__FILE__) + '/../../spec_helper'

describe TwitterAuth::BasicUser do
before do
stub_basic!
end

describe '#password=' do
before do
@user = Factory.build(:twitter_basic_user)
end

it 'should change the value of crypted_password' do
lambda{@user.password = 'newpass'}.should change(@user, :crypted_password)
end

it 'should change the value of salt' do
lambda{@user.password = 'newpass'}.should change(@user, :salt)
end

it 'should not store the plaintext password' do
@user.password = 'newpass'
@user.crypted_password.should_not == 'newpass'
end
end

describe '#password' do
before do
@user = Factory.build(:twitter_basic_user, :password => 'monkey')
end

it 'should return the password' do
@user.password.should == 'monkey'
end

it 'should not be a database attribute' do
@user['password'].should_not == 'monkey'
end
end
end
2 changes: 1 addition & 1 deletion spec/schema.rb
Expand Up @@ -7,7 +7,7 @@
t.string :access_secret

# Basic fields
t.string :crypted_password
t.binary :crypted_password
t.string :salt

# This information is automatically kept
Expand Down
18 changes: 9 additions & 9 deletions spec/spec_helper.rb
Expand Up @@ -11,10 +11,7 @@ class TwitterAuth::GenericUser
def self.table_name; 'twitter_auth_users' end
end

Object.send(:remove_const, :User)
class User < TwitterAuth::OauthUser

end
class User < TwitterAuth::GenericUser; end

require 'remarkable'
require File.dirname(__FILE__) + '/fixtures/factories'
Expand All @@ -27,12 +24,11 @@ class User < TwitterAuth::OauthUser
load(File.dirname(__FILE__) + '/schema.rb')

def define_basic_user_class!
Object.remove_const(:User)
Object.class_eval <<-RUBY
class User < TwitterAuth::BasicUser
TwitterAuth::GenericUser.send :include, TwitterAuth::BasicUser
end

end
RUBY
def define_oauth_user_class!
TwitterAuth::GenericUser.send :include, TwitterAuth::OauthUser
end

def stub_oauth!
Expand All @@ -41,11 +37,15 @@ def stub_oauth!
'oauth_consumer_key' => 'testkey',
'oauth_consumer_secret' => 'testsecret'
})
define_oauth_user_class!
end

def stub_basic!
TwitterAuth.stub!(:config).and_return({
'strategy' => 'basic',
'encryption_key' => 'secretcode'
})
define_basic_user_class!
end

define_oauth_user_class!
2 changes: 2 additions & 0 deletions spec/twitter_auth_spec.rb
Expand Up @@ -3,6 +3,7 @@
describe TwitterAuth do
describe '#base_url' do
it 'should have default to https://twitter.com' do
TwitterAuth.stub!(:config).and_return({})
TwitterAuth.base_url.should == 'https://twitter.com'
end

Expand Down Expand Up @@ -51,6 +52,7 @@

describe '#strategy' do
it 'should pull and symbolize from the config' do
TwitterAuth.stub!(:config).and_return({'strategy' => 'oauth'})
TwitterAuth.strategy.should == TwitterAuth.config['strategy'].to_sym
end

Expand Down

0 comments on commit 52bb18e

Please sign in to comment.