authorizme 0.0.1.alpha

Sign up to get free protection for your applications and to get access to all the features.
Files changed (45) hide show
  1. data/.DS_Store +0 -0
  2. data/.gitignore +4 -0
  3. data/.rspec +1 -0
  4. data/.rvmrc +81 -0
  5. data/CHANGELOG.md +21 -0
  6. data/Gemfile +4 -0
  7. data/LICENSE +7 -0
  8. data/README.md +39 -0
  9. data/Rakefile +7 -0
  10. data/app/controllers/authorizme/authorizme_controller.rb +37 -0
  11. data/app/controllers/authorizme/login/draugiem_controller.rb +37 -0
  12. data/app/controllers/authorizme/login/facebook_controller.rb +29 -0
  13. data/app/controllers/authorizme/login/twitter_controller.rb +62 -0
  14. data/app/controllers/authorizme/sessions_controller.rb +29 -0
  15. data/app/controllers/authorizme/users_controller.rb +17 -0
  16. data/app/models/authorizme/user_provider.rb +13 -0
  17. data/app/models/authorizme/user_role.rb +5 -0
  18. data/app/views/authorizme/authorizme/index.html.erb +23 -0
  19. data/app/views/authorizme/users/new.html.erb +26 -0
  20. data/authorizme.gemspec +37 -0
  21. data/config/routes.rb +19 -0
  22. data/lib/.DS_Store +0 -0
  23. data/lib/authorizme/.DS_Store +0 -0
  24. data/lib/authorizme/acts_as_authorizme.rb +131 -0
  25. data/lib/authorizme/engine.rb +5 -0
  26. data/lib/authorizme/for_controllers.rb +33 -0
  27. data/lib/authorizme/provider/draugiem.rb +58 -0
  28. data/lib/authorizme/provider.rb +5 -0
  29. data/lib/authorizme/version.rb +3 -0
  30. data/lib/authorizme.rb +78 -0
  31. data/lib/generators/.DS_Store +0 -0
  32. data/lib/generators/authorizme/.DS_Store +0 -0
  33. data/lib/generators/authorizme/install_generator.rb +35 -0
  34. data/lib/generators/authorizme/templates/.DS_Store +0 -0
  35. data/lib/generators/authorizme/templates/authorizme.rb.erb +68 -0
  36. data/lib/generators/authorizme/templates/migrations/1_create_users.rb +33 -0
  37. data/lib/generators/authorizme/templates/migrations/2_create_user_providers.rb +22 -0
  38. data/lib/generators/authorizme/templates/migrations/3_create_user_roles.rb +13 -0
  39. data/lib/generators/authorizme/templates/models/user.rb +3 -0
  40. data/spec/.DS_Store +0 -0
  41. data/spec/authorizme/providers/draugiem_spec.rb +26 -0
  42. data/spec/authorizme_spec.rb +17 -0
  43. data/spec/factories.rb +14 -0
  44. data/spec/spec_helper.rb +8 -0
  45. metadata +250 -0
data/.DS_Store ADDED
Binary file
data/.gitignore ADDED
@@ -0,0 +1,4 @@
1
+ *.gem
2
+ .bundle
3
+ Gemfile.lock
4
+ pkg/*
data/.rspec ADDED
@@ -0,0 +1 @@
1
+ --color
data/.rvmrc ADDED
@@ -0,0 +1,81 @@
1
+ #!/usr/bin/env bash
2
+
3
+ # This is an RVM Project .rvmrc file, used to automatically load the ruby
4
+ # development environment upon cd'ing into the directory
5
+
6
+ # First we specify our desired <ruby>[@<gemset>], the @gemset name is optional.
7
+ environment_id="ruby-1.9.3-p125@rails32"
8
+
9
+ #
10
+ # Uncomment the following lines if you want to verify rvm version per project
11
+ #
12
+ # rvmrc_rvm_version="1.10.2" # 1.10.1 seams as a safe start
13
+ # eval "$(echo ${rvm_version}.${rvmrc_rvm_version} | awk -F. '{print "[[ "$1*65536+$2*256+$3" -ge "$4*65536+$5*256+$6" ]]"}' )" || {
14
+ # echo "This .rvmrc file requires at least RVM ${rvmrc_rvm_version}, aborting loading."
15
+ # return 1
16
+ # }
17
+ #
18
+
19
+ #
20
+ # Uncomment following line if you want options to be set only for given project.
21
+ #
22
+ # PROJECT_JRUBY_OPTS=( --1.9 )
23
+ #
24
+ # The variable PROJECT_JRUBY_OPTS requires the following to be run in shell:
25
+ #
26
+ # chmod +x ${rvm_path}/hooks/after_use_jruby_opts
27
+ #
28
+
29
+ #
30
+ # First we attempt to load the desired environment directly from the environment
31
+ # file. This is very fast and efficient compared to running through the entire
32
+ # CLI and selector. If you want feedback on which environment was used then
33
+ # insert the word 'use' after --create as this triggers verbose mode.
34
+ #
35
+ if [[ -d "${rvm_path:-$HOME/.rvm}/environments" \
36
+ && -s "${rvm_path:-$HOME/.rvm}/environments/$environment_id" ]]
37
+ then
38
+ \. "${rvm_path:-$HOME/.rvm}/environments/$environment_id"
39
+
40
+ if [[ -s "${rvm_path:-$HOME/.rvm}/hooks/after_use" ]]
41
+ then
42
+ . "${rvm_path:-$HOME/.rvm}/hooks/after_use"
43
+ fi
44
+ else
45
+ # If the environment file has not yet been created, use the RVM CLI to select.
46
+ if ! rvm --create use "$environment_id"
47
+ then
48
+ echo "Failed to create RVM environment '${environment_id}'."
49
+ return 1
50
+ fi
51
+ fi
52
+
53
+ #
54
+ # If you use an RVM gemset file to install a list of gems (*.gems), you can have
55
+ # it be automatically loaded. Uncomment the following and adjust the filename if
56
+ # necessary.
57
+ #
58
+ # filename=".gems"
59
+ # if [[ -s "$filename" ]]
60
+ # then
61
+ # rvm gemset import "$filename" | grep -v already | grep -v listed | grep -v complete | sed '/^$/d'
62
+ # fi
63
+
64
+ # If you use bundler, this might be useful to you:
65
+ # if [[ -s Gemfile ]] && ! command -v bundle >/dev/null
66
+ # then
67
+ # printf "%b" "The rubygem 'bundler' is not installed. Installing it now.\n"
68
+ # gem install bundler
69
+ # fi
70
+ # if [[ -s Gemfile ]] && command -v bundle
71
+ # then
72
+ # bundle install
73
+ # fi
74
+
75
+ if [[ $- == *i* ]] # check for interactive shells
76
+ then
77
+ echo "Using: $(tput setaf 2)$GEM_HOME$(tput sgr0)" # show the user the ruby and gemset they are using in green
78
+ else
79
+ echo "Using: $GEM_HOME" # don't use colors in interactive shells
80
+ fi
81
+
data/CHANGELOG.md ADDED
@@ -0,0 +1,21 @@
1
+ ## 0.0.1
2
+
3
+ * Initial release
4
+
5
+ ### Features & Enhancements
6
+
7
+ * Basic authentication
8
+ * Twitter.com authentication
9
+ * Draugiem.lv authentication
10
+ * Facebook.com authentication
11
+
12
+ ### Bug Fixes
13
+
14
+ ### Test Suite
15
+
16
+ * RSpec
17
+
18
+ ### Contributors
19
+
20
+ * Arturs Braucs
21
+ * Creative Mobile
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "http://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in authorizme.gemspec
4
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,7 @@
1
+ Copyright (C) 2012 Creative Mobile
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
4
+
5
+ The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
6
+
7
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,39 @@
1
+ # Authorizme
2
+
3
+ Simple authorization plugin for Ruby on Rails applications that by default includes basic authorization and 3 provider authorization with Latvia social network draugiem.lv, facebook.com and twitter.com.
4
+
5
+ ## Installation
6
+
7
+ Add to your Gemfile and run the `bundle` command to install it.
8
+
9
+ ```ruby
10
+ gem "authorizme"
11
+ ```
12
+
13
+ Run authorizme install generator from your app folder
14
+
15
+ ```ruby
16
+ rails g authorizme:install
17
+ ```
18
+
19
+ That will install:
20
+
21
+ * config file `authorizme.rb` in to `config/initializers`
22
+ * `User` model with `acts_as_authorizme` method
23
+ * migrations for authorizme
24
+
25
+ Then migrate your database `rake db:migrate`
26
+
27
+ **Requires Ruby 1.9.2 or later and Rails 3.2.1 or later.**
28
+
29
+ ## Usage
30
+
31
+ ### Getting started
32
+
33
+ ### Advanced usage
34
+
35
+ ## Development
36
+
37
+ Questions or problems? Please post them on the [issue tracker](https://github.com/CreativeMobile/authorizme/issues). You can contribute changes by forking the project and submitting a pull request. You can ensure the tests passing by running `bundle` and `rake`.
38
+
39
+ This gem is created by Arturs Braucs @ Creative Mobile and is under the MIT License.
data/Rakefile ADDED
@@ -0,0 +1,7 @@
1
+ require "bundler/gem_tasks"
2
+
3
+ require 'rspec/core/rake_task'
4
+
5
+ RSpec::Core::RakeTask.new(:spec)
6
+
7
+ task default: :spec
@@ -0,0 +1,37 @@
1
+ module Authorizme
2
+ class AuthorizmeController < ::ApplicationController
3
+ respond_to :html, :json, :xml
4
+
5
+ def index
6
+ end
7
+
8
+ protected
9
+
10
+ def login user
11
+ session[:user_id] = user.id
12
+ end
13
+
14
+ def logout
15
+ session[:user_id] = nil
16
+ if Authorizme::remote
17
+ respond_with_status "logged_out"
18
+ else
19
+ redirect_to Authorizme::after_logout_path
20
+ end
21
+ end
22
+
23
+ def respond_with_status status_name, attributes = nil
24
+ status = {status: status_name}
25
+ status = status.merge(attributes) if attributes
26
+ respond_with status
27
+ end
28
+
29
+ def redirect_uri provider
30
+ if Rails.env.development?
31
+ redirect_url = "http://127.0.0.1:3000/authorizme/login/twitter/callback.json"
32
+ else
33
+ "http://#{request.host}/#{Authorizme::namespace}/login/#{provider}/callback"
34
+ end
35
+ end
36
+ end
37
+ end
@@ -0,0 +1,37 @@
1
+ module Authorizme
2
+ module Login
3
+ class DraugiemController < AuthorizmeController
4
+ before_filter :set_draugiem
5
+
6
+ def auth
7
+ redirect_to @draugiem.login_url
8
+ end
9
+
10
+ def callback
11
+ json = @draugiem.authorize params[:dr_auth_status], params[:dr_auth_code]
12
+ if params[:dr_auth_status] == "ok" && json["users"]
13
+ user_json = json["users"][json["uid"]]
14
+ attributes = {first_name: user_json["name"], last_name: user_json["surname"], image_url: user_json["img"]}
15
+ user = User.authenticate_with_draugiem(json["uid"], attributes, json["apikey"])
16
+ login user
17
+ respond_with_status "logged_in", user: user
18
+ else
19
+ respond_with_status "error_in_loggin"
20
+ end
21
+ end
22
+
23
+ private
24
+
25
+ def set_draugiem
26
+ options = {
27
+ draugiem_app_id: Authorizme::draugiem_app_id,
28
+ draugiem_app_key: Authorizme::draugiem_app_key,
29
+ draugiem_api_path: Authorizme::draugiem_api_path,
30
+ draugiem_api_authorize_path: Authorizme::draugiem_api_authorize_path,
31
+ redirect_url: redirect_uri("draugiem")
32
+ }
33
+ @draugiem = Authorizme::Provider::Draugiem.new(options)
34
+ end
35
+ end
36
+ end
37
+ end
@@ -0,0 +1,29 @@
1
+ module Authorizme
2
+ module Login
3
+ class FacebookController < AuthorizmeController
4
+
5
+ def auth
6
+ redirect_to client.authorization.authorize_url(:redirect_uri => redirect_uri("facebook"),
7
+ :scope => Authorizme::facebook_perms,
8
+ :display => "popup")
9
+ end
10
+
11
+ def callback
12
+ access_token = client.authorization.process_callback(params[:code], :redirect_uri => redirect_uri("facebook"))
13
+ user_json = client.selection.me.info!
14
+ image_url = "https://graph.facebook.com/#{user_json.id}/picture?type=large"
15
+ attributes = {first_name: user_json.first_name, last_name: user_json.last_name, image_url: image_url}
16
+ user = User.authenticate_with_facebook(user_json.id, attributes, access_token)
17
+ login user
18
+ respond_with_status "logged_in", user: user
19
+ end
20
+
21
+ private
22
+
23
+ def client
24
+ @client ||= FBGraph::Client.new(:client_id => Authorizme::facebook_client_id,
25
+ :secret_id => Authorizme::facebook_client_secret)
26
+ end
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,62 @@
1
+ module Authorizme
2
+ module Login
3
+ class TwitterController < AuthorizmeController
4
+
5
+ def auth
6
+ client = oauth_client
7
+ request_token = client.authentication_request_token(:oauth_callback => redirect_uri("twitter"))
8
+ session[:twitter_request_token] = request_token.token
9
+ session[:twitter_request_secret] = request_token.secret
10
+ redirect_to request_token.authorize_url
11
+ end
12
+
13
+ def callback
14
+ if params[:denied]
15
+ respond_with_status "error_in_logging"
16
+ else
17
+ access_token = authorize_with_twitter params[:oauth_token], params[:oauth_verifier]
18
+ twitter_user = Twitter.user
19
+ attributes = {first_name: twitter_user.name, image_url: twitter_user.profile_image_url}
20
+ user = User.authenticate_with_twitter(twitter_user.id, attributes, access_token.token, access_token.secret)
21
+ login user
22
+ respond_with_status "logged_in", user: user
23
+ end
24
+ end
25
+
26
+ private
27
+
28
+ def oauth_client
29
+ TwitterOAuth::Client.new(
30
+ :consumer_key => Authorizme::twitter_consumer_key,
31
+ :consumer_secret => Authorizme::twitter_consumer_secret
32
+ )
33
+ end
34
+
35
+ def twitter_client access_token
36
+ Twitter.configure do |config|
37
+ config.consumer_key = Authorizme::twitter_consumer_key
38
+ config.consumer_secret = Authorizme::twitter_consumer_secret
39
+ config.oauth_token = access_token.token if access_token.token
40
+ config.oauth_token_secret = access_token.secret if access_token.secret
41
+ end
42
+ end
43
+
44
+ def authorize_with_twitter oauth_token, oauth_verifier
45
+ request_token = session[:twitter_request_token]
46
+ request_secret = session[:twitter_request_secret]
47
+ if request_token && request_secret
48
+ client = oauth_client
49
+ access_token = client.authorize(
50
+ request_token,
51
+ request_secret,
52
+ :oauth_verifier => oauth_verifier
53
+ )
54
+
55
+ twitter_client access_token
56
+ access_token
57
+ end
58
+ end
59
+
60
+ end
61
+ end
62
+ end
@@ -0,0 +1,29 @@
1
+ module Authorizme
2
+ class SessionsController < AuthorizmeController
3
+
4
+ def create
5
+ user = User.find_by_email(params[:email])
6
+ if user && user.authenticate(params[:password])
7
+ login user
8
+ if Authorizme::remote
9
+ status = {status: "logged_in", user: user}
10
+ respond_with status
11
+ else
12
+ redirect_to Authorizme::after_login_path
13
+ end
14
+ else
15
+ if Authorizme::remote
16
+ status = {status: "error"}
17
+ respond_with status
18
+ else
19
+ render "new"
20
+ end
21
+ end
22
+ end
23
+
24
+ def destroy
25
+ logout
26
+ end
27
+
28
+ end
29
+ end
@@ -0,0 +1,17 @@
1
+ module Authorizme
2
+ class UsersController < AuthorizmeController
3
+
4
+ def new
5
+ @user = User.new
6
+ end
7
+
8
+ def create
9
+ @user = User.new(params[:user])
10
+ if @user.save
11
+ redirect_to root_url, :notice => "Signed up!"
12
+ else
13
+ render "new"
14
+ end
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,13 @@
1
+ module Authorizme
2
+ class UserProvider < ActiveRecord::Base
3
+ #Relations
4
+ belongs_to :user
5
+ belongs_to :origin_user, :class_name => "User"
6
+
7
+ #Validations
8
+ # => Attributes
9
+ validates :social_id, :presence => true
10
+ validates :token, :presence => true
11
+
12
+ end
13
+ end
@@ -0,0 +1,5 @@
1
+ module Authorizme
2
+ class UserRole < ActiveRecord::Base
3
+ has_many :users
4
+ end
5
+ end
@@ -0,0 +1,23 @@
1
+ <h2>Authorizme plugin for authorization</h2>
2
+ <div>Authorizme version <%= Authorizme::VERSION %></div>
3
+
4
+ <h3>Authorize with your credentials:</h3>
5
+ <%= form_tag "/#{Authorizme::namespace}/sessions" do %>
6
+ <p>
7
+ <%= label_tag :email %><br />
8
+ <%= text_field_tag :email, params[:email] %>
9
+ </p>
10
+ <p>
11
+ <%= label_tag :password %><br />
12
+ <%= password_field_tag :password %>
13
+ </p>
14
+ <p class="button"><%= submit_tag "Log in" %></p>
15
+ <% end %>
16
+
17
+ <h3>or use one of those providers:</h3>
18
+ <%= link_to "Draugiem.lv", "/#{Authorizme::namespace}/login/draugiem" %>
19
+ <%= link_to "Twitter.com", "/#{Authorizme::namespace}/login/twitter" %>
20
+ <%= link_to "Facebook.com", "/#{Authorizme::namespace}/login/facebook" %>
21
+
22
+ <h3>or you can signup:</h3>
23
+ <%= link_to "Signup", "/#{Authorizme::namespace}/signup" %>
@@ -0,0 +1,26 @@
1
+ <h1>Sign Up</h1>
2
+ <%= form_for @user, :url => authorizme_users_path do |f| %>
3
+ <% if @user.errors.any? %>
4
+ <div class="error_messages">
5
+ <h2>Form is invalid</h2>
6
+ <ul>
7
+ <% for message in @user.errors.full_messages %>
8
+ <li><%= message %></li>
9
+ <% end %>
10
+ </ul>
11
+ </div>
12
+ <% end %>
13
+ <p>
14
+ <%= f.label :email %><br />
15
+ <%= f.text_field :email %>
16
+ </p>
17
+ <p>
18
+ <%= f.label :password %><br />
19
+ <%= f.password_field :password %>
20
+ </p>
21
+ <p>
22
+ <%= f.label :password_confirmation %><br />
23
+ <%= f.password_field :password_confirmation %>
24
+ </p>
25
+ <p class="button"><%= f.submit %></p>
26
+ <% end %>
@@ -0,0 +1,37 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "authorizme/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "authorizme"
7
+ s.version = Authorizme::VERSION
8
+ s.authors = ["Arturs Braucs", "Creative Mobile"]
9
+ s.email = ["arturs@creo.mobi"]
10
+ s.homepage = "https://github.com/CreativeMobile/Authorizme"
11
+ s.summary = %q{Simple authorization gem for basic and Oauth: facebook.com, twitter.com and draugiem.lv}
12
+ s.description = %q{Authorization that includes basic authorization and 3 social authorization with Latvia social network draugiem.lv, facebook.com and twitter.com.}
13
+
14
+ s.rubyforge_project = "authorizme"
15
+
16
+ s.files = `git ls-files`.split("\n")
17
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
18
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
19
+ s.require_paths = ["lib"]
20
+
21
+ s.add_development_dependency 'rake'
22
+ s.add_development_dependency 'rspec'
23
+ s.add_development_dependency 'rspec-rails'
24
+ s.add_development_dependency 'webrat'
25
+ s.add_development_dependency 'capybara'
26
+ s.add_development_dependency 'factory_girl_rails'
27
+
28
+ s.add_dependency "activesupport", "~> 3.2.1"
29
+ s.add_dependency "rails", "~> 3.2.1"
30
+ s.add_dependency "bcrypt-ruby"
31
+ s.add_dependency "json"
32
+ s.add_dependency "twitter_oauth"
33
+ s.add_dependency "twitter"
34
+ s.add_dependency "oauth2"
35
+ s.add_dependency "fbgraph"
36
+
37
+ end
data/config/routes.rb ADDED
@@ -0,0 +1,19 @@
1
+ Rails.application.routes.draw do
2
+ # All authorizme routes will be under custom namespace
3
+ namespace Authorizme::namespace do
4
+ resources :sessions
5
+ resources :users
6
+
7
+ get '/' => 'authorizme#index', :as => 'main'
8
+ get '/signup' => 'users#new', :as => 'signup'
9
+
10
+ # Declare all provider routes.
11
+ Authorizme::providers.each do |provider|
12
+ get "/login/#{provider}" => "login/#{provider}#auth", :as => "#{provider}_login"
13
+ get "/login/#{provider}/callback" => "login/#{provider}#callback", :as => "#{provider}_callback"
14
+ end
15
+
16
+ post '/login' => 'sessions#create', :as => 'login'
17
+ get '/logout' => 'sessions#destroy', :as => 'logout'
18
+ end
19
+ end
data/lib/.DS_Store ADDED
Binary file
Binary file
@@ -0,0 +1,131 @@
1
+ module Authorizme
2
+ module ActsAsAuthorizme
3
+ extend ActiveSupport::Concern
4
+
5
+ included do
6
+ end
7
+
8
+ module ClassMethods
9
+ def acts_as_authorizme
10
+ # Load bcrypt-ruby only when acts_as_authorizme is called. Need for password digest
11
+ gem 'bcrypt-ruby', '~> 3.0.0'
12
+ require 'bcrypt'
13
+
14
+ # Relations
15
+ belongs_to :role, :class_name => "Authorizme::UserRole", :foreign_key => "user_role_id"
16
+ belongs_to :origin_provider, :class_name => "Authorizme::UserProvider"
17
+ has_many :providers, :class_name => "Authorizme::UserProvider"
18
+
19
+ attr_reader :password
20
+ attr_accessible :first_name, :last_name, :image_url, :email, :password, :password_confirmation
21
+
22
+ # Validations
23
+ validates_confirmation_of :password
24
+ validates_presence_of :password_digest, :if => :has_not_provider?
25
+ validates_presence_of :email, :on => :create, :if => :has_not_provider?
26
+ validates_uniqueness_of :email, :if => :has_not_provider?
27
+
28
+ # Filters
29
+ before_create :set_default_role
30
+
31
+ include InstanceMethodsOnActivation
32
+
33
+ if respond_to?(:attributes_protected_by_default)
34
+ def self.attributes_protected_by_default
35
+ super + ['password_digest']
36
+ end
37
+ end
38
+
39
+ def method_missing(meth, *args, &block)
40
+ if meth.to_s =~ /^authenticate_with_(.+)$/
41
+ run_authenticate_with_provider($1, *args, &block)
42
+ else
43
+ super
44
+ end
45
+ end
46
+
47
+ #def respond_to?(meth)
48
+ # if meth.to_s =~ /^authenticate_with_.*$/
49
+ # true
50
+ # else
51
+ # super
52
+ # end
53
+ #end
54
+
55
+ protected
56
+
57
+ # authorize
58
+ # Finds or creates user provider and creates or updates user with social data
59
+ # => Attributes
60
+ # *provider* provider name, e.g. facebook. Default in gem draugiem, twitter, facebook
61
+ # From args:
62
+ # *social_id* social network user identity number
63
+ # *attributes* attributes from social nettwork. Can be set: first_name, last_name, image_url
64
+ # *token* token
65
+ # *secret* secret
66
+ #
67
+ def run_authenticate_with_provider provider, *args, &block
68
+ social_id = args[0]
69
+ attributes = args[1]
70
+ token = args[2]
71
+ secret = args[3]
72
+
73
+ user_provider = Authorizme::UserProvider.find_or_initialize_by_social_id_and_provider_type(social_id.to_s, provider)
74
+ user_provider.token = token
75
+ user_provider.secret = secret if secret
76
+ user_provider.save!
77
+ self.create_or_update_by_provider user_provider, attributes
78
+ end
79
+
80
+
81
+ def create_or_update_by_provider provider, attributes
82
+ unless provider.user
83
+ provider.user = User.new
84
+ provider.user.origin_provider = provider
85
+ provider.user.has_provider = true
86
+ provider.user.save!
87
+ provider.origin_user = provider.user
88
+ provider.user
89
+ end
90
+ provider.user.has_provider = true
91
+ provider.user.attributes = attributes
92
+ provider.user.save!
93
+ provider.save!
94
+ provider.user
95
+ end
96
+ end
97
+ end
98
+
99
+ module InstanceMethodsOnActivation
100
+ # Returns self if the password is correct, otherwise false.
101
+ def authenticate(unencrypted_password)
102
+ if BCrypt::Password.new(password_digest) == unencrypted_password
103
+ self
104
+ else
105
+ false
106
+ end
107
+ end
108
+
109
+ # Encrypts the password into the password_digest attribute.
110
+ def password=(unencrypted_password)
111
+ @password = unencrypted_password
112
+ unless unencrypted_password.blank?
113
+ self.password_digest = BCrypt::Password.create(unencrypted_password)
114
+ end
115
+ end
116
+
117
+ def has_not_provider?
118
+ !self.has_provider
119
+ end
120
+
121
+ private
122
+
123
+ def set_default_role
124
+ new_role = Authorizme::UserRole.find(:first)
125
+ self.role = new_role if new_role
126
+ end
127
+ end
128
+ end
129
+ end
130
+
131
+ ActiveRecord::Base.send :include, Authorizme::ActsAsAuthorizme
@@ -0,0 +1,5 @@
1
+ module Authorizme
2
+ class Engine < Rails::Engine
3
+ config.authorizme = Authorizme
4
+ end
5
+ end