Class: OmniAuth::Strategies::YourMembershipToken

Inherits:
Object
  • Object
show all
Includes:
OmniAuth::Strategy
Defined in:
lib/omniauth/strategies/your_membership_token.rb

Constant Summary collapse

E_YM_SESSION_ID_BLANK =
<<~EOS
  Assertion failed: YourMembership ID blank during callback_phase. This ID
  was stored in the Rack session during the request_phase but has not
  survived to (is no longer present in) the callback_phase.
EOS
RACK_SESSION_KEY =
'omniauth_ym_session_id'.freeze

Instance Method Summary collapse

Instance Method Details

#callback_phaseObject

See discussion in ‘request_phase` re: the use of `env`.



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/omniauth/strategies/your_membership_token.rb', line 51

def callback_phase
  ym_session_id = env['rack.session'][RACK_SESSION_KEY]
  fail!(E_YM_SESSION_ID_BLANK) if ym_session_id.blank?
  ym_session  = YourMembership::Session.new(ym_session_id, 100)

  begin
    fail! 'Failed To Log In' unless ym_session.authenticated?
  rescue YourMembership::Error => e
    fail! e.error_description
  end

  @user_id = ym_session.user_id
  @access_token = ym_session.to_s

  super
end

#request_phaseObject



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/omniauth/strategies/your_membership_token.rb', line 21

def request_phase

  # We're doing this because the callback_url is built from the
  # options.name attribute which is built by down-casing the name of the
  # class. This returns an un-capitalized url which Rails will not
  # recognize as the same path as the devise controller. This is a forced
  # way to do this and probably has a more elegant solution.
  options.name = 'yourMembershipToken'
  # Build an Access Token
  session = YourMembership::Session.create
  token_hash = session.createToken(:RetUrl => callback_url)

  # Store the YourMembership session id somewhere it can be retrieved
  # during the callback_phase.
  #
  # In OmniAuth 1, we were able to do:
  #
  #     request.params['ym_session'] = session.to_s
  #
  # but this seems no longer possible in OmniAuth 2 (as described in
  # https://github.com/omniauth/omniauth/issues/975). So, the only thing I
  # can think of is to use `env['rack.session']`. If a better solution
  # is discovered, we can revisit this decision.
  env['rack.session'][RACK_SESSION_KEY] = session.to_s

  # Redirect to token url
  redirect token_hash['GoToUrl']
end