Class: Rack::TCTP
- Inherits:
-
Object
- Object
- Rack::TCTP
- Defined in:
- lib/rack/tctp.rb
Overview
This middleware enables Rack to make use of the Trusted Cloud Transfer Protocol (TCTP) for HTTP end-to-end body confidentiality and integrity.
Constant Summary collapse
- DEFAULT_TCTP_DISCOVERY_INFORMATION =
'/.*:/halecs'- TCTP_DISCOVERY_MEDIA_TYPE =
'text/prs.tctp-discovery'
Instance Attribute Summary collapse
-
#sessions ⇒ Object
readonly
The TCTP sessions.
Class Method Summary collapse
-
.new_slug ⇒ Object
Generate a new random slug (2^64 possibilities).
-
.slug_base ⇒ Object
The slug URI can contain any HTTP compatible characters.
Instance Method Summary collapse
-
#call(env) ⇒ Object
Middleware call.
-
#initialize(app) ⇒ TCTP
constructor
Initializes TCTP middleware.
Constructor Details
#initialize(app) ⇒ TCTP
Initializes TCTP middleware
27 28 29 30 |
# File 'lib/rack/tctp.rb', line 27 def initialize(app) @app = app @sessions = {} end |
Instance Attribute Details
#sessions ⇒ Object (readonly)
The TCTP sessions
24 25 26 |
# File 'lib/rack/tctp.rb', line 24 def sessions @sessions end |
Class Method Details
.new_slug ⇒ Object
Generate a new random slug (2^64 possibilities)
19 20 21 |
# File 'lib/rack/tctp.rb', line 19 def self.new_slug slug_base.convert(rand(2**64), 10) end |
.slug_base ⇒ Object
The slug URI can contain any HTTP compatible characters
14 15 16 |
# File 'lib/rack/tctp.rb', line 14 def self.slug_base Radix::Base.new(Radix::BASE::B62 + ['-', '_']) end |
Instance Method Details
#call(env) ⇒ Object
Middleware call. Supports all TCTP use cases:
-
TCTP discovery
-
HALEC creation
-
HALEC handshake
-
Decrypting TCTP secured entity-bodies
-
Encrypting entity-bodies using TCTP
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 |
# File 'lib/rack/tctp.rb', line 38 def call(env) begin req = Rack::Request.new(env) # Switch through TCTP use cases case when is_tctp_discovery?(req) # TCTP discovery # TODO Parameterize discovery information [200, {"Content-Type" => TCTP_DISCOVERY_MEDIA_TYPE, "Content-Length" => DEFAULT_TCTP_DISCOVERY_INFORMATION.length.to_s}, DEFAULT_TCTP_DISCOVERY_INFORMATION] when is_halec_creation?(req) # HALEC creation halec = ServerHALEC.new(url: '/halecs/' + TCTP::new_slug) # TODO Allow creation using predefined cookie session = TCTPSession.new # Send client_hello to server HALEC and read handshake_response client_hello = req.body.read halec.socket_there.write(client_hello) handshake_response = [halec.socket_there.recv(2048)] # Set location header and content-length header = {'Location' => halec.url, 'Content-Length' => handshake_response[0].length.to_s} # Set the TCTP session cookie header Rack::Utils.(header, "tctp_session_cookie", {:value => session.session_id, :path => '/', :expires => Time.now+24*60*60}) # Persist session and HALEC session.halecs[halec.url] = halec sessions[session.session_id] = session [201, header, handshake_response] when is_halec_handshake?(req) # Get persisted server HALEC halec = @sessions[req.['tctp_session_cookie']].halecs[req.path_info] # Write handshake message to server HALEC halec.socket_there.write(req.body.read) # Receive handshake response handshake_response = halec.socket_there.recv(2048) # Send back server HALEC response [200, {'Content-Length' => handshake_response.length.to_s}, [handshake_response]] else # Decrypt TCTP secured bodies if is_tctp_encrypted_body?(req) then decrypted_body = StringIO.new halec_url = req.body.readline.chomp # Gets the HALEC halec = @sessions[req.['tctp_session_cookie']].halecs[halec_url] halec.socket_there.write(req.body.read) decrypted_body.write(halec.ssl_socket.readpartial(2 ** 26 - 1)) req.body.string = decrypted_body.string end status, headers, body = @app.call(env) if is_tctp_response_requested?(req) # Gets the first free server HALEC for encryption # TODO Send error if cookie is missing halec = @sessions[req.['tctp_session_cookie']].free_halec # The length of the content body content_body_length = 0 # The first line first_line = halec.url + "\r\n" content_body_length += first_line.length # Encrypt the body. The first line of the response specifies the used HALEC encrypted_body = [] encrypted_body << first_line # Encrypt each body fragment body.each do |fragment| bodyio = StringIO.new(fragment) until bodyio.eof? do chunk = bodyio.read(16 * 1024) halec.ssl_socket.write(chunk) encrypted_chunk = halec.socket_there.readpartial(32 * 1024) encrypted_body << encrypted_chunk content_body_length += encrypted_chunk.length end end # Sets the content length and encoding headers['Content-Length'] = content_body_length.to_s headers['Content-Encoding'] = 'encrypted' [status, headers, encrypted_body] else [status, headers, body] end end rescue Exception => e puts e end end |