2012-09-03 22:03:16 +00:00
|
|
|
# this is not a jam session - this is an 'auth session'
|
2012-08-31 03:01:52 +00:00
|
|
|
class SessionsController < ApplicationController
|
|
|
|
|
|
2014-02-07 14:07:08 +00:00
|
|
|
layout "web"
|
|
|
|
|
|
2014-06-30 20:44:28 +00:00
|
|
|
def signin
|
2013-06-05 02:43:41 +00:00
|
|
|
@login_error = false
|
2014-03-21 03:23:33 +00:00
|
|
|
@sso = params[:sso]
|
|
|
|
|
@send_back_to = request.headers['REFERER']
|
|
|
|
|
params[:send_back_to] = @send_back_to
|
|
|
|
|
|
|
|
|
|
if current_user
|
|
|
|
|
# send them on their way
|
|
|
|
|
complete_sign_in(current_user)
|
|
|
|
|
return
|
|
|
|
|
end
|
|
|
|
|
|
2013-06-02 19:27:46 +00:00
|
|
|
render :layout => "landing"
|
2012-08-31 03:01:52 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def create
|
2012-11-14 05:57:10 +00:00
|
|
|
user = User.authenticate(params[:session][:email], params[:session][:password])
|
|
|
|
|
|
|
|
|
|
if user.nil?
|
2013-06-05 02:43:41 +00:00
|
|
|
@login_error = true
|
2014-03-21 03:23:33 +00:00
|
|
|
@sso = params[:sso]
|
|
|
|
|
@send_back_to = params[:send_back_to]
|
2014-06-30 20:44:28 +00:00
|
|
|
render 'signin', :layout => "landing"
|
2012-11-14 05:57:10 +00:00
|
|
|
else
|
2013-09-30 02:37:22 +00:00
|
|
|
|
|
|
|
|
if jkclient_agent?
|
|
|
|
|
user.update_progression_field(:first_ran_client_at)
|
|
|
|
|
end
|
|
|
|
|
|
2013-08-09 16:07:04 +00:00
|
|
|
@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
|
2012-08-31 03:01:52 +00:00
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
2014-02-07 14:07:08 +00:00
|
|
|
|
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.
|
2012-12-02 06:46:30 +00:00
|
|
|
#
|
|
|
|
|
# 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
|
|
|
|
|
|
2014-02-07 14:07:08 +00:00
|
|
|
|
|
|
|
|
# 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]
|
|
|
|
|
|
2014-02-07 14:07:08 +00:00
|
|
|
if provider == 'twitter'
|
2014-02-07 17:18:57 +00:00
|
|
|
@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
|
|
|
|
|
|
2014-02-07 14:07:08 +00:00
|
|
|
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!
|
|
|
|
|
|
2015-05-15 17:34:35 +00:00
|
|
|
# see if we can find a SignupHint for a JamTrack; if so, bounce them back to the redeemComplete page to let them know we bought it
|
|
|
|
|
|
|
|
|
|
if anonymous_user
|
|
|
|
|
signup_hint = anonymous_user.signup_hint
|
|
|
|
|
if signup_hint && signup_hint.redirect_location
|
|
|
|
|
options = {
|
|
|
|
|
first_name: first_name,
|
|
|
|
|
last_name: last_name,
|
|
|
|
|
email: email,
|
|
|
|
|
terms_of_service: true,
|
|
|
|
|
location: {:country => nil, :state => nil, :city => nil},
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
options = User.musician_defaults(request.remote_ip, ApplicationHelper.base_uri(request) + "/confirm", any_user, options)
|
|
|
|
|
|
|
|
|
|
user = UserManager.new.signup(options)
|
|
|
|
|
|
|
|
|
|
if user.errors.any?
|
|
|
|
|
redirect_to signup_hint.redirect_location
|
|
|
|
|
return
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
sign_in(user)
|
|
|
|
|
new_user(user, signup_hint)
|
|
|
|
|
redirect_to signup_hint.redirect_location
|
|
|
|
|
return
|
|
|
|
|
end
|
|
|
|
|
else
|
|
|
|
|
redirect_to "#{signup_path}?facebook_signup=#{fb_signup.lookup_id}"
|
|
|
|
|
return
|
|
|
|
|
end
|
2014-02-03 21:19:14 +00:00
|
|
|
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.
|
2014-10-15 18:41:49 +00:00
|
|
|
user_auth_hash = {
|
|
|
|
|
:provider => auth_hash[:provider],
|
|
|
|
|
:uid => auth_hash[:uid],
|
|
|
|
|
:token => auth_hash[:credentials][:token],
|
|
|
|
|
:token_expiration => Time.at(auth_hash[:credentials][:expires_at]),
|
|
|
|
|
:secret => auth_hash[:credentials][:secret]
|
|
|
|
|
}
|
|
|
|
|
|
2013-09-10 00:56:55 +00:00
|
|
|
#if authorization.nil?
|
2014-10-15 18:41:49 +00:00
|
|
|
authorization = current_user.user_authorizations.build(user_auth_hash)
|
2013-09-07 07:59:55 +00:00
|
|
|
authorization.save
|
2013-09-10 00:56:55 +00:00
|
|
|
#end
|
2013-09-07 07:59:55 +00:00
|
|
|
|
2013-09-08 03:17:11 +00:00
|
|
|
render 'oauth_complete', :layout => "landing"
|
2013-09-07 07:59:55 +00:00
|
|
|
end
|
|
|
|
|
|
2014-11-06 19:16:58 +00:00
|
|
|
def has_google_auth
|
|
|
|
|
render :json => {has_google_auth: (!!current_user && !!UserAuthorization.google_auth(current_user).first)}
|
|
|
|
|
end
|
|
|
|
|
|
2014-03-21 03:23:33 +00:00
|
|
|
def redirect_after_signin(default)
|
|
|
|
|
redirect_to(params['redirect-to'].blank? ? default : params['redirect-to'])
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def redirect_to_forums_after_signin
|
|
|
|
|
redirect_to("#{Rails.application.config.vanilla_login_url}?client_id=#{Rails.application.config.vanilla_client_id}&Target=#{ERB::Util.url_encode(params[:send_back_to].blank? ? '/' : params[:send_back_to])}")
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def redirect_to_support_after_signin(user)
|
|
|
|
|
# generate multipass token and sign it
|
|
|
|
|
multipass = DeskMultipass.new(user)
|
|
|
|
|
callback_url = Rails.application.config.multipass_callback_url
|
|
|
|
|
redirect_to "#{callback_url}?multipass=#{multipass.token}&signature=#{multipass.signature}"
|
|
|
|
|
end
|
|
|
|
|
|
2012-11-12 20:12:32 +00:00
|
|
|
|
2012-08-31 03:01:52 +00:00
|
|
|
def destroy
|
2013-09-02 03:09:49 +00:00
|
|
|
# earlier, code here would delete the connection using client_id from cookies
|
2013-09-01 19:23:09 +00:00
|
|
|
# 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.
|
2013-09-02 03:09:49 +00:00
|
|
|
# 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)
|
2012-08-31 03:01:52 +00:00
|
|
|
sign_out
|
2012-11-17 21:03:06 +00:00
|
|
|
redirect_to client_url
|
2012-08-31 03:01:52 +00:00
|
|
|
end
|
2012-11-12 20:12:32 +00:00
|
|
|
|
|
|
|
|
def failure
|
2014-02-07 14:07:08 +00:00
|
|
|
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
|
2013-03-01 01:51:15 +00:00
|
|
|
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
|