rockauth 0.0.1.pre2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (53) hide show
  1. checksums.yaml +7 -0
  2. data/MIT-LICENSE +20 -0
  3. data/Rakefile +30 -0
  4. data/app/admin/authentication.rb +37 -0
  5. data/app/admin/provider_authentications.rb +24 -0
  6. data/app/admin/resource_owner.rb +79 -0
  7. data/app/controllers/rockauth/authentications_controller.rb +48 -0
  8. data/app/controllers/rockauth/me_controller.rb +93 -0
  9. data/app/controllers/rockauth/provider_authentications_controller.rb +72 -0
  10. data/app/helpers/rockauth/application_helper.rb +4 -0
  11. data/app/models/rockauth/authentication.rb +6 -0
  12. data/app/models/rockauth/provider_authentication.rb +9 -0
  13. data/app/models/rockauth/user.rb +10 -0
  14. data/app/serializers/rockauth/authentication_serializer.rb +24 -0
  15. data/app/serializers/rockauth/base_serializer.rb +6 -0
  16. data/app/serializers/rockauth/error_serializer.rb +5 -0
  17. data/app/serializers/rockauth/provider_authentication_serializer.rb +5 -0
  18. data/app/serializers/rockauth/user_serializer.rb +23 -0
  19. data/app/views/layouts/rockauth/application.html.erb +14 -0
  20. data/config/locales/en.yml +12 -0
  21. data/config/routes.rb +9 -0
  22. data/db/migrate/20150709065335_create_rockauth_users.rb +16 -0
  23. data/db/migrate/20150709071113_create_rockauth_provider_authentications.rb +16 -0
  24. data/db/migrate/20150709084233_create_rockauth_authentications.rb +23 -0
  25. data/lib/generators/rockauth/client_generator.rb +33 -0
  26. data/lib/generators/rockauth/install_generator.rb +59 -0
  27. data/lib/generators/rockauth/migrations_generator.rb +9 -0
  28. data/lib/generators/rockauth/models_generator.rb +11 -0
  29. data/lib/generators/templates/authentication.rb +4 -0
  30. data/lib/generators/templates/provider_authentication.rb +5 -0
  31. data/lib/generators/templates/rockauth_clients.yml +9 -0
  32. data/lib/generators/templates/rockauth_full_initializer.rb +41 -0
  33. data/lib/generators/templates/rockauth_providers.json +50 -0
  34. data/lib/generators/templates/user.rb +7 -0
  35. data/lib/rockauth.rb +15 -0
  36. data/lib/rockauth/authenticator.rb +51 -0
  37. data/lib/rockauth/authenticator/response.rb +32 -0
  38. data/lib/rockauth/client.rb +4 -0
  39. data/lib/rockauth/configuration.rb +51 -0
  40. data/lib/rockauth/controllers.rb +5 -0
  41. data/lib/rockauth/controllers/authentication.rb +36 -0
  42. data/lib/rockauth/engine.rb +15 -0
  43. data/lib/rockauth/errors.rb +18 -0
  44. data/lib/rockauth/models.rb +9 -0
  45. data/lib/rockauth/models/authentication.rb +151 -0
  46. data/lib/rockauth/models/provider_authentication.rb +59 -0
  47. data/lib/rockauth/models/provider_validation.rb +61 -0
  48. data/lib/rockauth/models/resource_owner.rb +31 -0
  49. data/lib/rockauth/models/user.rb +25 -0
  50. data/lib/rockauth/provider_user_information.rb +103 -0
  51. data/lib/rockauth/version.rb +3 -0
  52. data/lib/tasks/rockauth_tasks.rake +9 -0
  53. metadata +361 -0
@@ -0,0 +1,24 @@
1
+ module Rockauth
2
+ class AuthenticationSerializer < BaseSerializer
3
+ attributes(*%i(id token token_id expiration client_version device_identifier device_os device_os_version device_description))
4
+
5
+ has_one Rockauth::Configuration.resource_owner_class.model_name.element.to_sym
6
+ has_one :provider_authentication
7
+
8
+ def include_token_id
9
+ object.token.present?
10
+ end
11
+
12
+ def include_jwt?
13
+ object.token.present?
14
+ end
15
+
16
+ define_method Rockauth::Configuration.resource_owner_class.model_name.element do
17
+ object.resource_owner
18
+ end
19
+
20
+ define_method(:"include_#{Rockauth::Configuration.resource_owner_class.model_name.element}?") do
21
+ !scope.try(:include_authentication?)
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,6 @@
1
+ require 'active_model_serializers'
2
+ module Rockauth
3
+ class BaseSerializer < ActiveModel::Serializer
4
+ # embed :ids, include: true
5
+ end
6
+ end
@@ -0,0 +1,5 @@
1
+ module Rockauth
2
+ class ErrorSerializer < BaseSerializer
3
+ attributes(*%i(status_code message validation_errors))
4
+ end
5
+ end
@@ -0,0 +1,5 @@
1
+ module Rockauth
2
+ class ProviderAuthenticationSerializer < BaseSerializer
3
+ attributes :id, :provider, :provider_user_id
4
+ end
5
+ end
@@ -0,0 +1,23 @@
1
+ module Rockauth
2
+ class UserSerializer < BaseSerializer
3
+ attributes :id, :email, :first_name, :last_name
4
+
5
+ has_one :authentication
6
+ has_many :provider_authentications
7
+
8
+ def authentication
9
+ object.authentications.first
10
+ end
11
+
12
+ def include_authentication?
13
+ current_resource_owner? && scope.try(:include_authentication?)
14
+ end
15
+
16
+ def current_resource_owner?
17
+ scope.try(:current_resource_owner) == object
18
+ end
19
+
20
+ alias :include_provider_authentications? :current_resource_owner?
21
+ alias :include_email? :current_resource_owner?
22
+ end
23
+ end
@@ -0,0 +1,14 @@
1
+ <!DOCTYPE html>
2
+ <html>
3
+ <head>
4
+ <title>Rockauth</title>
5
+ <%= stylesheet_link_tag "rockauth/application", media: "all" %>
6
+ <%= javascript_include_tag "rockauth/application" %>
7
+ <%= csrf_meta_tags %>
8
+ </head>
9
+ <body>
10
+
11
+ <%= yield %>
12
+
13
+ </body>
14
+ </html>
@@ -0,0 +1,12 @@
1
+ en:
2
+ errors:
3
+ messages:
4
+ rockauth_cannot_be_changed: "cannot be changed"
5
+ rockauth:
6
+ errors:
7
+ unauthorized: "You must be logged in to continue."
8
+ authentication_failed: "Authentication failure."
9
+ server_error: "Something went wrong."
10
+ create_error: "%{resource} could not be created."
11
+ update_error: "%{resource} could not be updated."
12
+ destroy_error: "%{resource} could not be destroyed."
@@ -0,0 +1,9 @@
1
+ Rockauth::Engine.routes.draw do
2
+ get :authentications, controller: 'rockauth/authentications', action: 'index'
3
+ post :authentications, controller: 'rockauth/authentications', action: 'authenticate'
4
+ delete :authentications, controller: 'rockauth/authentications', action: 'destroy'
5
+ delete 'authentications/:id', controller: 'rockauth/authentications', action: 'destroy'
6
+
7
+ resource :me, only: [:show, :create, :update, :destroy], controller: 'rockauth/me'
8
+ resources :provider_authentications, controller: 'rockauth/provider_authentications'
9
+ end
@@ -0,0 +1,16 @@
1
+ class CreateRockauthUsers < ActiveRecord::Migration
2
+ def up
3
+ create_table :users do |t|
4
+ t.string :first_name
5
+ t.string :last_name
6
+ t.string :email
7
+ t.string :password_digest
8
+ t.timestamps null: false
9
+ end
10
+ execute "CREATE UNIQUE INDEX index_users_on_lower_email ON users (lower(email))"
11
+ end
12
+
13
+ def down
14
+ drop_table :users
15
+ end
16
+ end
@@ -0,0 +1,16 @@
1
+ class CreateRockauthProviderAuthentications < ActiveRecord::Migration
2
+ def change
3
+ create_table :provider_authentications do |t|
4
+ t.references :resource_owner, polymorphic: true
5
+ t.string :provider, null: false
6
+ t.string :provider_user_id, null: false, index: true
7
+ t.string :provider_access_token, null: false
8
+ t.string :provider_access_token_secret
9
+
10
+ t.timestamps null: false
11
+ end
12
+
13
+ add_index :provider_authentications, [:provider, :provider_user_id], unique: true
14
+ add_index :provider_authentications, [:resource_owner_id, :resource_owner_type], name: 'index_provider_authentications_on_resource_owner'
15
+ end
16
+ end
@@ -0,0 +1,23 @@
1
+ class CreateRockauthAuthentications < ActiveRecord::Migration
2
+ def change
3
+ create_table :authentications do |t|
4
+ t.references :resource_owner, polymorphic: true
5
+ t.references :provider_authentication, index: true, foreign_key: true
6
+ t.integer :expiration
7
+ t.integer :issued_at
8
+ t.string :hashed_token_id
9
+ t.string :auth_type, null: false
10
+ t.string :client_id, null: false
11
+ t.string :client_version
12
+ t.string :device_identifier
13
+ t.string :device_description
14
+ t.string :device_os
15
+ t.string :device_os_version
16
+
17
+ t.timestamps null: false
18
+ end
19
+
20
+ add_index :authentications, [:resource_owner_id, :resource_owner_type], name: 'index_authentications_on_resource_owner'
21
+ add_index :authentications, :hashed_token_id
22
+ end
23
+ end
@@ -0,0 +1,33 @@
1
+ require 'securerandom'
2
+
3
+ module Rockauth
4
+ class ClientGenerator < Rails::Generators::NamedBase
5
+ source_root File.expand_path('../../templates', __FILE__)
6
+ desc "Generate a rockauth client"
7
+
8
+ class_option :environment, default: 'production', desc: 'Environment for the client'
9
+
10
+ def generate_client
11
+ client_id = SecureRandom.urlsafe_base64(16)
12
+ client_secret = SecureRandom.urlsafe_base64(32)
13
+
14
+ file_path = Rails.root.join("config/rockauth_clients.yml")
15
+ environments = {}
16
+
17
+ if File.exists?(file_path)
18
+ environments = YAML.load_file(file_path)
19
+ end
20
+
21
+ environments[options[:environment]] ||= []
22
+ environments[options[:environment]] << {
23
+ 'client_id' => client_id,
24
+ 'client_title' => name,
25
+ 'client_secret' => client_secret
26
+ }
27
+
28
+ File.open(file_path, 'wb') do |file|
29
+ file.write environments.stringify_keys.to_yaml(cannonical: false).gsub(/\A---\n/, '')
30
+ end
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,59 @@
1
+ require_relative './client_generator.rb'
2
+ module Rockauth
3
+ class InstallGenerator < Rails::Generators::Base
4
+ source_root File.expand_path('../../templates', __FILE__)
5
+
6
+ desc 'Installs Rockauth'
7
+
8
+ def copy_initializer
9
+ template 'rockauth_full_initializer.rb', 'config/initializers/rockauth.rb'
10
+ end
11
+
12
+ def copy_locales
13
+ copy_file '../../../config/locales/en.yml', 'config/locales/rockauth.en.yml'
14
+ end
15
+
16
+ def copy_clients
17
+ copy_file 'rockauth_clients.yml', 'config/rockauth_clients.yml'
18
+ end
19
+
20
+ def generate_models
21
+ invoke 'rockauth:models'
22
+ end
23
+
24
+ def generate_migrations
25
+ invoke 'rockauth:migrations'
26
+ end
27
+
28
+ def generate_serializers
29
+ puts File.expand_path('../../../app/serializers/rockauth/*.rb', File.dirname(__FILE__))
30
+ Dir[File.expand_path('../../../app/serializers/rockauth/*.rb', File.dirname(__FILE__))].each do |f|
31
+ basename = File.basename(f)
32
+ copy_file f, "app/serializers/#{basename}"
33
+ gsub_file "app/serializers/#{basename}", 'module Rockauth', ''
34
+ gsub_file "app/serializers/#{basename}", /^end$/, ''
35
+ gsub_file "app/serializers/#{basename}", /^\s\s/, ''
36
+ end
37
+ end
38
+
39
+ def install_route
40
+ route <<ROUTE
41
+ namespace :api do
42
+ mount Rockauth::Engine => '/'
43
+ end
44
+ ROUTE
45
+ end
46
+
47
+ def generate_development_client
48
+ invoke 'rockauth:client', ['Default Client'], environment: 'development'
49
+ end
50
+
51
+ def declare_dependencies
52
+ gem 'fb_graph2'
53
+ gem 'twitter'
54
+ gem 'google_plus'
55
+ gem 'instagram'
56
+ gem 'active_model_serializers', '~> 0.8.3'
57
+ end
58
+ end
59
+ end
@@ -0,0 +1,9 @@
1
+ module Rockauth
2
+ class MigrationsGenerator < Rails::Generators::Base
3
+ def install_migrations
4
+ Dir[File.expand_path("../../../../db/migrate/*.rb", __FILE__)].each do |file|
5
+ copy_file file, "db/migrate/#{File.basename(file)}"
6
+ end
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,11 @@
1
+ module Rockauth
2
+ class ModelsGenerator < Rails::Generators::Base
3
+ source_root File.expand_path('../../templates', __FILE__)
4
+
5
+ def install_models
6
+ template 'user.rb', 'app/models/user.rb'
7
+ template 'provider_authentication.rb', 'app/models/provider_authentication.rb'
8
+ template 'authentication.rb', 'app/models/authentication.rb'
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,4 @@
1
+ class Authentication < ActiveRecord::Base
2
+ include Rockauth::Models::Authentication
3
+ rockauth_authentication provider_authentication_class_name: "::ProviderAuthentication"
4
+ end
@@ -0,0 +1,5 @@
1
+ class ProviderAuthentication < ActiveRecord::Base
2
+ include Rockauth::Models::ProviderValidation
3
+ include Rockauth::Models::ProviderAuthentication
4
+ provider_authentication authentication_class_name: '::Authentication'
5
+ end
@@ -0,0 +1,9 @@
1
+ # List Rockauth clients for each environment here.
2
+ production:
3
+ #- client_id: random string
4
+ # client_secret: secret string
5
+ # client_title: iOS Client
6
+
7
+ development:
8
+
9
+ test:
@@ -0,0 +1,41 @@
1
+ Rockauth.configure do |config|
2
+ # config.allowed_password_length = 8..72
3
+ # config.email_regexp = /\A[^@\s]+@([^@\s]+\.)+[^@\W]+\z/
4
+ # config.token_time_to_live = 365 * 24 * 60 * 60
5
+ # config.clients = []
6
+ # config.resource_owner_class = 'Rockauth::User'
7
+ # config.warn_missing_social_auth_gems = true
8
+ # config.jwt.issuer = ''
9
+ # config.jwt.signing_method = 'HS256'
10
+
11
+ config.jwt.secret = '<%= SecureRandom.base64(32) %>'
12
+ config.resource_owner_class = '::User'
13
+
14
+ config.serializers.user = '::UserSerializer'
15
+ config.serializers.authentication = '::AuthenticationSerializer'
16
+ config.serializers.provider_authentication = '::ProviderAuthenticationSerializer'
17
+ config.serializers.error = '::ErrorSerializer'
18
+
19
+ # config.generate_active_admin_resources = nil # nil decides based on whether active_admin is loaded
20
+ # config.active_admin_menu_name = 'User Authentication'
21
+
22
+ begin
23
+ Array(YAML.load_file(Rails.root.join('config/rockauth_clients.yml'))[Rails.env]).each do |client_config|
24
+ config.clients << Rockauth::Client.new(*(%w(id secret title).map { |k| client_config["client_#{k}"] }))
25
+ end
26
+ rescue Errno::ENOENT
27
+ warn 'Could not load Rockauth clients from config/rockauth_clients.yml'
28
+ end
29
+
30
+ begin
31
+ parsed_json = JSON.parse(File.read(Rails.root.join('config/rockauth_providers.json')))[Rails.env] || {}
32
+ OpenStruct.new(parsed_json.with_indifferent_access)
33
+ rescue Errno::ENOENT
34
+ warn 'Could not load Rockauth providers from config/rockauth_providers.json'
35
+ end
36
+
37
+ Instagram.configure do |instagram_config|
38
+ instagram_config.client_id = config.providers.instagram[:client_id]
39
+ instagram_config.client_secret = config.providers.instagram[:client_secret]
40
+ end
41
+ end
@@ -0,0 +1,50 @@
1
+ {
2
+ "development": {
3
+ "twitter": {
4
+ "consumer_key": "",
5
+ "consumer_secret": ""
6
+ },
7
+ "instagram": {
8
+ "client_id": "",
9
+ "client_secret": ""
10
+ },
11
+ "twitter": {},
12
+ "google_plus": {}
13
+ },
14
+ "qa": {
15
+ "twitter": {
16
+ "consumer_key": "",
17
+ "consumer_secret": ""
18
+ },
19
+ "instagram": {
20
+ "client_id": "",
21
+ "client_secret": ""
22
+ },
23
+ "twitter": {},
24
+ "google_plus": {}
25
+ },
26
+ "staging": {
27
+ "twitter": {
28
+ "consumer_key": "",
29
+ "consumer_secret": ""
30
+ },
31
+ "instagram": {
32
+ "client_id": "",
33
+ "client_secret": ""
34
+ },
35
+ "twitter": {},
36
+ "google_plus": {}
37
+ },
38
+ "production": {
39
+ "twitter": {
40
+ "consumer_key": "",
41
+ "consumer_secret": ""
42
+ },
43
+ "instagram": {
44
+ "client_id": "",
45
+ "client_secret": ""
46
+ },
47
+ "twitter": {},
48
+ "google_plus": {}
49
+ }
50
+ }
@@ -0,0 +1,7 @@
1
+ class User < ActiveRecord::Base
2
+ include Rockauth::Models::ResourceOwner
3
+
4
+ resource_owner provider_authentication_class_name: '::ProviderAuthentication', authentication_class_name: '::Authentication'
5
+
6
+ include Rockauth::Models::User
7
+ end
@@ -0,0 +1,15 @@
1
+ require 'rails'
2
+ require 'active_support'
3
+ module Rockauth
4
+ extend ActiveSupport::Autoload
5
+ autoload :Authenticator
6
+ autoload :Client
7
+ autoload :Configuration
8
+ autoload :Controllers
9
+ autoload :Engine
10
+ autoload :Errors
11
+ autoload :Models
12
+ autoload :ProviderUserInformation
13
+ end
14
+ require 'rockauth/configuration'
15
+ require 'rockauth/engine'
@@ -0,0 +1,51 @@
1
+ module Rockauth
2
+ class Authenticator
3
+ autoload :Response, 'rockauth/authenticator/response'
4
+ attr_accessor :request
5
+ attr_accessor :controller
6
+ attr_accessor :response
7
+
8
+ delegate :params, to: :controller
9
+
10
+ def self.default_resource_owner_class
11
+ Rockauth::Configuration.resource_owner_class
12
+ end
13
+
14
+ def self.verified_authentication_for_request request, controller
15
+ bearer, token = request.env['HTTP_AUTHORIZATION'].to_s.split(' ')
16
+ if bearer.to_s.downcase == "bearer" && token.present?
17
+ default_resource_owner_class.authentication_class.for_token(token)
18
+ else
19
+ nil
20
+ end
21
+ end
22
+
23
+ def self.authentication_request request, controller
24
+ instance = new request, controller
25
+
26
+ resource_owner_class = controller.try(:resource_owner_class) || default_resource_owner_class
27
+
28
+ instance.authenticate resource_owner_class
29
+ instance.response
30
+ end
31
+
32
+ def initialize request, controller
33
+ self.request = request
34
+ self.controller = controller
35
+ end
36
+
37
+ def authenticate resource_owner_class=self.class.default_resource_owner_class
38
+ self.response = Response.new false
39
+ authentication_params = params.permit(authentication: authentication_permitted_params).fetch(:authentication, {}).merge(resource_owner_class: resource_owner_class)
40
+ if authentication_params.has_key? :provider_authentication
41
+ authentication_params[:provider_authentication_attributes] = authentication_params.delete(:provider_authentication)
42
+ end
43
+ response.authentication = resource_owner_class.authentication_class.new((controller.try(:authentication_options) || {}).merge(authentication_params))
44
+ response.apply
45
+ end
46
+
47
+ def authentication_permitted_params
48
+ [*%i(auth_type client_id client_secret username password client_version device_identifier device_description device_os device_os_version), provider_authentication: %i(provider provider_access_token provider_access_token_secret)]
49
+ end
50
+ end
51
+ end