Module: CanvasLtiThirdPartyCookies::RelaunchOnLogin

Extended by:
ActiveSupport::Concern
Defined in:
app/controllers/concerns/canvas_lti_third_party_cookies/relaunch_on_login.rb

Instance Method Summary collapse

Instance Method Details

#calculate_localeObject



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'app/controllers/concerns/canvas_lti_third_party_cookies/relaunch_on_login.rb', line 68

def calculate_locale
  decoded_jwt = params[:lti_message_hint] ? JSON::JWT.decode(params[:lti_message_hint], :skip_verification) : {}
  if decoded_jwt['canvas_locale']
    # this is essentially the same logic as language_region_compatible_from below
    # example: 'en-AU', 'da-x-k12', 'ru', 'zh-Hant'
    full_locale = decoded_jwt['canvas_locale'].to_sym
    return full_locale if I18n.available_locales.include?(full_locale)

    # The exact locale is not available, let's trim it down if possible
    # example: 'en', 'da', 'ru', 'zh'
    trimmed_locale = decoded_jwt['canvas_locale'][0..1].to_sym
    return trimmed_locale if I18n.available_locales.include?(trimmed_locale)
  end

  http_accept_language.language_region_compatible_from(I18n.available_locales) || I18n.default_locale
end

#relaunch_on_login(redirect_url, redirect_data, window_type: :new_window, width: 800, height: 600) ⇒ Object

this should replace your previous login render call, at the end of your login action.

‘redirect_url` (required): the authorization redirect URL to continue the login flow

‘redirect_data` (required): all form data required for the authorization redirect, which the previous login render call should have included in a form tag.

‘window_type`: (optional) Set to `:new_window` to open the tool in a new tab or window, or to `:popup` to open in a popup window. Defaults to `:new_window`.

‘width`: (optional) The width the popup window should be, in px. User has the discretion to ignore this. Only valid with window_type: popup. Defaults to 800px.

‘height`: (optional) The height the popup window should be, in px. User has the discretion to ignore this. Only valid with window_type: popup. Defaults to 600px.

example: include CanvasLtiThirdPartyCookies::RelaunchOnLogin … def login

state, nonce = create_and_cache_state # handled elsewhere
redirect_url = 'http://canvas.instructure.com/api/lti/authorize_redirect'
redirect_data = {
  scope: 'openid',
  response_type: 'id_token',
  response_mode: 'form_post',
  prompt: 'none',
  redirect_uri: redirect_uri, # the launch url of the tool
  client_id: params.require(:client_id),
  login_hint: params.require(:login_hint),
  lti_message_hint: params.require(:lti_message_hint),
  state: state,
  nonce: nonce
}

(redirect_url, redirect_data)

end

Raises:

  • (ArgumentError)


44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'app/controllers/concerns/canvas_lti_third_party_cookies/relaunch_on_login.rb', line 44

def (redirect_url, redirect_data, window_type: :new_window, width: 800, height: 600)
  raise ArgumentError.new("window_type must be either :new_window or :popup") unless [:new_window, :popup].include? window_type

  I18n.locale = calculate_locale
  form_target = 'login_relaunch'
  render(
    'canvas_lti_third_party_cookies/relaunch_on_login',
    locals: {
      redirect_url: redirect_url,
      redirect_data: redirect_data,
      relaunch_url: request.url,
      relaunch_data: params.permit(:canvas_region, :client_id, :iss, :login_hint, :lti_message_hint, :target_link_uri),
      form_target: form_target,
      window_type: window_type,
      js_env: {
        form_target: form_target,
        window_type: window_type,
        width: width,
        height: height
      }
    }
  )
end