Skip to content
This repository was archived by the owner on Jul 6, 2020. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 11 additions & 11 deletions oauth2-core/lib/oauth2/attributes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,26 @@
require 'active_support/core_ext/class/inheritable_attributes'

module OAuth2

module Attributes

extend ActiveSupport::Concern

included do
class_inheritable_array :attribute_names
self.attribute_names = Array.new
end

def attributes
@attributes ||= Hash.new
end

module ClassMethods

# Defines a callback for a handle.
def attribute(handle)
attribute_names << handle.to_sym

class_eval(<<-EOS, __FILE__, __LINE__ + 1)
def #{handle}(&block)
@attributes ||= {}
Expand All @@ -38,22 +38,22 @@ def #{handle}(&block)
raise "No attribute or callback for '#{handle}' defined"
end
end

def #{handle.to_s.gsub('?', '')}=(value)
@attributes ||= {}
@attributes[:#{handle}] = value
end
EOS
end

def attributes(*handles)
handles.each do |handle|
attribute(handle)
end
end

end

end

end
2 changes: 1 addition & 1 deletion oauth2-core/lib/oauth2/core.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
module OAuth2

autoload :Headers, 'oauth2/headers'
autoload :Signature, 'oauth2/signature'

Expand Down
8 changes: 4 additions & 4 deletions oauth2-core/lib/oauth2/headers.rb
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
module OAuth2

module Headers

autoload :Authorization, 'oauth2/headers/authorization'
autoload :Authenticate, 'oauth2/headers/authenticate'

end

end
20 changes: 10 additions & 10 deletions oauth2-core/lib/oauth2/headers/authenticate.rb
Original file line number Diff line number Diff line change
@@ -1,32 +1,32 @@
require 'oauth2/attributes'

module OAuth2

module Headers

class Authenticate

Attributes = [
:realm, :algorithms, :auth_uri, :token_uri, :error
]

Attributes.each do |attribute|
attr_accessor attribute
end

def valid?
raise "Realm not set" unless realm

end

def to_hash
Attributes.inject(Hash.new) do |hash, attribute|
value = send(attribute)
hash[attribute] = value unless value.nil?
hash
end
end

def to_s
to_hash.collect do |key, value|
name = key.to_s.gsub('_', '-')
Expand All @@ -35,7 +35,7 @@ def to_s
end

end

end

end
18 changes: 9 additions & 9 deletions oauth2-core/lib/oauth2/headers/authorization.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ def initialize(attributes = {})
Attributes.each do |attribute|
attr_accessor(attribute)
end

def validate
case request_type
when :bearer
Expand All @@ -37,21 +37,21 @@ def validate
end
end

@errors.blank?
@errors.blank?
end

def errors
@errors ||= []
end

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

def to_s
attrs = attributes.collect do |key, value|
%{#{key}="#{value}"} if value
Expand All @@ -77,16 +77,16 @@ def parse(string)
header.errors << :format_invalid
return header
end

tuples.map! { |tuple| tuple.strip! }

tuples.each do |tuple|

# Parameters sent without a value MUST be treated as if they were
# omitted from the request.
# http://tools.ietf.org/html/draft-ietf-oauth-v2-09#page-18
unless tuple =~ /\s*(.+)="(.*)"/
header.errors << :format_invalid
header.errors << :format_invalid
else
key, value = $1.to_sym, $2
next if value.empty?
Expand Down
8 changes: 4 additions & 4 deletions oauth2-core/oauth2-core.gemspec
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# -*- encoding: utf-8 -*-
lib = File.expand_path('../lib/', __FILE__)
$:.unshift lib unless $:.include?(lib)

Gem::Specification.new do |s|
s.name = "oauth2-core"
s.version = '0.1.4'
Expand All @@ -11,13 +11,13 @@ Gem::Specification.new do |s|
s.homepage = "http://github.com/aflatter/oauth2-ruby"
s.summary = ""
s.description = ""

s.required_rubygems_version = ">= 1.3.6"

s.add_dependency(%q<activesupport>, [">= 0.0.0"])

s.add_development_dependency "rspec"

s.files = Dir.glob("{bin,lib}/**/*") + %w(LICENSE README.md)
s.require_path = 'lib'
end
10 changes: 5 additions & 5 deletions oauth2-core/spec/attributes_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@
include OAuth2::Attributes
attributes :foo
end

klass.new
end

it "should allow to set an attribute to nil" do
subject.foo = nil
subject.foo.should be_nil
Expand All @@ -22,15 +22,15 @@
subject.foo { foo }
subject.foo.should == foo
end

it "should return user defined value if defined" do
foo = stub("some value")
subject.foo = foo
subject.foo.should == foo
end

it "should raise exception if no callback or value is defined" do
lambda { subject.foo }.should raise_error(RuntimeError)
end

end
2 changes: 1 addition & 1 deletion oauth2-core/spec/headers/authenticate_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@
require 'oauth2/headers/authenticate'

describe OAuth2::Headers::Authenticate do

end
4 changes: 2 additions & 2 deletions oauth2-core/spec/headers/authorization_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,14 @@
Token token="vF9dft4qmT",
nonce=""
EOS

example_without_empty_value = <<-EOS
Token token="vF9dft4qmT"
EOS

OAuth2::Headers::Authorization.parse(example_with_empty_value).to_s.should == OAuth2::Headers::Authorization.parse(example_without_empty_value).to_s
end

it "ignores parameters without value using the new method" do
OAuth2::Headers::Authorization.new(:token => "vF9dft4qmT", :signature => "").to_s.should == OAuth2::Headers::Authorization.new(:token => "vF9dft4qmT").to_s
end
Expand Down
4 changes: 2 additions & 2 deletions oauth2-rails/app/models/oauth2/authorization_code_grant.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
class AuthorizationCodeGrant < ActiveRecord::Base

set_table_name 'oauth2_authorization_code_grants'

end
10 changes: 5 additions & 5 deletions oauth2-rails/lib/oauth2/rails.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,20 @@
require 'active_support/core_ext/module'

module OAuth2

module Rails

class << self

mattr_accessor :config
self.config = Config.new

def setup(&block)
config.setup(&block)
end

end

end

end
10 changes: 5 additions & 5 deletions oauth2-rails/lib/oauth2/rails/adapters/active_record.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ module Rails
module Adapters

class ActiveRecord < Base

def initialize
super
@models = Hash.new
Expand All @@ -16,20 +16,20 @@ def create_authorization_code_grant(attributes)
attributes.slice(:client, :user, :request_uri, :code)
)
end

# create_table :oauth2_authorization_code_grants do |t|
# t.string :code
# t.string :redirect_uri
# t.integer :user_id
# t.integer :client_id
# end

protected

def constantize_model(id)
OAuth2::Rails.config.models[:models][id].to_s.camelize.constantize
end

def model(id)
@models[id] ||= constantize_model(id)
end
Expand Down
8 changes: 4 additions & 4 deletions oauth2-rails/lib/oauth2/rails/adapters/base.rb
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
module OAuth2

module Rails

module Adapters

class Base

def initialize(options)
@options = options
end

# Must be implemented by subclasses
# Returns true on success, else false.
def create_authorization_code_grant(attributes)
Expand Down
4 changes: 2 additions & 2 deletions oauth2-rails/lib/oauth2/rails/config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@ module OAuth2
module Rails

class Config

class << self
# We alias this method to something shorter.
alias :option :attr_accessor_with_default
end

option :adapter, :active_record

option :enforce_scopes, true
Expand Down
Loading