Permalink
Browse files

Initial attempt at OAuth2 with Google

  • Loading branch information...
soundasleep committed Aug 21, 2014
1 parent c1e9b4b commit 6f83482fe0d25fa05ddc24c9020b69afefddf0a2
@@ -57,3 +57,5 @@ gem 'activerecord-session_store'
gem 'bourbon', '>= 4.0.0'
gem 'bitters', :git => 'https://github.com/soundasleep/bitters.git'
gem 'neat'

gem 'oauth2'
@@ -52,6 +52,8 @@ GEM
coffee-script-source (1.7.1)
erubis (2.7.0)
execjs (2.2.1)
faraday (0.9.0)
multipart-post (>= 1.2, < 3)
haml (4.0.5)
tilt
hike (1.2.3)
@@ -63,15 +65,24 @@ GEM
railties (>= 3.0, < 5.0)
thor (>= 0.14, < 2.0)
json (1.8.1)
jwt (1.0.0)
mail (2.5.4)
mime-types (~> 1.16)
treetop (~> 1.4.8)
mime-types (1.25.1)
minitest (4.7.5)
multi_json (1.10.1)
multi_xml (0.5.5)
multipart-post (2.0.0)
neat (1.6.0)
bourbon (>= 3.1)
sass (>= 3.3)
oauth2 (1.0.0)
faraday (>= 0.8, < 0.10)
jwt (~> 1.0)
multi_json (~> 1.3)
multi_xml (~> 0.5)
rack (~> 1.2)
polyglot (0.3.5)
rack (1.5.2)
rack-test (0.6.2)
@@ -136,6 +147,7 @@ DEPENDENCIES
jbuilder (~> 1.2)
jquery-rails
neat
oauth2
rails (= 4.0.3)
sass-rails (~> 4.0.0)
sdoc
@@ -0,0 +1,3 @@
# Place all the behaviors and hooks related to the matching controller here.
# All this logic will automatically be available in application.js.
# You can use CoffeeScript in this file: http://coffeescript.org/
@@ -20,9 +20,12 @@

@import "navigation";
@import "logs";

// each controller
@import "player";
@import "world";
@import "scores";
@import "security";

body {
margin: 10px;
@@ -0,0 +1,3 @@
// Place all the styles related to the Security controller here.
// They will automatically be included in application.css.
// You can use Sass (SCSS) here: http://sass-lang.com/
@@ -0,0 +1,42 @@
class SecurityController < ApplicationController
def redirect_uri
'http://localhost:3000/security/oauth2callback'
end

def client
require 'oauth2'

if Rails.env == "development"
OpenSSL::SSL.const_set(:VERIFY_PEER, OpenSSL::SSL::VERIFY_NONE)
end

client_id = '946722529018-k9qfuast1j4ld84sqqsqrc08g883mlt2.apps.googleusercontent.com'
client_secret = '0t40pjkcp4lF9QumwN3L0Zpg'

# site = "https://accounts.google.com/o/oauth2/auth"
# site = "https://accounts.google.com/o/oauth2/auth?scope=openid%20email"
site = "https://accounts.google.com"

return OAuth2::Client.new(client_id, client_secret, :site => site, :authorize_url => "/o/oauth2/auth", :token_url => "/o/oauth2/token")
end

def login
@url = client.auth_code.authorize_url(:redirect_uri => redirect_uri, :scope => "openid email")
redirect_to(@url)
end

def oauth2callback
begin
code = params[:code]
@token = client.auth_code.get_token(code, :redirect_uri => redirect_uri)

# redirect_to("/security/success")
render "success"
rescue OAuth2::Error
redirect_to("/security/login")
end
end

def success
end
end
@@ -0,0 +1,2 @@
module SecurityHelper
end
@@ -18,3 +18,6 @@
- if current_player
Score: #{current_player.score}
= link_to "High scores", controller: 'scores'

%li.login
= link_to "Login", controller: 'security', action: 'login'
@@ -0,0 +1,9 @@
%li= session[:token]
%li= @token
%li= @token.inspect
%ol
- @token.params.each do |key, value|
%li #{key} = #{value}
%li base64 decoded = #{Base64.decode64(@token.params['id_token'])}
%li expired = #{@token.expired?}
%li token = #{@token.token}
@@ -32,6 +32,10 @@
get "scores/index"
get "scores" => "scores#index"

get "security/login"
get "security" => "security#login"
get "security/oauth2callback"

# Example resource route with options:
# resources :products do
# member do
@@ -0,0 +1,7 @@
require 'test_helper'

class SecurityControllerTest < ActionController::TestCase
# test "the truth" do
# assert true
# end
end
@@ -0,0 +1,4 @@
require 'test_helper'

class SecurityHelperTest < ActionView::TestCase
end

0 comments on commit 6f83482

Please sign in to comment.