| 1 |
#!/usr/bin/env ruby |
|---|
| 2 |
|
|---|
| 3 |
require 'soap/rpc/standaloneServer' |
|---|
| 4 |
require 'soap/header/simplehandler' |
|---|
| 5 |
require 'authmgr' |
|---|
| 6 |
|
|---|
| 7 |
class AuthHeaderPortServer < SOAP::RPC::StandaloneServer |
|---|
| 8 |
class AuthHeaderService |
|---|
| 9 |
def initialize(authmgr) |
|---|
| 10 |
@authmgr = authmgr |
|---|
| 11 |
end |
|---|
| 12 |
|
|---|
| 13 |
def login(userid, passwd) |
|---|
| 14 |
if @authmgr.login(userid, passwd) |
|---|
| 15 |
@authmgr.create_session(userid) |
|---|
| 16 |
else |
|---|
| 17 |
raise RuntimeError.new("authentication failed") |
|---|
| 18 |
end |
|---|
| 19 |
end |
|---|
| 20 |
|
|---|
| 21 |
def deposit(amt) |
|---|
| 22 |
"deposit #{amt} OK" |
|---|
| 23 |
end |
|---|
| 24 |
|
|---|
| 25 |
def withdrawal(amt) |
|---|
| 26 |
"withdrawal #{amt} OK" |
|---|
| 27 |
end |
|---|
| 28 |
end |
|---|
| 29 |
|
|---|
| 30 |
Name = 'http://tempuri.org/authHeaderPort' |
|---|
| 31 |
def initialize(*arg) |
|---|
| 32 |
super |
|---|
| 33 |
authmgr = Authmgr.new |
|---|
| 34 |
add_rpc_servant(AuthHeaderService.new(authmgr), Name) |
|---|
| 35 |
ServerAuthHeaderHandler.init(authmgr) |
|---|
| 36 |
# header handler must be a per request handler. |
|---|
| 37 |
add_request_headerhandler(ServerAuthHeaderHandler) |
|---|
| 38 |
end |
|---|
| 39 |
|
|---|
| 40 |
class ServerAuthHeaderHandler < SOAP::Header::SimpleHandler |
|---|
| 41 |
MyHeaderName = XSD::QName.new("http://tempuri.org/authHeader", "auth") |
|---|
| 42 |
|
|---|
| 43 |
def self.init(authmgr) |
|---|
| 44 |
@authmgr = authmgr |
|---|
| 45 |
end |
|---|
| 46 |
|
|---|
| 47 |
def self.create |
|---|
| 48 |
new(@authmgr) |
|---|
| 49 |
end |
|---|
| 50 |
|
|---|
| 51 |
def initialize(authmgr) |
|---|
| 52 |
super(MyHeaderName) |
|---|
| 53 |
@authmgr = authmgr |
|---|
| 54 |
@sessionid = nil |
|---|
| 55 |
end |
|---|
| 56 |
|
|---|
| 57 |
def on_simple_outbound |
|---|
| 58 |
if @sessionid |
|---|
| 59 |
{ "sessionid" => @sessionid } |
|---|
| 60 |
end |
|---|
| 61 |
end |
|---|
| 62 |
|
|---|
| 63 |
def on_simple_inbound(my_header, mu) |
|---|
| 64 |
auth = false |
|---|
| 65 |
if sessionid = my_header["sessionid"] |
|---|
| 66 |
if userid = @authmgr.auth(sessionid) |
|---|
| 67 |
@authmgr.destroy_session(sessionid) |
|---|
| 68 |
@sessionid = @authmgr.create_session(userid) |
|---|
| 69 |
auth = true |
|---|
| 70 |
end |
|---|
| 71 |
end |
|---|
| 72 |
raise RuntimeError.new("authentication failed") unless auth |
|---|
| 73 |
end |
|---|
| 74 |
end |
|---|
| 75 |
end |
|---|
| 76 |
|
|---|
| 77 |
if $0 == __FILE__ |
|---|
| 78 |
svr = AuthHeaderPortServer.new('AuthHeaderPortServer', nil, '0.0.0.0', 7000) |
|---|
| 79 |
trap(:INT) do |
|---|
| 80 |
svr.shutdown |
|---|
| 81 |
end |
|---|
| 82 |
status = svr.start |
|---|
| 83 |
end |
|---|