From 099a3503932edf9ad85da439bc07d4f179b057d3 Mon Sep 17 00:00:00 2001 From: "Fuji, Goro" Date: Fri, 8 Aug 2014 18:56:56 +0900 Subject: [PATCH 1/4] [WIP] github (including GHE) sign-in --- app/assets/javascripts/sessions.js | 2 ++ app/assets/javascripts/users.js | 2 ++ app/assets/stylesheets/sessions.css.scss | 3 +++ app/assets/stylesheets/users.css.scss | 3 +++ app/controllers/application_controller.rb | 9 +++++++++ app/controllers/sessions_controller.rb | 8 ++++++++ app/controllers/users_controller.rb | 2 ++ app/helpers/sessions_helper.rb | 2 ++ app/helpers/users_helper.rb | 2 ++ app/models/user.rb | 11 +++++++++++ app/views/layouts/application.html.slim | 1 + config/initializers/omniauth_github.rb | 8 +++++++- config/routes.rb | 5 +++++ config/secrets.yml.sample | 3 +++ db/migrate/20140808095155_create_users.rb | 11 +++++++++++ test/controllers/sessions_controller_test.rb | 7 +++++++ test/controllers/users_controller_test.rb | 7 +++++++ test/fixtures/users.yml | 11 +++++++++++ test/helpers/sessions_helper_test.rb | 4 ++++ test/helpers/users_helper_test.rb | 4 ++++ test/models/user_test.rb | 7 +++++++ 21 files changed, 111 insertions(+), 1 deletion(-) create mode 100644 app/assets/javascripts/sessions.js create mode 100644 app/assets/javascripts/users.js create mode 100644 app/assets/stylesheets/sessions.css.scss create mode 100644 app/assets/stylesheets/users.css.scss create mode 100644 app/controllers/sessions_controller.rb create mode 100644 app/controllers/users_controller.rb create mode 100644 app/helpers/sessions_helper.rb create mode 100644 app/helpers/users_helper.rb create mode 100644 app/models/user.rb create mode 100644 db/migrate/20140808095155_create_users.rb create mode 100644 test/controllers/sessions_controller_test.rb create mode 100644 test/controllers/users_controller_test.rb create mode 100644 test/fixtures/users.yml create mode 100644 test/helpers/sessions_helper_test.rb create mode 100644 test/helpers/users_helper_test.rb create mode 100644 test/models/user_test.rb diff --git a/app/assets/javascripts/sessions.js b/app/assets/javascripts/sessions.js new file mode 100644 index 0000000..dee720f --- /dev/null +++ b/app/assets/javascripts/sessions.js @@ -0,0 +1,2 @@ +// Place all the behaviors and hooks related to the matching controller here. +// All this logic will automatically be available in application.js. diff --git a/app/assets/javascripts/users.js b/app/assets/javascripts/users.js new file mode 100644 index 0000000..dee720f --- /dev/null +++ b/app/assets/javascripts/users.js @@ -0,0 +1,2 @@ +// Place all the behaviors and hooks related to the matching controller here. +// All this logic will automatically be available in application.js. diff --git a/app/assets/stylesheets/sessions.css.scss b/app/assets/stylesheets/sessions.css.scss new file mode 100644 index 0000000..7bef9cf --- /dev/null +++ b/app/assets/stylesheets/sessions.css.scss @@ -0,0 +1,3 @@ +// Place all the styles related to the sessions controller here. +// They will automatically be included in application.css. +// You can use Sass (SCSS) here: http://sass-lang.com/ diff --git a/app/assets/stylesheets/users.css.scss b/app/assets/stylesheets/users.css.scss new file mode 100644 index 0000000..1efc835 --- /dev/null +++ b/app/assets/stylesheets/users.css.scss @@ -0,0 +1,3 @@ +// Place all the styles related to the users controller here. +// They will automatically be included in application.css. +// You can use Sass (SCSS) here: http://sass-lang.com/ diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index d83690e..6266d96 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -2,4 +2,13 @@ class ApplicationController < ActionController::Base # Prevent CSRF attacks by raising an exception. # For APIs, you may want to use :null_session instead. protect_from_forgery with: :exception + + heper_method :current_user + + private + + # @return [User] + def current_user + @current_user ||= User.find(session[:user_id]) if session + end end diff --git a/app/controllers/sessions_controller.rb b/app/controllers/sessions_controller.rb new file mode 100644 index 0000000..aabd98a --- /dev/null +++ b/app/controllers/sessions_controller.rb @@ -0,0 +1,8 @@ +class SessionsController < ApplicationController + + def destroy + sessions[:user_id] = nil + redirect_to root_path, notice: 'Signed out!' + end + +end diff --git a/app/controllers/users_controller.rb b/app/controllers/users_controller.rb new file mode 100644 index 0000000..3e74dea --- /dev/null +++ b/app/controllers/users_controller.rb @@ -0,0 +1,2 @@ +class UsersController < ApplicationController +end diff --git a/app/helpers/sessions_helper.rb b/app/helpers/sessions_helper.rb new file mode 100644 index 0000000..309f8b2 --- /dev/null +++ b/app/helpers/sessions_helper.rb @@ -0,0 +1,2 @@ +module SessionsHelper +end diff --git a/app/helpers/users_helper.rb b/app/helpers/users_helper.rb new file mode 100644 index 0000000..2310a24 --- /dev/null +++ b/app/helpers/users_helper.rb @@ -0,0 +1,2 @@ +module UsersHelper +end diff --git a/app/models/user.rb b/app/models/user.rb new file mode 100644 index 0000000..6029a6d --- /dev/null +++ b/app/models/user.rb @@ -0,0 +1,11 @@ +class User < ActiveRecord::Base + + # @return [User] + def self.create_with_omniauth(auth) + create! do |user| + user.provider = auth["provider"] + user.uid = auth["uid"] + user.name = auth["info"]["name"] + end + end +end diff --git a/app/views/layouts/application.html.slim b/app/views/layouts/application.html.slim index 2bc7295..132fdcc 100644 --- a/app/views/layouts/application.html.slim +++ b/app/views/layouts/application.html.slim @@ -27,6 +27,7 @@ html lang="en" a.brand href="/"= site_name .container.nav-collapse ul.nav + li= link_to "Sign in", "/auth/github" li= link_to "Project on GitHub", "http://github.com/android-frontier" .container style="margin-top: 64px" diff --git a/config/initializers/omniauth_github.rb b/config/initializers/omniauth_github.rb index fcaef06..d602248 100644 --- a/config/initializers/omniauth_github.rb +++ b/config/initializers/omniauth_github.rb @@ -4,5 +4,11 @@ ) config = Rails.application.secrets - provider :github, config['github_key'], config['github_secret'], scope: scope + provider :github, config['github_key'], config['github_secret'], scope: scope, + client_options: { + site: config['github_site'], + authorize_url: config['github_authorize_url'], + token_url: config['github_token_url'], + } + end diff --git a/config/routes.rb b/config/routes.rb index 3c79ba5..fc7c010 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -20,4 +20,9 @@ } end end + + + # sessions + get "/auth/:provider/callback" => "sessions#create" + get "/signout" => "sessions#destroy", :as => :signout end diff --git a/config/secrets.yml.sample b/config/secrets.yml.sample index c2279c7..5989cf8 100644 --- a/config/secrets.yml.sample +++ b/config/secrets.yml.sample @@ -9,6 +9,9 @@ development: artifact_root_path: <%= ENV["ARTIFACT_ROOT_PATH"] %> github_key: <%= ENV["GITHUB_KEY"] %> github_secret: <%= ENV["GITHUB_SECRET"] %> + github_site: 'https://api.github.com/' + github_authorize_url: 'https://github.com/login/oauth/authorize' + github_token_url: 'https://github.com/login/oauth/access_token' site: *site test: diff --git a/db/migrate/20140808095155_create_users.rb b/db/migrate/20140808095155_create_users.rb new file mode 100644 index 0000000..cb4a56e --- /dev/null +++ b/db/migrate/20140808095155_create_users.rb @@ -0,0 +1,11 @@ +class CreateUsers < ActiveRecord::Migration + def change + create_table :users do |t| + t.string :provider + t.string :uid + t.string :name + + t.timestamps + end + end +end diff --git a/test/controllers/sessions_controller_test.rb b/test/controllers/sessions_controller_test.rb new file mode 100644 index 0000000..d30ebc3 --- /dev/null +++ b/test/controllers/sessions_controller_test.rb @@ -0,0 +1,7 @@ +require 'test_helper' + +class SessionsControllerTest < ActionController::TestCase + # test "the truth" do + # assert true + # end +end diff --git a/test/controllers/users_controller_test.rb b/test/controllers/users_controller_test.rb new file mode 100644 index 0000000..d23f182 --- /dev/null +++ b/test/controllers/users_controller_test.rb @@ -0,0 +1,7 @@ +require 'test_helper' + +class UsersControllerTest < ActionController::TestCase + # test "the truth" do + # assert true + # end +end diff --git a/test/fixtures/users.yml b/test/fixtures/users.yml new file mode 100644 index 0000000..2f72a66 --- /dev/null +++ b/test/fixtures/users.yml @@ -0,0 +1,11 @@ +# Read about fixtures at http://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html + +one: + provider: MyString + uid: MyString + name: MyString + +two: + provider: MyString + uid: MyString + name: MyString diff --git a/test/helpers/sessions_helper_test.rb b/test/helpers/sessions_helper_test.rb new file mode 100644 index 0000000..7d44e09 --- /dev/null +++ b/test/helpers/sessions_helper_test.rb @@ -0,0 +1,4 @@ +require 'test_helper' + +class SessionsHelperTest < ActionView::TestCase +end diff --git a/test/helpers/users_helper_test.rb b/test/helpers/users_helper_test.rb new file mode 100644 index 0000000..96af37a --- /dev/null +++ b/test/helpers/users_helper_test.rb @@ -0,0 +1,4 @@ +require 'test_helper' + +class UsersHelperTest < ActionView::TestCase +end diff --git a/test/models/user_test.rb b/test/models/user_test.rb new file mode 100644 index 0000000..82f61e0 --- /dev/null +++ b/test/models/user_test.rb @@ -0,0 +1,7 @@ +require 'test_helper' + +class UserTest < ActiveSupport::TestCase + # test "the truth" do + # assert true + # end +end From fe717b1bdc345936ac1a549682a51aaecf319521 Mon Sep 17 00:00:00 2001 From: "Fuji, Goro" Date: Fri, 22 Aug 2014 17:38:15 +0900 Subject: [PATCH 2/4] fix things --- .travis.yml | 1 + app/controllers/application_controller.rb | 2 +- db/schema.rb | 10 +++++++++- 3 files changed, 11 insertions(+), 2 deletions(-) diff --git a/.travis.yml b/.travis.yml index 8b624b3..3014581 100644 --- a/.travis.yml +++ b/.travis.yml @@ -3,3 +3,4 @@ rvm: - 2.1.1 before_install: - cp config/secrets.yml.sample config/secrets.yml + - bin/rake db:migrate RAILS_ENV=test diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index 6266d96..c02c56f 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -3,7 +3,7 @@ class ApplicationController < ActionController::Base # For APIs, you may want to use :null_session instead. protect_from_forgery with: :exception - heper_method :current_user + helper_method :current_user private diff --git a/db/schema.rb b/db/schema.rb index 4dfbb16..8eee1b1 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -11,6 +11,14 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema.define(version: 0) do +ActiveRecord::Schema.define(version: 20140808095155) do + + create_table "users", force: true do |t| + t.string "provider" + t.string "uid" + t.string "name" + t.datetime "created_at" + t.datetime "updated_at" + end end From 06055b355797ff0dacbacb4f26f57a776a42e996 Mon Sep 17 00:00:00 2001 From: "Fuji, Goro" Date: Fri, 22 Aug 2014 17:42:32 +0900 Subject: [PATCH 3/4] travis --- .travis.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/.travis.yml b/.travis.yml index 3014581..8ffbdcb 100644 --- a/.travis.yml +++ b/.travis.yml @@ -3,4 +3,5 @@ rvm: - 2.1.1 before_install: - cp config/secrets.yml.sample config/secrets.yml +before_script: - bin/rake db:migrate RAILS_ENV=test From a5d3e356935e173eeac116b44115543ad187296b Mon Sep 17 00:00:00 2001 From: "Fuji, Goro" Date: Fri, 22 Aug 2014 18:33:48 +0900 Subject: [PATCH 4/4] add SessionsController#create --- app/controllers/sessions_controller.rb | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/app/controllers/sessions_controller.rb b/app/controllers/sessions_controller.rb index aabd98a..7011a92 100644 --- a/app/controllers/sessions_controller.rb +++ b/app/controllers/sessions_controller.rb @@ -1,5 +1,12 @@ class SessionsController < ApplicationController + def create + auth = request.env["omniauth.auth"] + user = User.find_by_provider_and_uid(auth["provider"], auth["uid"]) || User.create_with_omniauth(auth) + session[:user_id] = user.id + redirect_to root_url, :notice => "Signed in!" + end + def destroy sessions[:user_id] = nil redirect_to root_path, notice: 'Signed out!'