class Warden::GitHub::Strategy

Constants

SESSION_KEY

Public Instance Methods

authenticate!() click to toggle source

The first time this is called, the flow gets set up, stored in the session and the user gets redirected to GitHub to perform the login.

When this is called a second time, the flow gets evaluated, the code gets exchanged for a token, and the user gets loaded and passed to warden.

If anything goes wrong, the flow is aborted and reset, and warden gets notified about the failure.

Once the user gets set, warden invokes the after_authentication callback that handles the redirect to the originally requested url and cleans up the flow. Note that this is done in a hook because setting a user (through success!) and redirecting (through redirect!) inside the authenticate! method are mutual exclusive.

# File lib/warden/github/strategy.rb, line 21
def authenticate!
  if in_flow?
    continue_flow!
  else
    begin_flow!
  end
end
finalize_flow!() click to toggle source

This is called by the after_authentication hook which is invoked after invoking success!.

# File lib/warden/github/strategy.rb, line 37
def finalize_flow!
  redirect!(custom_session['return_to'])
  teardown_flow
  throw(:warden)
end
in_flow?() click to toggle source
# File lib/warden/github/strategy.rb, line 29
def in_flow?
  !custom_session.empty? &&
    params['state'] &&
    (params['code'] || params['error'])
end

Private Instance Methods

abort_flow!(message) click to toggle source
# File lib/warden/github/strategy.rb, line 57
def abort_flow!(message)
  teardown_flow
  fail!(message)
  throw(:warden)
end
begin_flow!() click to toggle source
# File lib/warden/github/strategy.rb, line 45
def begin_flow!
  custom_session['state'] = state
  custom_session['return_to'] = request.url
  redirect!(oauth.authorize_uri.to_s)
  throw(:warden)
end
config() click to toggle source
# File lib/warden/github/strategy.rb, line 98
def config
  @config ||= ::Warden::GitHub::Config.new(env, scope)
end
continue_flow!() click to toggle source
# File lib/warden/github/strategy.rb, line 52
def continue_flow!
  validate_flow!
  success!(load_user)
end
custom_session() click to toggle source
# File lib/warden/github/strategy.rb, line 79
def custom_session
  session[SESSION_KEY] ||= {}
end
load_user() click to toggle source
# File lib/warden/github/strategy.rb, line 83
def load_user
  User.load(oauth.access_token, custom_session['browser_session_id'])
rescue OAuth::BadVerificationCode => e
  abort_flow!(e.message)
end
oauth() click to toggle source
# File lib/warden/github/strategy.rb, line 93
def oauth
  @oauth ||= OAuth.new(
    config.to_hash.merge(code: params['code'], state: state))
end
state() click to toggle source
# File lib/warden/github/strategy.rb, line 89
def state
  @state ||= custom_session['state'] || SecureRandom.hex(20)
end
teardown_flow() click to toggle source
# File lib/warden/github/strategy.rb, line 63
def teardown_flow
  session.delete(SESSION_KEY)
end
validate_flow!() click to toggle source
# File lib/warden/github/strategy.rb, line 67
def validate_flow!
  if params['state'] != state
    abort_flow!('State mismatch')
  elsif (error = params['error']) && !error.empty?
    abort_flow!(error.gsub(/_/, ' '))
  end

  if params['browser_session_id']
    custom_session['browser_session_id'] = params['browser_session_id']
  end
end