stormpath-sdk 0.4.0 → 1.0.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. data/.gitignore +6 -0
  2. data/.ruby-gemset +1 -0
  3. data/.ruby-version +1 -0
  4. data/.travis.yml +27 -0
  5. data/CHANGES.md +21 -1
  6. data/Gemfile +1 -2
  7. data/README.md +457 -11
  8. data/Rakefile +15 -1
  9. data/lib/stormpath-sdk.rb +52 -33
  10. data/lib/stormpath-sdk/{resource/group_list.rb → api_key.rb} +5 -9
  11. data/lib/stormpath-sdk/auth/authentication_result.rb +3 -13
  12. data/lib/stormpath-sdk/auth/basic_authenticator.rb +5 -11
  13. data/lib/stormpath-sdk/auth/basic_login_attempt.rb +6 -8
  14. data/lib/stormpath-sdk/auth/username_password_request.rb +2 -5
  15. data/lib/stormpath-sdk/cache/cache.rb +54 -0
  16. data/lib/stormpath-sdk/cache/cache_entry.rb +33 -0
  17. data/lib/stormpath-sdk/cache/cache_manager.rb +22 -0
  18. data/lib/stormpath-sdk/cache/cache_stats.rb +35 -0
  19. data/lib/stormpath-sdk/cache/memory_store.rb +29 -0
  20. data/lib/stormpath-sdk/cache/redis_store.rb +32 -0
  21. data/lib/stormpath-sdk/client.rb +111 -0
  22. data/lib/stormpath-sdk/data_store.rb +241 -0
  23. data/lib/stormpath-sdk/{client/api_key.rb → error.rb} +16 -10
  24. data/lib/stormpath-sdk/{util → ext}/hash.rb +1 -2
  25. data/lib/stormpath-sdk/http/authc/sauthc1_signer.rb +8 -4
  26. data/lib/stormpath-sdk/http/http_client_request_executor.rb +8 -7
  27. data/lib/stormpath-sdk/http/request.rb +4 -8
  28. data/lib/stormpath-sdk/{util/request_utils.rb → http/utils.rb} +17 -38
  29. data/lib/stormpath-sdk/resource/account.rb +12 -108
  30. data/lib/stormpath-sdk/resource/application.rb +35 -171
  31. data/lib/stormpath-sdk/resource/associations.rb +97 -0
  32. data/lib/stormpath-sdk/resource/base.rb +256 -0
  33. data/lib/stormpath-sdk/resource/collection.rb +94 -0
  34. data/lib/stormpath-sdk/resource/directory.rb +11 -68
  35. data/lib/stormpath-sdk/resource/email_verification_token.rb +3 -9
  36. data/lib/stormpath-sdk/resource/error.rb +4 -38
  37. data/lib/stormpath-sdk/resource/expansion.rb +28 -0
  38. data/lib/stormpath-sdk/resource/group.rb +8 -66
  39. data/lib/stormpath-sdk/resource/group_membership.rb +4 -55
  40. data/lib/stormpath-sdk/resource/{application_list.rb → instance.rb} +7 -13
  41. data/lib/stormpath-sdk/resource/password_reset_token.rb +5 -23
  42. data/lib/stormpath-sdk/resource/status.rb +22 -28
  43. data/lib/stormpath-sdk/resource/tenant.rb +5 -52
  44. data/lib/stormpath-sdk/resource/utils.rb +43 -13
  45. data/lib/stormpath-sdk/util/assert.rb +5 -15
  46. data/lib/stormpath-sdk/version.rb +3 -3
  47. data/spec/api_key_spec.rb +19 -0
  48. data/spec/auth/basic_authenticator_spec.rb +25 -0
  49. data/spec/auth/sauthc1_signer_spec.rb +42 -0
  50. data/spec/cache/cache_entry_spec.rb +157 -0
  51. data/spec/cache/cache_spec.rb +89 -0
  52. data/spec/cache/cache_stats_spec.rb +106 -0
  53. data/spec/client_spec.rb +538 -0
  54. data/spec/data_store_spec.rb +130 -0
  55. data/spec/resource/account_spec.rb +74 -0
  56. data/spec/resource/application_spec.rb +148 -0
  57. data/spec/resource/base_spec.rb +114 -0
  58. data/spec/resource/collection_spec.rb +169 -0
  59. data/spec/resource/directory_spec.rb +30 -0
  60. data/spec/resource/expansion_spec.rb +100 -0
  61. data/spec/resource/group_spec.rb +49 -0
  62. data/spec/spec_helper.rb +135 -0
  63. data/spec/support/resource_factory.rb +48 -0
  64. data/spec/support/resource_matchers.rb +27 -0
  65. data/spec/support/test_cache_stores.rb +9 -0
  66. data/spec/support/test_request_executor.rb +11 -0
  67. data/stormpath-sdk.gemspec +14 -4
  68. data/support/api.rb +55 -0
  69. metadata +214 -44
  70. data/lib/stormpath-sdk/client/client.rb +0 -38
  71. data/lib/stormpath-sdk/client/client_application.rb +0 -38
  72. data/lib/stormpath-sdk/client/client_application_builder.rb +0 -351
  73. data/lib/stormpath-sdk/client/client_builder.rb +0 -305
  74. data/lib/stormpath-sdk/ds/data_store.rb +0 -210
  75. data/lib/stormpath-sdk/ds/resource_factory.rb +0 -37
  76. data/lib/stormpath-sdk/resource/account_list.rb +0 -32
  77. data/lib/stormpath-sdk/resource/collection_resource.rb +0 -91
  78. data/lib/stormpath-sdk/resource/directory_list.rb +0 -30
  79. data/lib/stormpath-sdk/resource/group_membership_list.rb +0 -32
  80. data/lib/stormpath-sdk/resource/instance_resource.rb +0 -28
  81. data/lib/stormpath-sdk/resource/resource.rb +0 -327
  82. data/lib/stormpath-sdk/resource/resource_error.rb +0 -47
  83. data/test/client/client.yml +0 -16
  84. data/test/client/client_application_builder_spec.rb +0 -114
  85. data/test/client/client_builder_spec.rb +0 -176
  86. data/test/client/read_spec.rb +0 -254
  87. data/test/client/write_spec.rb +0 -420
  88. data/test/resource/resource_spec.rb +0 -41
  89. data/test/resource/test_resource.rb +0 -28
@@ -13,14 +13,8 @@
13
13
  # See the License for the specific language governing permissions and
14
14
  # limitations under the License.
15
15
  #
16
- module Stormpath
17
-
18
- module Resource
19
-
20
- class EmailVerificationToken < InstanceResource
21
- end
22
-
16
+ class Stormpath::Resource::EmailVerificationToken < Stormpath::Resource::Instance
17
+ def token
18
+ href.split('/').last
23
19
  end
24
-
25
20
  end
26
-
@@ -13,45 +13,11 @@
13
13
  # See the License for the specific language governing permissions and
14
14
  # limitations under the License.
15
15
  #
16
- module Stormpath
16
+ class Stormpath::Resource::Error < Stormpath::Resource::Base
17
17
 
18
- module Resource
19
-
20
- class Error < Resource
21
-
22
- STATUS = "status"
23
- CODE = "code"
24
- MESSAGE = "message"
25
- DEV_MESSAGE = "developerMessage"
26
- MORE_INFO = "moreInfo"
27
-
28
- def initialize body
29
- super nil, body
30
- end
31
-
32
- def get_status
33
- get_property STATUS
34
- end
35
-
36
- def get_code
37
- get_property CODE
38
- end
39
-
40
- def get_message
41
- get_property MESSAGE
42
- end
43
-
44
- def get_developer_message
45
- get_property DEV_MESSAGE
46
- end
47
-
48
- def get_more_info
49
- get_property MORE_INFO
50
- end
51
-
52
- end
18
+ prop_reader :status, :code, :message, :developer_message, :more_info
53
19
 
20
+ def initialize body
21
+ super body, nil
54
22
  end
55
-
56
23
  end
57
-
@@ -0,0 +1,28 @@
1
+ class Stormpath::Resource::Expansion
2
+ attr_reader :query
3
+
4
+ def initialize *names
5
+ @query = {}
6
+ @properties = {}
7
+
8
+ names.each { |name| add_property name }
9
+ end
10
+
11
+ def add_property name, options = {}
12
+ @properties[name] = if options[:offset] || options[:limit]
13
+ pagination = []
14
+ pagination.push("offset:#{options[:offset]}") if options[:offset]
15
+ pagination.push("limit:#{options[:limit]}") if options[:limit]
16
+
17
+ "#{name}(#{pagination.join(',')})"
18
+ else
19
+ name
20
+ end
21
+ end
22
+
23
+ def to_query
24
+ if @properties.any?
25
+ { expand: @properties.values.join(',') }
26
+ end
27
+ end
28
+ end
@@ -13,75 +13,17 @@
13
13
  # See the License for the specific language governing permissions and
14
14
  # limitations under the License.
15
15
  #
16
- module Stormpath
16
+ class Stormpath::Resource::Group < Stormpath::Resource::Instance
17
+ include Stormpath::Resource::Status
17
18
 
18
- module Resource
19
+ prop_accessor :name, :description
19
20
 
20
- class Group < InstanceResource
21
+ belongs_to :tenant
22
+ belongs_to :directory
21
23
 
22
- include Status
23
-
24
- NAME = "name"
25
- DESCRIPTION = "description"
26
- STATUS = "status"
27
- TENANT = "tenant"
28
- DIRECTORY = "directory"
29
- ACCOUNTS = "accounts"
30
-
31
- def get_name
32
- get_property NAME
33
- end
34
-
35
- def set_name name
36
- set_property NAME, name
37
- end
38
-
39
- def get_description
40
- get_property DESCRIPTION
41
- end
42
-
43
- def set_description description
44
- set_property DESCRIPTION, description
45
- end
46
-
47
- def get_status
48
- value = get_property STATUS
49
-
50
- if !value.nil?
51
- value = value.upcase
52
- end
53
-
54
- value
55
- end
56
-
57
- def set_status status
58
-
59
- if get_status_hash.has_key? status
60
- set_property STATUS, get_status_hash[status]
61
- end
62
-
63
- end
64
-
65
- def get_tenant
66
- get_resource_property TENANT, Tenant
67
- end
68
-
69
- def get_directory
70
- get_resource_property DIRECTORY, Directory
71
- end
72
-
73
- def get_accounts
74
- get_resource_property ACCOUNTS, AccountList
75
- end
76
-
77
- def add_account account
78
-
79
- GroupMembership::_create account, self, data_store
80
-
81
- end
82
-
83
- end
24
+ has_many :accounts
84
25
 
26
+ def add_account account
27
+ client.group_memberships.create group: self, account: account
85
28
  end
86
-
87
29
  end
@@ -13,60 +13,9 @@
13
13
  # See the License for the specific language governing permissions and
14
14
  # limitations under the License.
15
15
  #
16
- module Stormpath
16
+ class Stormpath::Resource::GroupMembership < Stormpath::Resource::Instance
17
17
 
18
- module Resource
18
+ has_one :account
19
+ has_one :group
19
20
 
20
- class GroupMembership < Resource
21
-
22
- ACCOUNT = "account"
23
- GROUP = "group"
24
-
25
- def get_account
26
- get_resource_property ACCOUNT, Account
27
- end
28
-
29
- def get_group
30
- get_resource_property GROUP, Group
31
- end
32
-
33
- def delete
34
- data_store.delete self
35
- end
36
-
37
- #
38
- # THIS IS NOT PART OF THE STORMPATH PUBLIC API. SDK end-users should not call it - it could be removed or
39
- # changed at any time. It is publicly accessible only as an implementation technique to be used by other
40
- # resource classes.
41
- #
42
- # @param account the account to associate with the group.
43
- # @param group the group which will contain the account.
44
- # @param data_store the datastore used to create the membership
45
- # @return the created GroupMembership instance.
46
- #
47
- def self._create account, group, data_store
48
-
49
- #TODO: enable auto discovery
50
- href = "/groupMemberships"
51
-
52
- account_props = Hash.new
53
- account_props.store HREF_PROP_NAME, account.get_href
54
-
55
- group_props = Hash.new
56
- group_props.store HREF_PROP_NAME, group.get_href
57
-
58
- props = Hash.new
59
- props.store ACCOUNT, account_props
60
- props.store GROUP, group_props
61
-
62
- group_membership = data_store.instantiate GroupMembership, props
63
-
64
- data_store.create href, group_membership, GroupMembership
65
-
66
- end
67
-
68
- end
69
-
70
- end
71
-
72
- end
21
+ end
@@ -13,20 +13,14 @@
13
13
  # See the License for the specific language governing permissions and
14
14
  # limitations under the License.
15
15
  #
16
- module Stormpath
17
-
18
- module Resource
19
-
20
- class ApplicationList < CollectionResource
21
-
22
- def get_item_type
23
-
24
- Application
25
-
26
- end
16
+ class Stormpath::Resource::Instance < Stormpath::Resource::Base
17
+ def save
18
+ data_store.save self
19
+ end
27
20
 
21
+ def delete
22
+ unless new?
23
+ data_store.delete self
28
24
  end
29
-
30
25
  end
31
26
  end
32
-
@@ -13,30 +13,12 @@
13
13
  # See the License for the specific language governing permissions and
14
14
  # limitations under the License.
15
15
  #
16
- module Stormpath
16
+ class Stormpath::Resource::PasswordResetToken < Stormpath::Resource::Base
17
+ prop_reader :email, :account
17
18
 
18
- module Resource
19
-
20
- class PasswordResetToken < Resource
21
-
22
-
23
- EMAIL = "email"
24
- ACCOUNT = "account"
25
-
26
- def get_email
27
- get_property EMAIL
28
- end
29
-
30
- def set_email email
31
- set_property EMAIL, email
32
- end
33
-
34
- def get_account
35
- get_resource_property ACCOUNT, Account
36
- end
37
-
38
- end
19
+ belongs_to :account
39
20
 
21
+ def token
22
+ href.split('/').last
40
23
  end
41
-
42
24
  end
@@ -1,34 +1,28 @@
1
- #
2
- # Copyright 2012 Stormpath, Inc.
3
- #
4
- # Licensed under the Apache License, Version 2.0 (the "License");
5
- # you may not use this file except in compliance with the License.
6
- # You may obtain a copy of the License at
7
- #
8
- # http://www.apache.org/licenses/LICENSE-2.0
9
- #
10
- # Unless required by applicable law or agreed to in writing, software
11
- # distributed under the License is distributed on an "AS IS" BASIS,
12
- # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
- # See the License for the specific language governing permissions and
14
- # limitations under the License.
15
- #
16
- module Stormpath
17
-
18
- module Resource
19
-
20
- module Status
21
-
22
- ENABLED = 'ENABLED'
23
- DISABLED = 'DISABLED'
24
-
25
- def get_status_hash
26
- {ENABLED => ENABLED, DISABLED => DISABLED}
27
- end
1
+ module Stormpath::Resource::Status
28
2
 
3
+ ENABLED = 'ENABLED'
4
+ DISABLED = 'DISABLED'
5
+
6
+ def status_hash
7
+ {ENABLED => ENABLED, DISABLED => DISABLED}
8
+ end
9
+
10
+ STATUS = "status"
11
+
12
+ def status
13
+ value = get_property STATUS
14
+
15
+ if !value.nil?
16
+ value = value.upcase
29
17
  end
30
18
 
19
+ value
31
20
  end
32
21
 
33
- end
22
+ def status=(status)
23
+ if status_hash.has_key? status
24
+ set_property STATUS, status_hash[status]
25
+ end
26
+ end
34
27
 
28
+ end
@@ -13,58 +13,11 @@
13
13
  # See the License for the specific language governing permissions and
14
14
  # limitations under the License.
15
15
  #
16
- module Stormpath
16
+ class Stormpath::Resource::Tenant < Stormpath::Resource::Instance
17
17
 
18
- module Resource
18
+ prop_reader :name, :key
19
19
 
20
- class Tenant < InstanceResource
20
+ has_many :applications
21
+ has_many :directories
21
22
 
22
- NAME = "name"
23
- KEY = "key"
24
- APPLICATIONS = "applications"
25
- DIRECTORIES = "directories"
26
-
27
- def get_name
28
- get_property NAME
29
- end
30
-
31
- def get_key
32
- get_property KEY
33
- end
34
-
35
- def create_application application
36
-
37
- href = "/applications"; #TODO: enable auto discovery
38
- data_store.create href, application, Application
39
-
40
- end
41
-
42
- def get_applications
43
-
44
- get_resource_property APPLICATIONS, ApplicationList
45
-
46
- end
47
-
48
- def get_directories
49
-
50
- get_resource_property DIRECTORIES, DirectoryList
51
-
52
- end
53
-
54
- def verify_account_email token
55
-
56
- #TODO: enable auto discovery via Tenant resource (should be just /emailVerificationTokens)
57
- href = "/accounts/emailVerificationTokens/" + token
58
-
59
- token_hash = Hash.new
60
- token_hash.store HREF_PROP_NAME, href
61
-
62
- ev_token = data_store.instantiate EmailVerificationToken, token_hash
63
-
64
- #execute a POST (should clean this up / make it more obvious)
65
- data_store.save ev_token, Account
66
- end
67
-
68
- end
69
- end
70
- end
23
+ end
@@ -14,29 +14,59 @@
14
14
  # limitations under the License.
15
15
  #
16
16
  module Stormpath
17
-
18
17
  module Resource
19
-
20
18
  module Utils
21
-
22
19
  include ActiveSupport::Inflector
23
20
  include Stormpath::Util::Assert
24
21
 
25
- @@resources_hash = Hash.new
26
-
27
- def to_class_from_instance resource
28
-
29
- assert_kind_of Resource, resource, "resource argument must be instance of Stormpath::Resource::Resource"
22
+ def inspect
23
+ ''.tap do |str|
24
+ str << %Q[#<#{class_name_with_id} @properties={]
25
+ @read_lock.lock
26
+ begin
27
+ str << properties.map do |key, value|
28
+ if printable_property? key
29
+ if value.kind_of? Hash and value.has_key? Stormpath::Resource::Base::HREF_PROP_NAME
30
+ value = %Q[{ "#{Stormpath::Resource::Base::HREF_PROP_NAME}" => "#{value[Stormpath::Resource::Base::HREF_PROP_NAME]}" }]
31
+ end
32
+ %Q["#{key} => #{value}"]
33
+ end
34
+ end.compact.join(',')
35
+ ensure
36
+ @read_lock.unlock
37
+ end
38
+ str << '}>'
39
+ end
40
+ end
30
41
 
31
- if !@@resources_hash.has_key? resource.class.name
42
+ def to_s
43
+ "#<#{class_name_with_id}>"
44
+ end
32
45
 
33
- @@resources_hash[resource.class.name] = constantize resource.class.name
46
+ def to_yaml
47
+ "--- !ruby/object: #{self.class.name}\n".tap do |yaml|
48
+ @read_lock.lock
34
49
 
50
+ begin
51
+ properties_yaml = properties.each do |key, value|
52
+ if printable_property? key
53
+ " #{key}: #{value} \n"
54
+ end
55
+ end.compact.join("\n")
56
+ unless properties_yaml.empty?
57
+ yaml << " properties\n "
58
+ yaml << properties_yaml
59
+ end
60
+ ensure
61
+ @read_lock.unlock
62
+ end
35
63
  end
64
+ end
36
65
 
37
- @@resources_hash[resource.class.name]
38
-
66
+ def class_name_with_id
67
+ object_id_hex = '%x' % (self.object_id << 1)
68
+ "#{self.class.name}:0x#{object_id_hex}"
39
69
  end
40
70
  end
41
71
  end
42
- end
72
+ end