#!/bin/env ruby
# encoding : utf-8
require 'webrick/cgi'
require 'net/http'
Net::HTTP.version_1_2
class LoginProxy < WEBrick::CGI
# GET request
def do_GET(request, response)
# Shibbolethの認証状態の確認
# このアクションが実行された時点で、Shibbolethのセッションは確立されています。
# 必要に応じてSPの返却したパラメータを参照して権限の確認等を行います。
# SPの返却したパラメータは、HTTP環境変数に追加されています。
unless ENV.key?('eduPersonTargetedIDpersistent-id')
# eduPersonTargetedID が存在しない場合は認証エラー
response.staus = 403 # 403 Forbidden
return
end
# 既存アプリケーションのセッション情報を生成する
# この部分の実装は既存アプリケーションの構成に依存します。
# ここでは、'http://application.example.net/login' に諸元をPOSTすると、セッションが生成され
# クライアントに返却すべきCookieが得られるものとします。
http = Net::HTTP.start('application.example.net', 80)
app_response = http.post('/login', 'user_id=shibboleth_user&password=shibboleth_password')
app_cookies = app_response.get_fields('Set-Cookie').inject({}) do |cookies, cookie_string|
key, value = cookie_string[/[^;]*/].split('=')
cookies[key] = value
end
# 取得されたアプリケーションのセッションCookieをレスポンスに設定する
session_cookie = WEBrick::Cookie.new('session_cookie', app_cookies['session_cookie'])
session_cookie.path = '/'
session_cookie.domain = 'application.example.net'
response.cookies << session_cookie
# ログイン後のトップページへリダイレクトする
redirect_url = 'http://application.example.net'
response.set_redirect(WEBrick::HTTPStatus::Found, redirect_url)
end
alias do_POST do_GET
end
LoginProxy.new.start
|