Sha256: 4f40807e5f80c21a14ce93482a8b61cc8376699aee8fb6e22c243a28aa955341

Contents?: true

Size: 1.66 KB

Versions: 3

Compression:

Stored size: 1.66 KB

Contents

require 'net/http'

module TwitterAuth
  module BasicUser
    def self.included(base)
      base.class_eval do
        attr_accessor :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

3 entries across 3 versions & 1 rubygems

Version Path
twitter-auth-with-mongo-mapper-0.1.1 app/models/twitter_auth/basic_user.rb
twitter-auth-with-mongo-mapper-0.1.0 app/models/twitter_auth/basic_user.rb
twitter-auth-with-mongo-mapper-0.0.9 app/models/twitter_auth/basic_user.rb