Sha256: 2de7cfbaa7c5819a79a6e26a0edb2f6dcc8c637b5c79838858850519b73c1bb7

Contents?: true

Size: 1.66 KB

Versions: 6

Compression:

Stored size: 1.66 KB

Contents

require 'net/http'

module TwitterAuth
  module BasicUser
    def self.included(base)
      base.class_eval do
        attr_protected :crypted_password, :salt
      end

      base.extend TwitterAuth::BasicUser::ClassMethods
    end

    module ClassMethods
      def verify_credentials(login, password)
        response = TwitterAuth.net.start { |http|
          request = Net::HTTP::Get.new('/account/verify_credentials.json')
          request.basic_auth login, password
          http.request(request)
        }

        if response.code == '200'
          JSON.parse(response.body)
        else
          false
        end
      end

      def authenticate(login, password)
        if twitter_hash = verify_credentials(login, password)
          user = identify_or_create_from_twitter_hash_and_password(twitter_hash, password)
          user
        else
          nil
        end
      end

      def identify_or_create_from_twitter_hash_and_password(twitter_hash, password)
        if user = User.find_by_twitter_id(twitter_hash['id'].to_s)
          user.login = twitter_hash['screen_name']
          user.assign_twitter_attributes(twitter_hash)
          user.password = password
          user.save
          user
        else
          user = User.new_from_twitter_hash(twitter_hash)
          user.password = password
          user.save
          user
        end
      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

Version data entries

6 entries across 6 versions & 2 rubygems

Version Path
mbleigh-twitter-auth-0.1.20 app/models/twitter_auth/basic_user.rb
mbleigh-twitter-auth-0.1.21 app/models/twitter_auth/basic_user.rb
mbleigh-twitter-auth-0.1.22 app/models/twitter_auth/basic_user.rb
twitter-auth-0.1.22 app/models/twitter_auth/basic_user.rb
twitter-auth-0.1.20 app/models/twitter_auth/basic_user.rb
twitter-auth-0.1.21 app/models/twitter_auth/basic_user.rb