Skip to content

Commit

Permalink
handle JWT error
Browse files Browse the repository at this point in the history
  • Loading branch information
thenickcox committed Jan 26, 2016
1 parent c5efc28 commit 978a66b
Show file tree
Hide file tree
Showing 8 changed files with 51 additions and 1 deletion.
6 changes: 6 additions & 0 deletions app/controllers/errors_controller.rb
Expand Up @@ -17,4 +17,10 @@ def internal_server_error
def incorrect_passcode
render status: 401
end

def login_error
# a status in the 500 range will automatically bypass this and
# render internal_server_error
render status: 200
end
end
9 changes: 9 additions & 0 deletions app/controllers/omniauth_callbacks_controller.rb
Expand Up @@ -7,11 +7,20 @@ class OmniauthCallbacksController < Devise::OmniauthCallbacksController
def mediawiki
@user = UserImporter.from_omniauth(request.env['omniauth.auth'])

return handle_jwt_error if request.env['JWT_ERROR']

if @user.persisted?
remember_me @user
sign_in_and_redirect @user
else
redirect_to root_url
end
end

protected

def handle_jwt_error
Raven.capture_message "\nJWT Decoding failed: #{request.env['JWT_DATA'].body}\n"
return redirect_to errors_login_error_path
end
end
12 changes: 12 additions & 0 deletions app/views/errors/login_error.html.erb
@@ -0,0 +1,12 @@
<header class="main-page">
<h1><%= Figaro.env.dashboard_title %></h1>
</header>

<section id="courses">
<div class="container narrow">
<div class="section-header">
<h3><%= t("login_error.header") %></h3>
<p><%= t("login_error.explanation") %></p>
</div>
</div>
</section>
5 changes: 4 additions & 1 deletion config/initializers/devise.rb
@@ -1,3 +1,5 @@
require_relative '../../lib/custom_strategy'

# Use this hook to configure devise mailer, warden hooks and so forth.
# Many of these configuration options can be set straight in your model.
Devise.setup do |config|
Expand Down Expand Up @@ -240,6 +242,7 @@
config.omniauth :mediawiki,
Figaro.env.wikipedia_token,
Figaro.env.wikipedia_secret,
strategy_class: CustomStrategy,
client_options: {
site: "https://#{Figaro.env.wiki_language}.wikipedia.org"
}
Expand All @@ -249,7 +252,7 @@
Figaro.env.wikipedia_token,
Figaro.env.wikipedia_secret,
name: 'mediawiki_signup',
strategy_class: OmniAuth::Strategies::Mediawiki,
strategy_class: CustomStrategy,
client_options: {
site: "https://#{Figaro.env.wiki_language}.wikipedia.org",
signup: true
Expand Down
5 changes: 5 additions & 0 deletions config/locales/en.yml
Expand Up @@ -326,6 +326,11 @@ en:
Something went wrong. Please contact the maintainers of this dashboard
to let them know about the problem.
login_error:
header: "Login Error"
explanation: >
It looks like there was an problem with the sign in process. (Sorry about that! It happens occasionally.) Please try logging in or enrolling again.
# This is a maintenance script message.
timeout: "A request to the %{api} API has timed out and will retried %{tries} times."

Expand Down
2 changes: 2 additions & 0 deletions config/routes.rb
Expand Up @@ -3,6 +3,7 @@
Rails.application.routes.draw do
get 'errors/file_not_found'
get 'errors/unprocessable'
get 'errors/login_error'
get 'errors/internal_server_error'
get 'errors/incorrect_passcode'

Expand Down Expand Up @@ -160,5 +161,6 @@
# Errors
match '/404', to: 'errors#file_not_found', via: :all
match '/422', to: 'errors#unprocessable', via: :all
match '/599', to: 'errors#login_error', via: :all
match '/500', to: 'errors#internal_server_error', via: :all
end
12 changes: 12 additions & 0 deletions lib/custom_strategy.rb
@@ -0,0 +1,12 @@
class LoginError < StandardError; end

class CustomStrategy < OmniAuth::Strategies::Mediawiki
def parse_info(jwt_data)
begin
super
rescue JWT::DecodeError
request.env['JWT_ERROR'] = true
request.env['JWT_DATA'] = jwt_data
end
end
end
1 change: 1 addition & 0 deletions lib/wiki.rb
Expand Up @@ -21,6 +21,7 @@ def self.get_user_id(username, language=nil)
user_query = { list: 'users',
ususers: username }
user_data = wikipedia('query', user_query, language: language)
return unless user_data.data['users'].any?
user_id = user_data.data['users'][0]['userid']
user_id
end
Expand Down

0 comments on commit 978a66b

Please sign in to comment.