stormpath-rails 1.0.0.beta.2 → 1.1.0.beta

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 (89) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +12 -19
  3. data/.rspec +0 -1
  4. data/.travis.yml +4 -23
  5. data/Gemfile +18 -0
  6. data/README.md +163 -73
  7. data/Rakefile +18 -3
  8. data/app/assets/stylesheets/stormpath.css.scss +283 -0
  9. data/app/controllers/stormpath/rails/base_controller.rb +10 -0
  10. data/app/controllers/stormpath/rails/omniauth_controller.rb +11 -0
  11. data/app/controllers/stormpath/rails/passwords_controller.rb +56 -0
  12. data/app/controllers/stormpath/rails/sessions_controller.rb +52 -0
  13. data/app/controllers/stormpath/rails/users_controller.rb +65 -0
  14. data/app/helpers/social_helper.rb +13 -0
  15. data/app/views/layouts/stormpath.html.erb +31 -0
  16. data/app/views/passwords/edit.html.erb +0 -0
  17. data/app/views/passwords/email_sent.html.erb +15 -0
  18. data/app/views/passwords/forgot.html.erb +33 -0
  19. data/app/views/passwords/forgot_change.html.erb +38 -0
  20. data/app/views/passwords/forgot_change_failed.html.erb +14 -0
  21. data/app/views/passwords/forgot_complete.html.erb +19 -0
  22. data/app/views/sessions/_facebook_login_form.erb +31 -0
  23. data/app/views/sessions/_form.html.erb +31 -0
  24. data/app/views/sessions/_google_login_form.html.erb +3 -0
  25. data/app/views/sessions/_social_auth.html.erb +7 -0
  26. data/app/views/sessions/new.html.erb +21 -0
  27. data/app/views/users/_form.html.erb +43 -0
  28. data/app/views/users/new.html.erb +10 -0
  29. data/app/views/users/verification_complete.html.erb +20 -0
  30. data/app/views/users/verification_email_sent.html.erb +15 -0
  31. data/app/views/users/verification_failed.html.erb +14 -0
  32. data/app/views/users/verification_resend.html.erb +14 -0
  33. data/bin/console +14 -0
  34. data/bin/rails +12 -0
  35. data/bin/rake +16 -0
  36. data/bin/rspec +16 -0
  37. data/bin/setup +8 -0
  38. data/config/initializers/assets.rb +1 -0
  39. data/config/routes.rb +16 -0
  40. data/lib/generators/stormpath/install/install_generator.rb +114 -0
  41. data/lib/generators/stormpath/install/templates/db/migrate/add_stormpath_to_users.rb +21 -0
  42. data/lib/generators/stormpath/install/templates/db/migrate/create_users.rb +12 -0
  43. data/lib/generators/stormpath/install/templates/stormpath.rb +4 -0
  44. data/lib/generators/stormpath/install/templates/user.rb +3 -0
  45. data/lib/generators/stormpath/routes/routes_generator.rb +23 -0
  46. data/lib/generators/stormpath/routes/templates/routes.rb +5 -0
  47. data/lib/generators/stormpath/views/USAGE +11 -0
  48. data/lib/generators/stormpath/views/views_generator.rb +29 -0
  49. data/lib/stormpath/rails/account.rb +2 -116
  50. data/lib/stormpath/rails/account_status.rb +28 -0
  51. data/lib/stormpath/rails/authentication.rb +72 -0
  52. data/lib/stormpath/rails/authentication_status.rb +22 -0
  53. data/lib/stormpath/rails/client.rb +73 -60
  54. data/lib/stormpath/rails/configuration.rb +56 -0
  55. data/lib/stormpath/rails/controller.rb +13 -0
  56. data/lib/stormpath/rails/engine.rb +6 -0
  57. data/lib/stormpath/rails/session.rb +37 -0
  58. data/lib/stormpath/rails/social.rb +34 -0
  59. data/lib/stormpath/rails/user.rb +25 -0
  60. data/lib/stormpath/rails/user_config/api_key.rb +17 -0
  61. data/lib/stormpath/rails/user_config/application.rb +12 -0
  62. data/lib/stormpath/rails/user_config/facebook.rb +16 -0
  63. data/lib/stormpath/rails/user_config/forgot_password.rb +12 -0
  64. data/lib/stormpath/rails/user_config/google.rb +16 -0
  65. data/lib/stormpath/rails/user_config/id_site.rb +13 -0
  66. data/lib/stormpath/rails/user_config/login.rb +13 -0
  67. data/lib/stormpath/rails/user_config/logout.rb +13 -0
  68. data/lib/stormpath/rails/user_config/register.rb +13 -0
  69. data/lib/stormpath/rails/user_config/verify_email.rb +14 -0
  70. data/lib/stormpath/rails/version.rb +1 -2
  71. data/lib/stormpath/rails.rb +32 -0
  72. data/lib/stormpath/testing/helpers.rb +49 -0
  73. data/lib/stormpath-rails.rb +6 -1
  74. data/stormpath-rails.gemspec +22 -28
  75. metadata +97 -256
  76. data/.ruby-gemset +0 -1
  77. data/.ruby-version +0 -1
  78. data/CHANGES.md +0 -19
  79. data/Guardfile +0 -23
  80. data/LICENSE.txt +0 -22
  81. data/lib/generators/stormpath/rails/migration/migration_generator.rb +0 -17
  82. data/lib/generators/stormpath/rails/templates/update_account_model.rb +0 -10
  83. data/lib/stormpath/rails/railtie.rb +0 -13
  84. data/spec/client_spec.rb +0 -222
  85. data/spec/generators/migration_generator_spec.rb +0 -19
  86. data/spec/integration/active_record_spec.rb +0 -23
  87. data/spec/integration/mongoid_spec.rb +0 -19
  88. data/spec/spec_helper.rb +0 -96
  89. data/spec/support/stormpath_account_shared_examples.rb +0 -212
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/bin/sh
2
+ set -e
3
+
4
+ # Install required gems
5
+ bundle install
6
+
7
+ # Set up database for the application that Clearance tests against
8
+ RAILS_ENV=test bundle exec rake dummy:db:reset
@@ -0,0 +1 @@
1
+ Rails.application.config.assets.precompile += %w( stormpath.css )
data/config/routes.rb ADDED
@@ -0,0 +1,16 @@
1
+ Rails.application.routes.draw do
2
+ resource :session, controller: 'stormpath/rails/sessions', only: [:create]
3
+ resource :users, controller: 'stormpath/rails/users', only: :create
4
+ get Stormpath::Rails.config.register.uri => 'stormpath/rails/users#new', as: 'sign_up'
5
+ get '/verify' => 'stormpath/rails/users#verify', as: 'verify'
6
+
7
+ get Stormpath::Rails.config.login.uri => 'stormpath/rails/sessions#new', as: 'sign_in'
8
+ delete Stormpath::Rails.config.logout.uri => 'stormpath/rails/sessions#destroy', as: 'sign_out'
9
+ get Stormpath::Rails.config.id_site.uri => 'stormpath/rails/sessions#redirect', as: 'redirect'
10
+ get '/omniauth_login' => 'stormpath/rails/omniauth#create', as: 'omniauth_login'
11
+
12
+ get Stormpath::Rails.config.forgot_password.uri => 'stormpath/rails/passwords#forgot', as: 'forgot'
13
+ post Stormpath::Rails.config.forgot_password.uri => 'stormpath/rails/passwords#forgot_send', as: 'forgot_send'
14
+ get '/forgot/change' => 'stormpath/rails/passwords#forgot_change', as: 'forgot_change'
15
+ post '/forgot/change/:account_url' => 'stormpath/rails/passwords#forgot_update', as: 'forgot_update'
16
+ end
@@ -0,0 +1,114 @@
1
+ require 'rails/generators/base'
2
+ require 'rails/generators/active_record'
3
+
4
+ module Stormpath
5
+ module Generators
6
+ class InstallGenerator < ::Rails::Generators::Base
7
+ include ::Rails::Generators::Migration
8
+ source_root File.expand_path('../templates', __FILE__)
9
+
10
+ def create_stormpath_configuration_file
11
+ copy_file 'stormpath.rb', 'config/initializers/stormpath.rb'
12
+ end
13
+
14
+ def inject_stormpath_into_application_controller
15
+ inject_into_class(
16
+ 'app/controllers/application_controller.rb',
17
+ ApplicationController,
18
+ " include Stormpath::Rails::Controller\n"
19
+ )
20
+ end
21
+
22
+ def create_user_model
23
+ if File.exist?(destination_root + '/app/models/user.rb')
24
+ inject_into_file(
25
+ 'app/models/user.rb',
26
+ "include Stormpath::Rails::User\n\n",
27
+ after: "class User < ActiveRecord::Base\n"
28
+ )
29
+ else
30
+ copy_file 'user.rb', 'app/models/user.rb'
31
+ end
32
+ end
33
+
34
+ def create_stormpath_migration
35
+ if user_table_exists?
36
+ create_add_columns_migration
37
+ else
38
+ copy_migration 'create_users.rb'
39
+ end
40
+ end
41
+
42
+ private
43
+
44
+ def create_add_columns_migration
45
+ return unless migration_needed?
46
+
47
+ config = {
48
+ new_columns: new_columns,
49
+ new_indexes: new_indexes
50
+ }
51
+
52
+ copy_migration('add_stormpath_to_users.rb', config)
53
+ end
54
+
55
+ def migration_needed?
56
+ new_columns.any? || new_indexes.any?
57
+ end
58
+
59
+ def new_columns
60
+ @new_columns ||= {
61
+ email: 't.string :email, null: false',
62
+ given_name: 't.string :given_name, null: false',
63
+ surname: 't.string :surname, null: false'
64
+ }.reject { |column| existing_users_columns.include?(column.to_s) }
65
+ end
66
+
67
+ def new_indexes
68
+ @new_indexes ||= {
69
+ index_users_on_email: 'add_index :users, :email'
70
+ }.reject { |index| existing_users_indexes.include?(index.to_s) }
71
+ end
72
+
73
+ def user_table_exists?
74
+ ActiveRecord::Base.connection.table_exists?(:users)
75
+ end
76
+
77
+ def copy_migration(migration_name, config = {})
78
+ return if migration_exists?(migration_name)
79
+ migration_template(
80
+ "db/migrate/#{migration_name}",
81
+ "db/migrate/#{migration_name}",
82
+ config
83
+ )
84
+ end
85
+
86
+ def migration_exists?(name)
87
+ existing_migrations.include?(name)
88
+ end
89
+
90
+ def existing_migrations
91
+ @existing_migrations ||= Dir.glob('db/migrate/*.rb').map do |file|
92
+ migration_name_without_timestamp(file)
93
+ end
94
+ end
95
+
96
+ def migration_name_without_timestamp(file)
97
+ file.sub(%r{^.*(db/migrate/)(?:\d+_)?}, '')
98
+ end
99
+
100
+ # for generating a timestamp when using `create_migration`
101
+ def self.next_migration_number(dir)
102
+ ActiveRecord::Generators::Base.next_migration_number(dir)
103
+ end
104
+
105
+ def existing_users_columns
106
+ ActiveRecord::Base.connection.columns(:users).map(&:name)
107
+ end
108
+
109
+ def existing_users_indexes
110
+ ActiveRecord::Base.connection.indexes(:users).map(&:name)
111
+ end
112
+ end
113
+ end
114
+ end
@@ -0,0 +1,21 @@
1
+ class AddStormpathToUsers < ActiveRecord::Migration
2
+ def self.up
3
+ change_table :users do |t|
4
+ <% config[:new_columns].values.each do |column| -%>
5
+ <%= column %>
6
+ <% end -%>
7
+ end
8
+
9
+ <% config[:new_indexes].values.each do |index| -%>
10
+ <%= index %>
11
+ <% end -%>
12
+ end
13
+
14
+ def self.down
15
+ change_table :users do |t|
16
+ <% if config[:new_columns].any? -%>
17
+ t.remove <%= new_columns.keys.map { |column| ":#{column}" }.join(",") %>
18
+ <% end -%>
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,12 @@
1
+ class CreateUsers < ActiveRecord::Migration
2
+ def change
3
+ create_table :users do |t|
4
+ t.timestamps null: false
5
+ t.string :email, null: false
6
+ t.string :given_name, null: false
7
+ t.string :surname, null: false
8
+ end
9
+
10
+ add_index :users, :email
11
+ end
12
+ end
@@ -0,0 +1,4 @@
1
+ Stormpath::Rails.configure do |config|
2
+ config.api_key_file = ENV['STORMPATH_API_KEY_FILE_LOCATION']
3
+ config.application = ENV['STORMPATH_SDK_TEST_APPLICATION_URL']
4
+ end
@@ -0,0 +1,3 @@
1
+ class User < ActiveRecord::Base
2
+ include Stormpath::Rails::User
3
+ end
@@ -0,0 +1,23 @@
1
+ require 'rails/generators/base'
2
+
3
+ module Stormpath
4
+ module Generators
5
+ class RoutesGenerator < ::Rails::Generators::Base
6
+ source_root File.expand_path('../templates', __FILE__)
7
+
8
+ def inject_stormpath_routes_into_application_routes
9
+ route(stormpath_routes)
10
+ end
11
+
12
+ private
13
+
14
+ def stormpath_routes
15
+ File.read(routes_file_path)
16
+ end
17
+
18
+ def routes_file_path
19
+ File.expand_path(find_in_source_paths('routes.rb'))
20
+ end
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,5 @@
1
+ resource :session, controller: 'stormpath/sessions', only: [:create]
2
+ resource :users, controller: 'stormpath/users', only: :create
3
+ get '/register' => 'stormpath/users#new', as: 'sign_up'
4
+ get '/login' => 'stormpath/sessions#new', as: 'sign_in'
5
+ delete '/sign_out' => 'stormpath/sessions#destroy', as: 'sign_out'
@@ -0,0 +1,11 @@
1
+ Description:
2
+ Override the default stormpath views. This generator will copy all off the
3
+ base stormpath views into your project.
4
+
5
+ Examples:
6
+ rails generate stormpath:views
7
+
8
+ View: app/views/sessions/_form.html.erb
9
+ View: app/views/sessions/new.html.erb
10
+ View: app/views/users/_form.html.erb
11
+ View: app/views/users/new.html.erb
@@ -0,0 +1,29 @@
1
+ require 'rails/generators/base'
2
+
3
+ module Stormpath
4
+ module Generators
5
+ class ViewsGenerator < ::Rails::Generators::Base
6
+ source_root File.expand_path('../../../../..', __FILE__)
7
+
8
+ def create_views
9
+ views.each do |view|
10
+ copy_file view
11
+ end
12
+ end
13
+
14
+ private
15
+
16
+ def views
17
+ files_within_root('.', 'app/views/**/*.*')
18
+ end
19
+
20
+ def files_within_root(prefix, glob)
21
+ root = "#{self.class.source_root}/#{prefix}"
22
+
23
+ Dir["#{root}/#{glob}"].sort.map do |full_path|
24
+ full_path.sub(root, '.').gsub('/./', '/')
25
+ end
26
+ end
27
+ end
28
+ end
29
+ end
@@ -1,120 +1,6 @@
1
- require 'active_support/concern'
2
- require "stormpath-sdk"
3
-
4
1
  module Stormpath
5
2
  module Rails
6
- module Account
7
- extend ActiveSupport::Concern
8
-
9
- STORMPATH_FIELDS = [ :email, :password, :username, :given_name, :middle_name, :surname, :status ]
10
-
11
- module ClassMethods
12
- def authenticate username, password
13
- account = Stormpath::Rails::Client.authenticate_account username, password
14
- self.where(stormpath_url: account.href).first
15
- end
16
-
17
- def send_password_reset_email email
18
- account = Stormpath::Rails::Client.send_password_reset_email email
19
- self.where(stormpath_url: account.href).first
20
- end
21
-
22
- def verify_password_reset_token token
23
- account = Stormpath::Rails::Client.verify_password_reset_token token
24
- self.where(stormpath_url: account.href).first
25
- end
26
-
27
- def verify_account_email token
28
- account = Stormpath::Rails::Client.verify_account_email token
29
- self.where(stormpath_url: account.href).first
30
- end
31
- end
32
-
33
- included do
34
- #AR specific workaround
35
- self.partial_updates = false if self.respond_to?(:partial_updates)
36
-
37
- #Mongoid specific declaration
38
- field(:stormpath_url, type: String) if self.respond_to?(:field)
39
- index({ stormpath_url: 1 }, { unique: true }) if self.respond_to?(:index)
40
-
41
- attr_accessor(*STORMPATH_FIELDS)
42
- attr_accessible(*STORMPATH_FIELDS)
43
-
44
- before_create :create_account_on_stormpath
45
- before_update :update_account_on_stormpath
46
- after_destroy :delete_account_on_stormpath
47
-
48
- def stormpath_account
49
- if stormpath_url
50
- @stormpath_account ||= begin
51
- Stormpath::Rails::Client.find_account(stormpath_url)
52
- rescue Stormpath::Error => error
53
- Stormpath::Rails.logger.warn "Error loading Stormpath account (#{error})"
54
- end
55
- end
56
- end
57
-
58
- def stormpath_pre_create_attrs
59
- @stormpath_pre_create_attrs ||= {}
60
- end
61
-
62
- (STORMPATH_FIELDS - [:password]).each do |name|
63
- define_method(name) do
64
- if stormpath_account.present?
65
- stormpath_account.send(name)
66
- else
67
- stormpath_pre_create_attrs[name]
68
- end
69
- end
70
- end
71
-
72
- STORMPATH_FIELDS.each do |name|
73
- define_method("#{name}=") do |val|
74
- if stormpath_account.present?
75
- stormpath_account.send("#{name}=", val)
76
- else
77
- stormpath_pre_create_attrs[name] = val
78
- end
79
- end
80
- end
81
-
82
- def create_account_on_stormpath
83
- begin
84
- @stormpath_account = Stormpath::Rails::Client.create_account! stormpath_pre_create_attrs
85
- stormpath_pre_create_attrs.clear
86
- self.stormpath_url = @stormpath_account.href
87
- rescue Stormpath::Error => error
88
- self.errors[:base] << error.to_s
89
- false
90
- end
91
- end
92
-
93
- def update_account_on_stormpath
94
- if self.stormpath_url.present?
95
- begin
96
- stormpath_account.save
97
- rescue Stormpath::Error => error
98
- self.errors[:base] << error.to_s
99
- false
100
- end
101
- else
102
- true
103
- end
104
- end
105
-
106
- def delete_account_on_stormpath
107
- if self.stormpath_url.present?
108
- begin
109
- stormpath_account.delete
110
- rescue Stormpath::Error => error
111
- Stormpath::Rails.logger.warn "Error destroying Stormpath account (#{error})"
112
- end
113
- else
114
- true
115
- end
116
- end
117
- end
3
+ class Account
118
4
  end
119
5
  end
120
- end
6
+ end
@@ -0,0 +1,28 @@
1
+ module Stormpath
2
+ module Rails
3
+ class AccountStatus
4
+ attr_accessor :response
5
+
6
+ def initialize(response)
7
+ @response = response
8
+ end
9
+
10
+ def success?
11
+ @response.instance_of? Stormpath::Resource::Account
12
+ end
13
+
14
+ def error_message
15
+ if @response.instance_of? String
16
+ return @response
17
+ else
18
+ ''
19
+ end
20
+ end
21
+
22
+ def account_url
23
+ return '' unless success?
24
+ @response.href.split('/').last
25
+ end
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,72 @@
1
+ module Stormpath
2
+ module Rails
3
+ module Authentication
4
+ private
5
+
6
+ def create_stormpath_account(user)
7
+ Client.create_stormpath_account(user)
8
+ end
9
+
10
+ def authenticate(user)
11
+ Client.authenticate(user)
12
+ end
13
+
14
+ def reset_password(email)
15
+ Client.reset_password(email)
16
+ end
17
+
18
+ def verify_email_token(token)
19
+ Client.verify_email_token(token)
20
+ end
21
+
22
+ def create_omniauth_user(provider, access_token)
23
+ Client.create_omniauth_user(provider, access_token)
24
+ end
25
+
26
+ def find_or_create_user_from_account(account)
27
+ user = find_user_by_email(account.email)
28
+ return user if user
29
+
30
+ create_user_from_account(account)
31
+ end
32
+
33
+ def create_user_from_account(account)
34
+ user = ::User.new
35
+ user.email = account.email
36
+ user.given_name = account.given_name
37
+ user.surname = account.surname
38
+ user.save
39
+
40
+ user
41
+ end
42
+
43
+ def update_password(password, account)
44
+ Client.update_password(password, account)
45
+ end
46
+
47
+ def id_site_login_url
48
+ Client.id_site_url callback_uri: (request.base_url + configuration.id_site.uri)
49
+ end
50
+
51
+ def id_site_register_url
52
+ Client.id_site_url callback_uri: (request.base_url + configuration.id_site.uri), path: '/#register'
53
+ end
54
+
55
+ def handle_id_site_callback(url)
56
+ Client.handle_id_site_callback(url)
57
+ end
58
+
59
+ def configuration
60
+ Stormpath::Rails.config
61
+ end
62
+
63
+ def find_user_by_email(email)
64
+ configuration.user_model.find_user email
65
+ end
66
+
67
+ def find_user_by_id(id)
68
+ configuration.user_model.find(id)
69
+ end
70
+ end
71
+ end
72
+ end
@@ -0,0 +1,22 @@
1
+ module Stormpath
2
+ module Rails
3
+ class AuthenticationStatus
4
+
5
+ def initialize(response)
6
+ @response = response
7
+ end
8
+
9
+ def success?
10
+ @response.instance_of? Stormpath::Authentication::AuthenticationResult
11
+ end
12
+
13
+ def error_message
14
+ if @response.instance_of? String
15
+ return @response
16
+ else
17
+ ''
18
+ end
19
+ end
20
+ end
21
+ end
22
+ end
@@ -1,99 +1,112 @@
1
- require "stormpath-sdk"
2
-
3
1
  module Stormpath
4
2
  module Rails
5
- class ConfigurationError < StandardError; end
6
-
7
3
  class Client
8
4
  class << self
9
- attr_accessor :connection, :root_application
5
+ attr_accessor :connection
10
6
  end
11
7
 
12
- def self.authenticate_account(username, password)
13
- auth_result = application.authenticate_account(
14
- Stormpath::Authentication::UsernamePasswordRequest.new(username, password)
15
- )
16
- auth_result.account
17
- end
8
+ def self.create_stormpath_account(user)
9
+ begin
10
+ result = application.accounts.create build_account(user)
11
+ rescue Stormpath::Error => error
12
+ result = error.message
13
+ end
18
14
 
19
- def self.send_password_reset_email(email)
20
- application.send_password_reset_email email
15
+ AccountStatus.new(result)
21
16
  end
22
17
 
23
- def self.verify_password_reset_token(token)
24
- application.verify_password_reset_token token
25
- end
18
+ def self.authenticate(user)
19
+ begin
20
+ result = application.authenticate_account build_username_password_request(user)
21
+ rescue Stormpath::Error => error
22
+ result = error.message
23
+ end
26
24
 
27
- def self.verify_account_email(token)
28
- self.client.accounts.verify_email_token token
25
+ AuthenticationStatus.new(result)
29
26
  end
30
27
 
31
- def self.create_account!(attributes)
32
- self.application.accounts.create attributes
28
+ def self.reset_password(email)
29
+ begin
30
+ result = application.send_password_reset_email email
31
+ rescue Stormpath::Error => error
32
+ result = error.message
33
+ end
34
+
35
+ AccountStatus.new(result)
33
36
  end
34
37
 
35
- def self.all_accounts
36
- self.application.accounts
38
+ def self.verify_email_token(token)
39
+ begin
40
+ result = client.accounts.verify_email_token token
41
+ rescue Stormpath::Error => error
42
+ result = error.message
43
+ end
44
+
45
+ AccountStatus.new(result)
37
46
  end
38
47
 
39
- def self.find_account(href)
40
- self.client.accounts.get href
48
+ def self.handle_id_site_callback(url)
49
+ response = application.handle_id_site_callback(url)
50
+ client.accounts.get response.account_href
41
51
  end
42
52
 
43
- def self.update_account!(href, attributes)
44
- account = self.find_account href
45
- attributes.each { |field, value| account.send("#{field}=", value) }
46
- account.save
53
+ def self.id_site_url(options)
54
+ application.create_id_site_url callback_uri: options[:callback_uri], path: options[:path]
47
55
  end
48
56
 
49
- def self.delete_account!(href)
50
- self.client.accounts.get(href).delete
57
+ def self.account_params(user)
58
+ account_params = user.attributes.select do |k, v|
59
+ %W[given_name surname email username password].include?(k) && !v.nil?
60
+ end
61
+
62
+ account_params.merge!("password" => user.password) unless user.password.blank?
51
63
  end
52
64
 
53
- def self.ds
54
- self.client.data_store
65
+ def self.update_password(account, password)
66
+ begin
67
+ account = client.accounts.get account
68
+ account.password = password
69
+ result = account.save
70
+ rescue Stormpath::Error => error
71
+ result = error.message
72
+ end
73
+
74
+ AccountStatus.new(result)
55
75
  end
76
+
77
+ def self.create_omniauth_user(provider, access_token)
78
+ request = Stormpath::Provider::AccountRequest.new(provider, :access_token, access_token)
79
+ application.get_provider_account(request)
80
+ end
56
81
 
57
82
  def self.application
58
- self.root_application ||= self.client.applications.get ENV["STORMPATH_APPLICATION_URL"]
83
+ self.client.applications.get Stormpath::Rails.config.application.href
59
84
  end
60
85
 
61
86
  def self.client
62
- unless self.connection
63
- if ENV['STORMPATH_URL'].nil? && ENV['STORMPATH_APPLICATION_URL'].nil?
64
- raise ConfigurationError, 'Either STORMPATH_URL or STORMPATH_APPLICATION_URL must be set'
65
- end
66
-
67
- composite_url = ENV['STORMPATH_URL']
68
-
69
- if composite_url
70
- self.root_application = Stormpath::Resource::Application.load composite_url
71
- self.connection = self.root_application.client
72
- else
73
- self.connection = Stormpath::Client.new client_options
74
- end
75
- end
76
-
77
- self.connection
87
+ self.connection ||= Stormpath::Client.new(client_options)
78
88
  end
79
89
 
80
90
  def self.client_options
81
- Hash.new.tap do |o|
82
- set_if_not_empty(o, "api_key_file_location", ENV["STORMPATH_API_KEY_FILE_LOCATION"])
83
- set_if_not_empty(o, "api_key_id_property_name", ENV["STORMPATH_API_KEY_ID_PROPERTY_NAME"])
84
- set_if_not_empty(o, "api_key_secret_property_name", ENV["STORMPATH_API_KEY_SECRET_PROPERTY_NAME"])
85
-
86
- o[:api_key] = {
87
- id: ENV["STORMPATH_API_KEY_ID"],
88
- secret: ENV["STORMPATH_API_KEY_SECRET"]
89
- } unless ENV["STORMPATH_API_KEY_ID"].blank? or ENV["STORMPATH_API_KEY_SECRET"].blank?
91
+ if Stormpath::Rails.config.api_key.file_location_provided?
92
+ Hash.new.tap { |options| options[:api_key_file_location] = Stormpath::Rails.config.api_key.file }
93
+ else
94
+ Hash.new.tap do |options|
95
+ options[:api_key] = {}
96
+ options[:api_key][:id] = Stormpath::Rails.config.api_key.id
97
+ options[:api_key][:secret] = Stormpath::Rails.config.api_key.secret
98
+ end
90
99
  end
91
100
  end
92
101
 
93
102
  private
94
103
 
95
- def self.set_if_not_empty(object, property, value)
96
- object[property.to_sym] = value unless value.blank?
104
+ def self.build_username_password_request(user)
105
+ Stormpath::Authentication::UsernamePasswordRequest.new user.email, user.password
106
+ end
107
+
108
+ def self.build_account(user)
109
+ Stormpath::Resource::Account.new account_params(user)
97
110
  end
98
111
  end
99
112
  end