Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # This controller's job is to exchange twitter credentials for Shortmail credentials
- class TwitterReverseAuthController < ApplicationController
- def api_key_exchange
- auth = OauthAuthenticator.new(params)
- if auth.is_valid?
- render :json => { :api_key => auth.response[:api_key] }
- else
- render :json => {
- :error => auth.response[:errors].full_messages.join(", "),
- :status => :unprocessable_entity
- }
- end
- end
- class OauthAuthenticate < OpenStruct
- #magic_attrs: screen_name,oauth_token,oauth_secret,token,description
- #public
- def is_valid?
- self.validate_device
- self.valid?
- end
- def response
- {:errors => self.errors, :api_key =>self.device && self.device.api_key}
- end
- #private
- include ActiveModel::Validations; def to_model; self; end
- def find_user
- User.by_screen_name(screen_name).
- where(:oauth_token => oauth_token,
- :oauth_secret => oauth_secret).first
- end
- def find_device
- Device.find_or_create_by_token({
- :token => token,
- :description => description,
- :user_id => user.id
- })
- end
- validates_presence_of :screen_name, :oauth_token, :oauth_secret, :message => "required"
- def validate_user
- return self unless valid?
- self.user = find_user
- self.errors.add(:user, 'not found') unless user
- return self
- end
- def validate_device
- return self unless validate_user && valid?
- self.device = find_device
- unless device
- self.errors.add(:device, 'not found')
- device.errors.each do |k,m|
- self.errors.add(k,m)
- end unless device.saved?
- end
- return self
- end
- end
- end
Add Comment
Please, Sign In to add comment