Skip to content

Commit

Permalink
Modularized OauthProvider generator, added appropriate hooks for orm,
Browse files Browse the repository at this point in the history
test_framework and template, added generators for ActiveRecord, ERB,
Haml, TestUnit and RSpec. Also removed trailing spaces from all
templates, used in OauthPRovider generating.
  • Loading branch information
alsemyonov committed Oct 30, 2010
1 parent 91e1362 commit ead01d2
Show file tree
Hide file tree
Showing 55 changed files with 671 additions and 528 deletions.
39 changes: 39 additions & 0 deletions lib/generators/active_record/oauth_provider_generator.rb
@@ -0,0 +1,39 @@
require 'rails/generators/active_record'

module ActiveRecord
module Generators
class OauthProviderGenerator < Rails::Generators::Base
include Rails::Generators::Migration

source_root File.expand_path('../templates', __FILE__)

# Implement the required interface for Rails::Generators::Migration.
def self.next_migration_number(dirname) #:nodoc:
next_migration_number = current_migration_number(dirname) + 1
if ActiveRecord::Base.timestamped_migrations
[Time.now.utc.strftime("%Y%m%d%H%M%S"), "%.14d" % next_migration_number].max
else
"%.3d" % next_migration_number
end
end

def check_class_collisions
class_collisions '', %w(ClientApplication OauthNonce RequestToken AccessToken OauthToken)
end

def copy_models
template 'client_application.rb', File.join('app/models', 'client_application.rb')
template 'oauth_token.rb', File.join('app/models', 'oauth_token.rb')
template 'request_token.rb', File.join('app/models', 'request_token.rb')
template 'access_token.rb', File.join('app/models', 'access_token.rb')
template 'oauth2_token.rb', File.join('app/models', 'oauth2_token.rb')
template 'oauth2_verifier.rb', File.join('app/models', 'oauth2_verifier.rb')
template 'oauth_nonce.rb', File.join('app/models', 'oauth_nonce.rb')
end

def copy_migration
migration_template 'migration.rb', 'db/migrate/create_oauth_tables'
end
end
end
end
@@ -1,15 +1,15 @@
class AccessToken < OauthToken
validates_presence_of :user, :secret
before_create :set_authorized_at

# Implement this to return a hash or array of the capabilities the access token has
# This is particularly useful if you have implemented user defined permissions.
# def capabilities
# {:invalidate=>"/oauth/invalidate",:capabilities=>"/oauth/capabilities"}
# end
protected

protected

def set_authorized_at
self.authorized_at = Time.now
end
Expand Down
Expand Up @@ -14,7 +14,7 @@ class ClientApplication < ActiveRecord::Base
validates_format_of :callback_url, :with => /\Ahttp(s?):\/\/(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?/i, :allow_blank=>true

attr_accessor :token_callback_url

def self.find_token(token_key)
token = OauthToken.find_by_token(token_key, :include => :client_application)
if token && token.authorized?
Expand All @@ -23,7 +23,7 @@ def self.find_token(token_key)
nil
end
end

def self.verify_request(request, options = {}, &block)
begin
signature = OAuth::Signature.build(request, options, &block)
Expand All @@ -34,22 +34,22 @@ def self.verify_request(request, options = {}, &block)
false
end
end

def oauth_server
@oauth_server ||= OAuth::Server.new("http://your.site")
end

def credentials
@oauth_client ||= OAuth::Consumer.new(key, secret)
end

# If your application requires passing in extra parameters handle it here
def create_request_token(params={})
def create_request_token(params={})
RequestToken.create :client_application => self, :callback_url=>self.token_callback_url
end

protected

def generate_keys
self.key = OAuth::Helper.generate_key(40)[0,40]
self.secret = OAuth::Helper.generate_key(40)[0,40]
Expand Down
Expand Up @@ -12,7 +12,7 @@ def self.up
t.timestamps
end
add_index :client_applications, :key, :unique => true

create_table :oauth_tokens do |t|
t.integer :user_id
t.string :type, :limit => 20
Expand All @@ -25,17 +25,17 @@ def self.up
t.timestamp :authorized_at, :invalidated_at, :valid_to
t.timestamps
end

add_index :oauth_tokens, :token, :unique => true

create_table :oauth_nonces do |t|
t.string :nonce
t.integer :timestamp

t.timestamps
end
add_index :oauth_nonces,[:nonce, :timestamp], :unique => true

end

def self.down
Expand Down
@@ -1,5 +1,4 @@
class Oauth2Token < AccessToken

def as_json(options={})
{:access_token=>token}
end
Expand Down
@@ -1,28 +1,28 @@
class Oauth2Verifier < OauthToken
validates_presence_of :user

def exchange!(params={})
OauthToken.transaction do
token = Oauth2Token.create! :user=>user,:client_application=>client_application
invalidate!
token
end
end

def code
token
end

def redirect_url
callback_url
end

protected

def generate_keys
self.token = OAuth::Helper.generate_key(20)[0,20]
self.valid_to = 10.minutes.from_now
self.authorized_at = Time.now
end

end
Expand Up @@ -3,7 +3,7 @@
class OauthNonce < ActiveRecord::Base
validates_presence_of :nonce, :timestamp
validates_uniqueness_of :nonce, :scope => :timestamp

# Remembers a nonce and it's associated timestamp. It returns false if it has already been used
def self.remember(nonce, timestamp)
oauth_nonce = OauthNonce.create(:nonce => nonce, :timestamp => timestamp)
Expand Down
Expand Up @@ -4,25 +4,25 @@ class OauthToken < ActiveRecord::Base
validates_uniqueness_of :token
validates_presence_of :client_application, :token
before_validation :generate_keys, :on => :create

def invalidated?
invalidated_at != nil
end

def invalidate!
update_attribute(:invalidated_at, Time.now)
end

def authorized?
authorized_at != nil && !invalidated?
end

def to_query
"oauth_token=#{token}&oauth_token_secret=#{secret}"
end

protected

def generate_keys
self.token = OAuth::Helper.generate_key(40)[0,40]
self.secret = OAuth::Helper.generate_key(40)[0,40]
Expand Down
@@ -1,40 +1,40 @@
class RequestToken < OauthToken

attr_accessor :provided_oauth_verifier

def authorize!(user)
return false if authorized?
self.user = user
self.authorized_at = Time.now
self.verifier=OAuth::Helper.generate_key(20)[0,20] unless oauth10?
self.save
end

def exchange!
return false unless authorized?
return false unless oauth10? || verifier==provided_oauth_verifier

RequestToken.transaction do
access_token = AccessToken.create(:user => user, :client_application => client_application)
invalidate!
access_token
end
end

def to_query
if oauth10?
super
else
"#{super}&oauth_callback_confirmed=true"
end
end

def oob?
self.callback_url=='oob'
end

def oauth10?
(defined? OAUTH_10_SUPPORT) && OAUTH_10_SUPPORT && self.callback_url.blank?
end

end
end
21 changes: 21 additions & 0 deletions lib/generators/erb/oauth_provider_generator.rb
@@ -0,0 +1,21 @@
require 'rails/generators/erb'

module Erb
module Generators
class OauthProviderGenerator < Erb::Generators::Base
source_root File.expand_path('../templates', __FILE__)

def copy_view_files
template '_form.html.erb', File.join('app/views', class_path, 'oauth_clients', '_form.html.erb')
template 'new.html.erb', File.join('app/views', class_path, 'oauth_clients', 'new.html.erb')
template 'index.html.erb', File.join('app/views', class_path, 'oauth_clients', 'index.html.erb')
template 'show.html.erb', File.join('app/views', class_path, 'oauth_clients', 'show.html.erb')
template 'edit.html.erb', File.join('app/views', class_path, 'oauth_clients', 'edit.html.erb')
template 'authorize.html.erb', File.join('app/views', class_path, file_name, 'authorize.html.erb')
template 'oauth2_authorize.html.erb', File.join('app/views', class_path, file_name, 'oauth2_authorize.html.erb')
template 'authorize_success.html.erb', File.join('app/views', class_path, file_name, 'authorize_success.html.erb')
template 'authorize_failure.html.erb', File.join('app/views', class_path, file_name, 'authorize_failure.html.erb')
end
end
end
end
File renamed without changes.
Expand Up @@ -11,4 +11,4 @@
<p>
<%%= submit_tag %>
</p>
<%% end %>
<%% end %>
Expand Up @@ -4,4 +4,4 @@
<%%= submit_tag "Edit" %>
<%% end %>
<%%= link_to 'Show', oauth_client_path(@client_application) %> |
<%%= link_to 'Back', oauth_clients_path %>
<%%= link_to 'Back', oauth_clients_path %>
Expand Up @@ -16,7 +16,7 @@
</td>
<%% end %>
<%% end %>

</table>
<%% end %>
<h3>Application Developers</h3>
Expand All @@ -33,7 +33,7 @@
</p>
<%% @client_applications.each do |client|%>
<%% div_for client do %>
<%%= link_to client.name, oauth_client_path(client) %>-
<%%= link_to client.name, oauth_client_path(client) %>-
<%%= link_to 'Edit', edit_oauth_client_path(client) %>
<%%= link_to 'Delete', oauth_client_path(client), :confirm => "Are you sure?", :method => :delete %>
<%% end %>
Expand Down
Expand Up @@ -2,4 +2,4 @@
<%% form_for :client_application, :url => { :action => :create } do |f| %>
<%%= render :partial => "form", :locals => { :f => f } %>
<%%= submit_tag "Register" %>
<%% end %>
<%% end %>
Expand Up @@ -6,11 +6,11 @@
<%%= hidden_field_tag "redirect_url", params[:redirect_url]%>
<%%= hidden_field_tag "state", params[:state]%>
<%%= hidden_field_tag "scope", params[:scope]%>

<p>
<%%= check_box_tag 'authorize' %> authorize access
</p>
<p>
<%%= submit_tag %>
</p>
<%% end %>
<%% end %>
File renamed without changes.
28 changes: 28 additions & 0 deletions lib/generators/haml/oauth_provider_generator.rb
@@ -0,0 +1,28 @@
require 'rails/generators/erb/controller/controller_generator'

module Haml
module Generators
class OauthProviderGenerator < Erb::Generators::Base
source_root File.expand_path('../templates', __FILE__)

argument :name, :type => :string, :default => 'Oauth'

def copy_view_files
template '_form.html.haml', File.join('app/views', class_path, 'oauth_clients', '_form.html.haml')
template 'new.html.haml', File.join('app/views', class_path, 'oauth_clients', 'new.html.haml')
template 'index.html.haml', File.join('app/views', class_path, 'oauth_clients', 'index.html.haml')
template 'show.html.haml', File.join('app/views', class_path, 'oauth_clients', 'show.html.haml')
template 'edit.html.haml', File.join('app/views', class_path, 'oauth_clients', 'edit.html.haml')
template 'authorize.html.haml', File.join('app/views', class_path, file_name, 'authorize.html.haml')
template 'oauth2_authorize.html.haml', File.join('app/views', class_path, file_name, 'oauth2_authorize.html.haml')
template 'authorize_success.html.haml', File.join('app/views', class_path, file_name, 'authorize_success.html.haml')
template 'authorize_failure.html.haml', File.join('app/views', class_path, file_name, 'authorize_failure.html.haml')
end

protected
def handler
:haml
end
end
end
end
Expand Up @@ -2,20 +2,20 @@
.field
%label{:for=>"client_application_name"} Name*
%br
= f.text_field :name
= f.text_field :name

.field
%label{:for=>"client_application_url"} Main Application URL*
%br
= f.text_field :url
= f.text_field :url

.field
%label{:for=>"client_application_callback_url"} Callback URL*
%br
= f.text_field :callback_url
= f.text_field :callback_url

.field
%label{:for=>"client_application_support_url"} Support URL
%br
= f.text_field :support_url
= f.text_field :support_url

0 comments on commit ead01d2

Please sign in to comment.