2 require 'openid/extension'
3 require 'openid/extensions/sreg'
4 require 'openid/store/filesystem'
15 attr_accessor :request,:openid_request,
16 :response, :openid_response,
19 def initialize(config, server_options)
20 @server_options = server_options
21 @sreg_fields = config['sreg']
25 on_openid_request(env) do
26 if !is_checkid_request?
27 @openid_response = @server.handle_request(@openid_request)
29 elsif is_checkid_immediate?
30 process_immediate_checkid_request
32 process_checkid_request
39 def on_openid_request(env)
41 if @openid_request.nil?
42 [200, {'Content-Type' => 'text/html'},
43 ["<html><body><h1>ROTS => This is an OpenID endpoint</h1></body></html>"] ]
49 def create_wrappers(env)
50 @request = Rack::Request.new(env)
51 @server = OpenID::Server::Server.new(storage, op_endpoint)
52 @openid_request = @server.decode_request(@request.params)
53 @openid_sreg_request = OpenID::SReg::Request.from_openid_request(@openid_request) unless @openid_request.nil?
56 def is_checkid_request?
57 @openid_request.is_a?(OpenID::Server::CheckIDRequest)
60 def is_checkid_immediate?
61 @openid_request && @openid_request.immediate
64 def process_immediate_checkid_request
65 # TODO: We should enable the user to configure
66 # if she wants immediate request support or not
67 url = OpenID::Util.append_args(@openid_request.return_to,
68 @request.params.merge('openid.mode' => 'setup_needed'))
72 def process_checkid_request
73 if checkid_request_is_valid?
74 return_successful_openid_response
76 return_cancel_openid_response
80 def checkid_request_is_valid?
81 @request.params['openid.success'] == 'true'
84 def return_successful_openid_response
85 @openid_response = @openid_request.answer(true)
86 process_sreg_extension
87 # TODO: Add support for SREG extension
88 @server.signatory.sign(@openid_response) if @openid_response.needs_signing
92 def process_sreg_extension
93 return if @openid_sreg_request.nil?
94 response = OpenID::SReg::Response.extract_response(@openid_sreg_request, @sreg_fields)
95 @openid_response.add_extension(response)
98 def return_cancel_openid_response
99 redirect(@openid_request.cancel_url)
103 web_response = @server.encode_response(@openid_response)
104 case web_response.code
105 when OpenID::Server::HTTP_OK
106 success(web_response.body)
107 when OpenID::Server::HTTP_REDIRECT
108 redirect(web_response.headers['location'])
115 [ 303, {'Content-Length'=>'0', 'Content-Type'=>'text/plain',
121 [ 400, {'Content-Type'=>'text/plain', 'Content-Length'=>'0'},
126 # create the folder if it doesn't exist
127 FileUtils.mkdir_p(@server_options[:storage]) unless File.exist?(@server_options[:storage])
128 OpenID::Store::Filesystem.new(@server_options[:storage])
132 Rack::Response.new(text).finish
136 if @request.url =~ /(.*\?openid.success=true)/
138 elsif @request.url =~ /([^?]*)/