~ubuntu-branches/ubuntu/raring/ruby-mixlib-authentication/raring

« back to all changes in this revision

Viewing changes to lib/mixlib/authentication/http_authentication_request.rb

  • Committer: Package Import Robot
  • Author(s): Paul van Tilburg
  • Date: 2012-05-17 13:56:37 UTC
  • Revision ID: package-import@ubuntu.com-20120517135637-fo8zzak5h5z7e2ma
Tags: upstream-1.1.4
ImportĀ upstreamĀ versionĀ 1.1.4

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#
 
2
# Author:: Daniel DeLeo (<dan@opscode.com>)
 
3
# Copyright:: Copyright (c) 2010 Opscode, Inc.
 
4
# License:: Apache License, Version 2.0
 
5
#
 
6
# Licensed under the Apache License, Version 2.0 (the "License");
 
7
# you may not use this file except in compliance with the License.
 
8
# You may obtain a copy of the License at
 
9
 
10
#     http://www.apache.org/licenses/LICENSE-2.0
 
11
 
12
# Unless required by applicable law or agreed to in writing, software
 
13
# distributed under the License is distributed on an "AS IS" BASIS,
 
14
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 
15
# See the License for the specific language governing permissions and
 
16
# limitations under the License.
 
17
#
 
18
 
 
19
require 'mixlib/authentication'
 
20
 
 
21
module Mixlib
 
22
  module Authentication
 
23
    class HTTPAuthenticationRequest
 
24
 
 
25
      MANDATORY_HEADERS = [:x_ops_sign, :x_ops_userid, :x_ops_timestamp, :host, :x_ops_content_hash]
 
26
 
 
27
      attr_reader :request
 
28
 
 
29
      def initialize(request)
 
30
        @request = request
 
31
        @request_signature = nil
 
32
        validate_headers!
 
33
      end
 
34
 
 
35
      def headers
 
36
        @headers ||= @request.env.inject({ }) { |memo, kv| memo[$2.gsub(/\-/,"_").downcase.to_sym] = kv[1] if kv[0] =~ /^(HTTP_)(.*)/; memo }
 
37
      end
 
38
 
 
39
      def http_method
 
40
        @request.method.to_s
 
41
      end
 
42
 
 
43
      def path
 
44
        @request.path.to_s
 
45
      end
 
46
 
 
47
      def signing_description
 
48
        headers[:x_ops_sign].chomp
 
49
      end
 
50
 
 
51
      def user_id
 
52
        headers[:x_ops_userid].chomp
 
53
      end
 
54
 
 
55
      def timestamp
 
56
        headers[:x_ops_timestamp].chomp
 
57
      end
 
58
 
 
59
      def host
 
60
        headers[:host].chomp
 
61
      end
 
62
 
 
63
      def content_hash
 
64
        headers[:x_ops_content_hash].chomp
 
65
      end
 
66
 
 
67
      def request_signature
 
68
        unless @request_signature
 
69
          @request_signature = headers.find_all { |h| h[0].to_s =~ /^x_ops_authorization_/ }.sort { |x,y| x.to_s <=> y.to_s}.map { |i| i[1] }.join("\n")
 
70
          Mixlib::Authentication::Log.debug "Reconstituted (user-supplied) request signature: #{@request_signature}"
 
71
        end
 
72
        @request_signature
 
73
      end
 
74
 
 
75
 
 
76
      def validate_headers!
 
77
        missing_headers = MANDATORY_HEADERS - headers.keys
 
78
        unless missing_headers.empty?
 
79
          missing_headers.map! { |h| h.to_s.upcase }
 
80
          raise MissingAuthenticationHeader, "missing required authentication header(s) '#{missing_headers.join("', '")}'"
 
81
        end
 
82
      end
 
83
 
 
84
 
 
85
    end
 
86
  end
 
87
end