Class: OAuth2::Headers::Authorization

Inherits:
Object
  • Object
show all
Defined in:
lib/oauth2/headers/authorization.rb

Constant Summary collapse

Attributes =

These attributes are expected to be contained in the header

[
  :token, :nonce, :timestamp, :algorithm, :signature
]

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attributes = {}) ⇒ Authorization

Returns a new instance of Authorization.



10
11
12
13
14
15
16
# File 'lib/oauth2/headers/authorization.rb', line 10

def initialize(attributes = {})
  attributes.each_pair do |key, value|
    if Attributes.include?(key.to_sym)
      instance_variable_set("@#{key}", value)
    end
  end
end

Class Method Details

.parse(string) ⇒ Object

This method does what it is named after. Give it a String and it returns a Hash. The header specification can be found on: tools.ietf.org/html/draft-hammer-oauth2-00#section-5.1 TODO: Verify that token is the first attribute.



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
# File 'lib/oauth2/headers/authorization.rb', line 69

def parse(string)
  header = new

  string.strip!

  type, tuples = string[0..4], string[5..-1].split("\n")

  unless type == "Token"
    header.errors << :format_invalid
    return header
  end
  
  tuples.map! { |tuple| tuple.strip! }
  
  tuples.each do |tuple|
    unless tuple =~ /\s*(.+)="(.+)"/
      header.errors << :format_invalid 
    else
      key, value = $1.to_sym, $2

      unless Attributes.include?(key)
        header.errors << "unknown_attribute_#{key}".to_sym
      else
        header.send("#{key}=".to_sym, value)
      end
    end
  end

  header
end

Instance Method Details

#attributesObject



47
48
49
50
51
52
53
# File 'lib/oauth2/headers/authorization.rb', line 47

def attributes
  hash = ActiveSupport::OrderedHash.new
  Attributes.each do |attribute|
    hash[attribute] = instance_variable_get("@#{attribute}")
  end
  hash
end

#errorsObject



43
44
45
# File 'lib/oauth2/headers/authorization.rb', line 43

def errors
  @errors ||= []
end

#to_sObject



55
56
57
58
59
60
61
# File 'lib/oauth2/headers/authorization.rb', line 55

def to_s
  attrs = attributes.collect do |key, value|
    %{#{key}="#{value}"} if value
  end.compact

  "Token " + attrs.join(",\n     ")
end

#validateObject



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/oauth2/headers/authorization.rb', line 27

def validate
  case request_type
  when :bearer
    @errors << :bearer_request_requires_token unless token
  when :cryptographic
    %w{nonce timestamp algorithm signature}.each do |attribute|
      unless instance_variable_get("@#{attribute}")
        error = "cryptographic_request_requires_#{attribute}".to_sym
        @errors << error
      end
    end
  end

  @errors.blank?     
end