Repository URL to install this package:
|
Version:
2.2.0 ▾
|
# frozen_string_literal: true
require 'sinatra/auth_helpers'
module Sinatra
# Helpers for production - using AWS Cognito
module CognitoAuth
# Sinatra instance methods (callable from routes, etc)
module Helpers
include Sinatra::AuthHelpers
def authenticate!
session[:redirect_after_login] = request.fullpath
redirect '/auth/cognito-idp'
end
def _omniauth_username
auth = request.env['omniauth.auth']
principal = auth.extra.raw_info.preferred_username || ''
elements = principal.split('@', 2)
elements.last == 'york.ac.uk' ? elements.first : nil
end
end
def self.registered(app)
app.helpers Helpers
app.get '/auth/:name/callback' do
session[:username] = _omniauth_username
redirect session[:redirect_after_login] || '/'
end
app.get '/logout' do
session.destroy
redirect '/'
end
end
end
end