Permalink
Browse files

auth

  • Loading branch information...
1 parent 9d3d726 commit 2cdd263c718ffbe51645abb5264781a779310611 @yayugu committed Feb 16, 2014
View
6 Gemfile
@@ -6,8 +6,7 @@ gem 'rails', '4.0.2'
gem 'sqlite3'
gem 'pg'
-# Use SCSS for stylesheets
-gem 'sass-rails', '~> 4.0.0'
+gem 'slim'
# Use Uglifier as compressor for JavaScript assets
gem 'uglifier', '>= 1.3.0'
@@ -40,3 +39,6 @@ end
# Use debugger
# gem 'debugger', group: [:development, :test]
+
+
+gem 'oauth'
View
13 Gemfile.lock
@@ -52,6 +52,7 @@ GEM
mime-types (1.25.1)
minitest (4.7.5)
multi_json (1.8.4)
+ oauth (0.4.7)
pg (0.17.1)
polyglot (0.3.3)
rack (1.5.2)
@@ -73,14 +74,12 @@ GEM
rake (10.1.1)
rdoc (4.1.1)
json (~> 1.4)
- sass (3.2.14)
- sass-rails (4.0.1)
- railties (>= 4.0.0, < 5.0)
- sass (>= 3.1.10)
- sprockets-rails (~> 2.0.0)
sdoc (0.4.0)
json (~> 1.8)
rdoc (~> 4.0, < 5.0)
+ slim (2.0.2)
+ temple (~> 0.6.6)
+ tilt (>= 1.3.3, < 2.1)
sprockets (2.10.1)
hike (~> 1.2)
multi_json (~> 1.0)
@@ -91,6 +90,7 @@ GEM
activesupport (>= 3.0)
sprockets (~> 2.8)
sqlite3 (1.3.8)
+ temple (0.6.7)
thor (0.18.1)
thread_safe (0.1.3)
atomic
@@ -110,9 +110,10 @@ DEPENDENCIES
coffee-rails (~> 4.0.0)
jbuilder (~> 1.2)
jquery-rails
+ oauth
pg
rails (= 4.0.2)
- sass-rails (~> 4.0.0)
sdoc
+ slim
sqlite3
uglifier (>= 1.3.0)
View
50 app/controllers/auth_controller.rb
@@ -0,0 +1,50 @@
+require 'oauth_twitter'
+
+class AuthController < ApplicationController
+ def auth
+ token = request_token
+ session[:request_token] = token.token
+ session[:request_token_secret] = token.secret
+ redirect_to token.authorize_url
+ end
+
+ def callback
+ @access_token = nil
+ begin
+ @access_token = oauth_twitter.access_token(
+ session[:request_token],
+ session[:request_token_secret],
+ params[:oauth_token],
+ params[:oauth_verifier],
+ )
+ rescue OAuth::Unauthorized => @exception
+ render inline: %{oauth failed: <%=h @exception.message %>}
+ return
+ end
+
+ twitter_user_id = oauth_twitter.user_id(@access_token)
+
+ @user = User.where(twitter_id: twitter_user_id).first
+
+ unless @user
+ @user = User.create!(
+ twitter_id: twitter_user_id,
+ twitter_access_token: @access_token.token,
+ twitter_access_secret: @access_token.secret,
+ )
+ end
+
+ #bookmarklet = URI.encode "javascript:void(function(){location.href = '#{base_url}/#{@id}' + location.pathname;})();"
+ end
+
+ private
+
+ def oauth_twitter
+ OAuthTwitter.new
+ end
+
+ def request_token
+ callback_url = url_for controller: self.controller_name, action: 'callback', only_path: false
+ oauth_twitter.request_token(:oauth_callback => callback_url)
+ end
+end
View
10 app/controllers/feed_controller.rb
@@ -0,0 +1,10 @@
+class FeedController < ApplicationController
+ def user
+ end
+
+ def list
+ end
+
+ def search
+ end
+end
View
2 app/models/user.rb
@@ -0,0 +1,2 @@
+class User < ActiveRecord::Base
+end
View
1 app/views/auth/callback.html.slim
@@ -0,0 +1 @@
+div done
View
66 config/routes.rb
@@ -1,56 +1,14 @@
TwitterGreatRss::Application.routes.draw do
- # The priority is based upon order of creation: first created -> highest priority.
- # See how all your routes lay out with "rake routes".
-
- # You can have the root of your site routed with "root"
- # root 'welcome#index'
-
- # Example of regular route:
- # get 'products/:id' => 'catalog#view'
-
- # Example of named route that can be invoked with purchase_url(id: product.id)
- # get 'products/:id/purchase' => 'catalog#purchase', as: :purchase
-
- # Example resource route (maps HTTP verbs to controller actions automatically):
- # resources :products
-
- # Example resource route with options:
- # resources :products do
- # member do
- # get 'short'
- # post 'toggle'
- # end
- #
- # collection do
- # get 'sold'
- # end
- # end
-
- # Example resource route with sub-resources:
- # resources :products do
- # resources :comments, :sales
- # resource :seller
- # end
-
- # Example resource route with more complex sub-resources:
- # resources :products do
- # resources :comments
- # resources :sales do
- # get 'recent', on: :collection
- # end
- # end
-
- # Example resource route with concerns:
- # concern :toggleable do
- # post 'toggle'
- # end
- # resources :posts, concerns: :toggleable
- # resources :photos, concerns: :toggleable
-
- # Example resource route within a namespace:
- # namespace :admin do
- # # Directs /admin/products/* to Admin::ProductsController
- # # (app/controllers/admin/products_controller.rb)
- # resources :products
- # end
+ root 'main#index'
+
+ scope :auth do
+ get 'auth' => 'auth#auth'
+ get 'callback' => 'auth#callback'
+ end
+
+ scope :feed do
+ get 'user' => 'feed#user'
+ get 'list' => 'feed#list'
+ get 'search' => 'feed#search'
+ end
end
View
14 db/migrate/20140216143921_create_users.rb
@@ -0,0 +1,14 @@
+class CreateUsers < ActiveRecord::Migration
+ def change
+ create_table :users do |t|
+ t.string :twitter_id
+ t.string :twitter_access_token
+ t.string :twitter_access_secret
+ t.string :url_id_hash
+ t.timestamps
+
+ t.index :twitter_id
+ t.index :url_id_hash
+ end
+ end
+end
View
28 db/schema.rb
@@ -0,0 +1,28 @@
+# encoding: UTF-8
+# This file is auto-generated from the current state of the database. Instead
+# of editing this file, please use the migrations feature of Active Record to
+# incrementally modify your database, and then regenerate this schema definition.
+#
+# Note that this schema.rb definition is the authoritative source for your
+# database schema. If you need to create the application database on another
+# system, you should be using db:schema:load, not running all the migrations
+# from scratch. The latter is a flawed and unsustainable approach (the more migrations
+# you'll amass, the slower it'll run and the greater likelihood for issues).
+#
+# It's strongly recommended that you check this file into your version control system.
+
+ActiveRecord::Schema.define(version: 20140216143921) do
+
+ create_table "users", force: true do |t|
+ t.string "twitter_id"
+ t.string "twitter_access_token"
+ t.string "twitter_access_secret"
+ t.string "url_id_hash"
+ t.datetime "created_at"
+ t.datetime "updated_at"
+ end
+
+ add_index "users", ["twitter_id"], name: "index_users_on_twitter_id"
+ add_index "users", ["url_id_hash"], name: "index_users_on_url_id_hash"
+
+end
View
39 lib/oauth_twitter.rb
@@ -0,0 +1,39 @@
+require 'oauth'
+require 'json'
+
+class OAuthTwitter
+ SITE_URL = 'https://api.twitter.com'
+
+ def request_token(hash)
+ self.consumer.get_request_token(hash)
+ end
+
+ def access_token(request_token, request_token_secret, oauth_token, oauth_verifier)
+ request_token = OAuth::RequestToken.new(self.consumer, request_token, request_token_secret)
+ request_token.get_access_token(
+ {},
+ :oauth_token => oauth_token,
+ :oauth_verifier => oauth_verifier
+ )
+ end
+
+ def get_and_json_parse(url, account)
+ body = OAuth::AccessToken.new(self.consumer, account.access_token, account.access_secret)
+ .get(url)
+ .body
+ JSON.parse(body)
+ end
+
+ def consumer
+ OAuth::Consumer.new(
+ ENV['CONSUMER_KEY'],
+ ENV['CONSUMER_SECRET'],
+ site: SITE_URL
+ )
+ end
+
+ def user_id(access_token)
+ body = access_token.get("#{SITE_URL}/1.1/account/verify_credentials.json").body
+ JSON.parse(body)['id_str']
+ end
+end
View
11 test/fixtures/users.yml
@@ -0,0 +1,11 @@
+# Read about fixtures at http://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html
+
+# This model initially had no columns defined. If you add columns to the
+# model remove the '{}' from the fixture names and add the columns immediately
+# below each fixture, per the syntax in the comments below
+#
+one: {}
+# column: value
+#
+two: {}
+# column: value
View
7 test/models/user_test.rb
@@ -0,0 +1,7 @@
+require 'test_helper'
+
+class UserTest < ActiveSupport::TestCase
+ # test "the truth" do
+ # assert true
+ # end
+end

0 comments on commit 2cdd263

Please sign in to comment.