-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathzhdk-aai-shibboleth-auth-system.rb
203 lines (155 loc) · 5.09 KB
/
zhdk-aai-shibboleth-auth-system.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
require 'active_support/all'
require 'cgi'
require 'haml'
require 'jwt'
require 'pathname'
require 'rest-client'
require 'sinatra/base'
require 'yaml'
$logger = Logger.new(STDOUT)
$logger.level = Logger::WARN
################################################################################
### options and config #########################################################
################################################################################
$config = YAML.load_file(
File.expand_path('config.yml', File.dirname(__FILE__))
).with_indifferent_access.tap do |c|
c[:my_private_key] = OpenSSL::PKey.read(c[:my_private_key])
c[:my_public_key] = OpenSSL::PKey.read(c[:my_public_key])
c[:leihs_public_key] = OpenSSL::PKey.read(c[:leihs_public_key])
end
################################################################################
### the web app ################################################################
################################################################################
class AuthenticatorApp < Sinatra::Base
# some helpers ###############################################################
def html_layout
Haml::Engine.new <<-HAML.strip_heredoc
%html
%head
%title Leihs AAI-Shibboleth Authentication-System
%body
%h1 Leihs AAI-Shibboleth Authentication-System
= yield
HAML
end
def env_table
Haml::Engine.new(
<<-HAML.strip_heredoc
%table
- @env.to_hash.each do |k, v|
%tr
%td
%pre= k
%td
%pre= v
HAML
)
end
### error handling and error messages ########################################
def expired_message sign_in_request_token
sign_in_request = JWT.decode sign_in_request_token,
$config[:leihs_public_key], false, { algorithm: 'ES256' }
<<-HTML.strip_heredoc
<html>
<head></head>
<body>
<h1>Error: Token Expired </h1>
<p> Please <a href="#{sign_in_request[0]['server_base_url']}"> try again. </a></p>
</body>
</html>
HTML
end
def generic_error_message e
$logger.error "#{e} #{e.backtrace}"
<<-HTML.strip_heredoc
<html>
<head></head>
<body>
<h1> Unspecified Error in leihs-AAI Authentication Service </h1>
<p> Please try again. </p>
<p> Contact your leihs administrator if this problem occurs again. </p>
</body>
</html>
HTML
end
### routes ####################################################################
get '/status' do
'OK'
end
get '/' do
html_layout.render do
Haml::Engine.new(<<-HAML.strip_heredoc
%p
See
%a{href: '/Shibboleth.sso/Session'}
%code /Shibboleth.sso/Session
HAML
).render
end
end
get '/debug' do
if ENV['RACK_ENV'] == 'development'
require 'pry'
require 'pry-remote'
binding.pry_remote
end
status 404
'Error 404'
end
get '/sign-in' do
begin
sign_in_request_token = params[:token]
sign_in_request = JWT.decode sign_in_request_token,
$config[:leihs_public_key], true, { algorithm: 'ES256' }
email = sign_in_request.first["email"].presence.try(:downcase)
throw ArgumentError, "email must be present" unless email
token = JWT.encode({
sign_in_request_token: sign_in_request_token
# and more if we ever need it
}, $config[:my_private_key], 'ES256')
url = $config[:my_external_base_url] + "/Shibboleth.sso/Login?target=" \
+ CGI::escape("#{$config[:my_external_base_url]}/callback?token=#{token}")
redirect url
rescue JWT::ExpiredSignature => e
expired_message sign_in_request_token
rescue StandardError => e
generic_error_message e
end
end
### callback ##################################################################
get '/callback' do
begin
token_data = JWT.decode(params[:token],
$config[:my_public_key], true, { algorithm: 'ES256'}) \
.first.with_indifferent_access
sign_in_request_token = token_data[:sign_in_request_token]
sign_in_request = JWT.decode sign_in_request_token,
$config[:leihs_public_key], true, { algorithm: 'ES256' }
raise 'Env var `mail` must be present in callback' unless @env['mail'].presence
token = JWT.encode({
sign_in_request_token: sign_in_request_token,
email: @env['mail'].downcase,
success: true}, $config[:my_private_key], 'ES256')
url = sign_in_request.first["server_base_url"] \
+ sign_in_request.first['path'] + "?token=#{token}"
redirect url
rescue JWT::ExpiredSignature => e
expired_message sign_in_request_token
rescue StandardError => e
generic_error_message e
end
end
get '/*' do
pass if /\/Shibboleth/ =~ request.path_info
status 404
html_layout.render do
Haml::Engine.new(
<<-HAML.strip_heredoc
%h2{ style: "color: red;"} Error 404
%p The requested resource does not exist.
HAML
).render
end
end
end