jam-cloud/web/app/controllers/sessions_controller.rb

173 lines
6.2 KiB
Ruby
Raw Normal View History

2012-09-03 22:03:16 +00:00
# this is not a jam session - this is an 'auth session'
class SessionsController < ApplicationController
layout "web"
def new
@login_error = false
2013-06-02 19:27:46 +00:00
render :layout => "landing"
end
def create
2012-11-14 05:57:10 +00:00
user = User.authenticate(params[:session][:email], params[:session][:password])
if user.nil?
@login_error = true
render 'new', :layout => "landing"
2012-11-14 05:57:10 +00:00
else
if jkclient_agent?
user.update_progression_field(:first_ran_client_at)
end
@session_only_cookie = !jkclient_agent? && !params[:user].nil? && 0 == params[:user][:remember_me].to_i
2012-11-15 08:47:19 +00:00
complete_sign_in user
end
end
2012-11-15 09:30:30 +00:00
# OAuth docs
# http://net.tutsplus.com/tutorials/ruby/how-to-use-omniauth-to-authenticate-your-users/
2012-11-12 20:12:32 +00:00
def create_oauth
auth_hash = request.env['omniauth.auth']
2012-11-15 06:18:37 +00:00
authorization = UserAuthorization.find_by_provider_and_uid(auth_hash["provider"], auth_hash["uid"])
if authorization
# Sign in for a user who has already registered.
complete_sign_in authorization.user
else
# Sign up for a completely new user.
# First/last name: auth_hash["info"]["first_name"] and auth_hash["info"]["last_name"]
# token: auth_hash["credentials"]["token"] -- "expires_at"
#
# For debugging - to see what all is there:
# render :text => auth_hash.to_yaml
2012-11-15 08:47:19 +00:00
#FbGraph.debug!
token = auth_hash[:credentials][:token]
# FIXME:
# This should probably be in a transaction somehow, meaning the user
# create and the authorization create. Concern is UserManager.new.signup sends
# an email and whatnot.
#
# Also, should we grab their photo from facebook?
2014-02-03 21:19:14 +00:00
user = UserManager.new.signup(remote_ip: remote_ip(),
first_name: auth_hash[:info][:first_name],
last_name: auth_hash[:info][:last_name],
email: auth_hash[:info][:email])
2012-11-20 09:48:48 +00:00
# Users who sign up using oauth are presumed to have valid email adddresses.
user.confirm_email!
2012-11-22 07:52:13 +00:00
auth = user.user_authorizations.build :provider => auth_hash[:provider],
:uid => auth_hash[:uid],
:token => auth_hash[:credentials][:token],
:token_expiration => Time.at(auth_hash[:credentials][:expires_at])
2012-11-15 06:18:37 +00:00
user.save
complete_sign_in user
end
end
# https://github.com/intridea/omniauth/wiki/Saving-User-Location
2013-09-07 07:59:55 +00:00
def oauth_callback
2014-02-03 21:19:14 +00:00
auth_hash = request.env['omniauth.auth']
provider = auth_hash[:provider]
if provider == 'twitter'
@user_authorization = current_user.build_twitter_authorization(auth_hash)
if !@user_authorization.save
# this is a very poorly styled page, but it's better than a server error.
# the only reason this happens is because some other account has authed this twitter acct
render "twitter_oauth_failure"
return
end
redirect_to request.env['omniauth.origin'] || '/'
return
elsif provider == 'facebook'
2014-02-03 21:19:14 +00:00
fb_uid = auth_hash[:uid]
token = auth_hash[:credentials][:token]
token_expiration = Time.at(auth_hash[:credentials][:expires_at])
first_name = auth_hash[:extra][:raw_info][:first_name]
last_name = auth_hash[:extra][:raw_info][:last_name]
email = auth_hash[:extra][:raw_info][:email]
gender = auth_hash[:extra][:raw_info][:gender]
fb_signup = FacebookSignup.new
fb_signup.uid = fb_uid
fb_signup.token = token
fb_signup.token_expires_at = token_expiration
fb_signup.first_name = first_name
fb_signup.last_name = last_name
fb_signup.email = email
if gender == 'male'
fb_signup.gender = 'M'
elsif gender == 'female'
fb_signup.gender = 'F'
end
fb_signup.save!
redirect_to "#{signup_path}?facebook_signup=#{fb_signup.lookup_id}"
return
end
2013-09-07 07:59:55 +00:00
if current_user.nil?
render :nothing => true, :status => 404
return
end
2013-09-10 00:56:55 +00:00
#authorization = UserAuthorization.find_by_provider_and_uid(auth_hash["provider"], auth_hash["uid"])
2013-09-07 07:59:55 +00:00
2013-09-10 00:56:55 +00:00
# Always make and save a new authorization. This is because they expire, and honestly there's no cost
# to just making and saving it.
#if authorization.nil?
2014-02-03 21:19:14 +00:00
authorization = current_user.user_authorizations.build :provider => auth_hash[:provider],
2013-09-07 07:59:55 +00:00
:uid => auth_hash[:uid],
:token => auth_hash[:credentials][:token],
:token_expiration => Time.at(auth_hash[:credentials][:expires_at])
authorization.save
2013-09-10 00:56:55 +00:00
#end
2013-09-07 07:59:55 +00:00
render 'oauth_complete', :layout => "landing"
2013-09-07 07:59:55 +00:00
end
2012-11-15 06:18:37 +00:00
def complete_sign_in(user)
sign_in user
2013-07-01 01:06:22 +00:00
if !params[:sso].nil? && params[:sso] == "desk"
2013-07-04 21:11:20 +00:00
# generate multipass token and sign it
multipass = DeskMultipass.new(user)
callback_url = SampleApp::Application.config.multipass_callback_url
2013-07-04 21:21:35 +00:00
redirect_to "#{callback_url}?multipass=#{multipass.token}&signature=#{multipass.signature}"
2013-07-01 01:06:22 +00:00
else
redirect_back_or client_url
end
2012-11-12 20:12:32 +00:00
end
def destroy
# earlier, code here would delete the connection using client_id from cookies
# however, we should never try to delete the client_id cookie (make it as permanent as possible)
# also, because the client will stop heartbeating and close the connection to gateway,
# in any case the server will notice after 10 seconds that the user is gone.
# if we really want someone to know right away that the client is gone, then just make sure the client calls
# leave session before it calls delete (VRFS-617 should solve that)
sign_out
redirect_to client_url
end
2012-11-12 20:12:32 +00:00
def failure
redirect_to request.query_parameters['origin'] || '/'
2012-11-12 20:12:32 +00:00
end
2013-02-28 18:44:33 +00:00
def connection_state
if (defined?(TEST_CONNECT_STATES) && TEST_CONNECT_STATES) || 'development'==Rails.env
@prefix = defined?(TEST_CONNECT_STATE_JS_LOG_PREFIX) ? TEST_CONNECT_STATE_JS_LOG_PREFIX : '*** '
render('connection_state', :layout => 'client') && return
end
render :nothing => true, :status => 404
2013-02-28 18:44:33 +00:00
end
2012-11-12 20:12:32 +00:00
end