fog-brightbox 0.6.1 → 0.7.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.gitignore +3 -0
- data/.rubocop.yml +20 -0
- data/.rubocop_todo.yml +81 -0
- data/CHANGELOG.md +17 -0
- data/README.md +6 -0
- data/fog-brightbox.gemspec +10 -9
- data/gemfiles/Gemfile.1.8.7 +1 -0
- data/lib/fog/brightbox.rb +2 -2
- data/lib/fog/brightbox/compute.rb +47 -48
- data/lib/fog/brightbox/compute/resource_locking.rb +26 -20
- data/lib/fog/brightbox/compute/shared.rb +2 -7
- data/lib/fog/brightbox/config.rb +12 -15
- data/lib/fog/brightbox/core.rb +5 -5
- data/lib/fog/brightbox/link_helper.rb +33 -0
- data/lib/fog/brightbox/models/compute/account.rb +0 -1
- data/lib/fog/brightbox/models/compute/accounts.rb +2 -6
- data/lib/fog/brightbox/models/compute/api_client.rb +2 -2
- data/lib/fog/brightbox/models/compute/application.rb +2 -2
- data/lib/fog/brightbox/models/compute/applications.rb +2 -6
- data/lib/fog/brightbox/models/compute/cloud_ip.rb +0 -4
- data/lib/fog/brightbox/models/compute/cloud_ips.rb +2 -6
- data/lib/fog/brightbox/models/compute/collaboration.rb +4 -4
- data/lib/fog/brightbox/models/compute/database_server.rb +11 -4
- data/lib/fog/brightbox/models/compute/database_servers.rb +2 -6
- data/lib/fog/brightbox/models/compute/database_snapshot.rb +1 -1
- data/lib/fog/brightbox/models/compute/database_snapshots.rb +2 -2
- data/lib/fog/brightbox/models/compute/database_types.rb +2 -4
- data/lib/fog/brightbox/models/compute/event.rb +0 -2
- data/lib/fog/brightbox/models/compute/events.rb +2 -4
- data/lib/fog/brightbox/models/compute/firewall_policies.rb +2 -6
- data/lib/fog/brightbox/models/compute/firewall_policy.rb +2 -3
- data/lib/fog/brightbox/models/compute/firewall_rule.rb +2 -3
- data/lib/fog/brightbox/models/compute/firewall_rules.rb +2 -6
- data/lib/fog/brightbox/models/compute/flavor.rb +0 -1
- data/lib/fog/brightbox/models/compute/flavors.rb +2 -6
- data/lib/fog/brightbox/models/compute/image.rb +3 -3
- data/lib/fog/brightbox/models/compute/images.rb +2 -6
- data/lib/fog/brightbox/models/compute/load_balancer.rb +5 -5
- data/lib/fog/brightbox/models/compute/load_balancers.rb +2 -6
- data/lib/fog/brightbox/models/compute/server.rb +15 -9
- data/lib/fog/brightbox/models/compute/server_group.rb +1 -1
- data/lib/fog/brightbox/models/compute/server_groups.rb +2 -6
- data/lib/fog/brightbox/models/compute/servers.rb +2 -6
- data/lib/fog/brightbox/models/compute/user.rb +0 -1
- data/lib/fog/brightbox/models/compute/user_collaboration.rb +2 -3
- data/lib/fog/brightbox/models/compute/users.rb +2 -5
- data/lib/fog/brightbox/models/compute/zones.rb +2 -6
- data/lib/fog/brightbox/models/storage/directories.rb +6 -13
- data/lib/fog/brightbox/models/storage/directory.rb +5 -7
- data/lib/fog/brightbox/models/storage/file.rb +37 -45
- data/lib/fog/brightbox/models/storage/files.rb +21 -25
- data/lib/fog/brightbox/oauth2.rb +136 -137
- data/lib/fog/brightbox/requests/compute/accept_user_collaboration.rb +0 -1
- data/lib/fog/brightbox/requests/compute/activate_console_server.rb +0 -1
- data/lib/fog/brightbox/requests/compute/add_listeners_load_balancer.rb +0 -1
- data/lib/fog/brightbox/requests/compute/add_nodes_load_balancer.rb +0 -1
- data/lib/fog/brightbox/requests/compute/add_servers_server_group.rb +0 -1
- data/lib/fog/brightbox/requests/compute/apply_to_firewall_policy.rb +0 -1
- data/lib/fog/brightbox/requests/compute/create_api_client.rb +0 -1
- data/lib/fog/brightbox/requests/compute/create_application.rb +0 -1
- data/lib/fog/brightbox/requests/compute/create_cloud_ip.rb +0 -1
- data/lib/fog/brightbox/requests/compute/create_collaboration.rb +0 -1
- data/lib/fog/brightbox/requests/compute/create_database_server.rb +0 -1
- data/lib/fog/brightbox/requests/compute/create_firewall_policy.rb +0 -1
- data/lib/fog/brightbox/requests/compute/create_firewall_rule.rb +0 -1
- data/lib/fog/brightbox/requests/compute/create_image.rb +0 -1
- data/lib/fog/brightbox/requests/compute/create_load_balancer.rb +0 -1
- data/lib/fog/brightbox/requests/compute/create_server.rb +0 -1
- data/lib/fog/brightbox/requests/compute/create_server_group.rb +0 -1
- data/lib/fog/brightbox/requests/compute/get_account.rb +0 -1
- data/lib/fog/brightbox/requests/compute/get_api_client.rb +0 -1
- data/lib/fog/brightbox/requests/compute/get_application.rb +0 -1
- data/lib/fog/brightbox/requests/compute/get_authenticated_user.rb +0 -2
- data/lib/fog/brightbox/requests/compute/get_cloud_ip.rb +0 -1
- data/lib/fog/brightbox/requests/compute/get_collaboration.rb +0 -1
- data/lib/fog/brightbox/requests/compute/get_database_server.rb +0 -1
- data/lib/fog/brightbox/requests/compute/get_database_snapshot.rb +0 -1
- data/lib/fog/brightbox/requests/compute/get_database_type.rb +0 -1
- data/lib/fog/brightbox/requests/compute/get_firewall_policy.rb +0 -1
- data/lib/fog/brightbox/requests/compute/get_firewall_rule.rb +0 -1
- data/lib/fog/brightbox/requests/compute/get_image.rb +0 -1
- data/lib/fog/brightbox/requests/compute/get_interface.rb +0 -1
- data/lib/fog/brightbox/requests/compute/get_load_balancer.rb +0 -1
- data/lib/fog/brightbox/requests/compute/get_scoped_account.rb +0 -1
- data/lib/fog/brightbox/requests/compute/get_server.rb +0 -1
- data/lib/fog/brightbox/requests/compute/get_server_group.rb +0 -1
- data/lib/fog/brightbox/requests/compute/get_server_type.rb +0 -1
- data/lib/fog/brightbox/requests/compute/get_user.rb +0 -1
- data/lib/fog/brightbox/requests/compute/get_user_collaboration.rb +0 -1
- data/lib/fog/brightbox/requests/compute/get_zone.rb +0 -1
- data/lib/fog/brightbox/requests/compute/list_accounts.rb +0 -1
- data/lib/fog/brightbox/requests/compute/list_api_clients.rb +0 -1
- data/lib/fog/brightbox/requests/compute/list_applications.rb +0 -1
- data/lib/fog/brightbox/requests/compute/list_cloud_ips.rb +0 -1
- data/lib/fog/brightbox/requests/compute/list_collaborations.rb +0 -1
- data/lib/fog/brightbox/requests/compute/list_database_servers.rb +0 -1
- data/lib/fog/brightbox/requests/compute/list_database_snapshots.rb +0 -1
- data/lib/fog/brightbox/requests/compute/list_database_types.rb +0 -1
- data/lib/fog/brightbox/requests/compute/list_events.rb +0 -1
- data/lib/fog/brightbox/requests/compute/list_firewall_policies.rb +0 -1
- data/lib/fog/brightbox/requests/compute/list_images.rb +0 -1
- data/lib/fog/brightbox/requests/compute/list_load_balancers.rb +0 -1
- data/lib/fog/brightbox/requests/compute/list_server_groups.rb +0 -1
- data/lib/fog/brightbox/requests/compute/list_server_types.rb +0 -1
- data/lib/fog/brightbox/requests/compute/list_servers.rb +0 -1
- data/lib/fog/brightbox/requests/compute/list_user_collaborations.rb +0 -1
- data/lib/fog/brightbox/requests/compute/list_users.rb +0 -1
- data/lib/fog/brightbox/requests/compute/list_zones.rb +0 -1
- data/lib/fog/brightbox/requests/compute/lock_resource_database_server.rb +0 -1
- data/lib/fog/brightbox/requests/compute/lock_resource_database_snapshot.rb +0 -1
- data/lib/fog/brightbox/requests/compute/lock_resource_image.rb +0 -1
- data/lib/fog/brightbox/requests/compute/lock_resource_load_balancer.rb +0 -1
- data/lib/fog/brightbox/requests/compute/lock_resource_server.rb +0 -1
- data/lib/fog/brightbox/requests/compute/map_cloud_ip.rb +0 -1
- data/lib/fog/brightbox/requests/compute/move_servers_server_group.rb +0 -1
- data/lib/fog/brightbox/requests/compute/reboot_server.rb +0 -1
- data/lib/fog/brightbox/requests/compute/reject_user_collaboration.rb +0 -1
- data/lib/fog/brightbox/requests/compute/remove_firewall_policy.rb +0 -1
- data/lib/fog/brightbox/requests/compute/remove_listeners_load_balancer.rb +0 -1
- data/lib/fog/brightbox/requests/compute/remove_nodes_load_balancer.rb +0 -1
- data/lib/fog/brightbox/requests/compute/remove_servers_server_group.rb +0 -1
- data/lib/fog/brightbox/requests/compute/resend_collaboration.rb +0 -1
- data/lib/fog/brightbox/requests/compute/reset_ftp_password_account.rb +0 -2
- data/lib/fog/brightbox/requests/compute/reset_ftp_password_scoped_account.rb +0 -1
- data/lib/fog/brightbox/requests/compute/reset_password_database_server.rb +0 -1
- data/lib/fog/brightbox/requests/compute/reset_secret_api_client.rb +0 -1
- data/lib/fog/brightbox/requests/compute/reset_secret_application.rb +0 -1
- data/lib/fog/brightbox/requests/compute/reset_server.rb +0 -1
- data/lib/fog/brightbox/requests/compute/shutdown_server.rb +0 -1
- data/lib/fog/brightbox/requests/compute/snapshot_database_server.rb +19 -3
- data/lib/fog/brightbox/requests/compute/snapshot_server.rb +19 -3
- data/lib/fog/brightbox/requests/compute/start_server.rb +0 -1
- data/lib/fog/brightbox/requests/compute/stop_server.rb +0 -1
- data/lib/fog/brightbox/requests/compute/unlock_resource_database_server.rb +0 -1
- data/lib/fog/brightbox/requests/compute/unlock_resource_database_snapshot.rb +0 -1
- data/lib/fog/brightbox/requests/compute/unlock_resource_image.rb +0 -1
- data/lib/fog/brightbox/requests/compute/unlock_resource_load_balancer.rb +0 -1
- data/lib/fog/brightbox/requests/compute/unlock_resource_server.rb +0 -1
- data/lib/fog/brightbox/requests/compute/unmap_cloud_ip.rb +0 -1
- data/lib/fog/brightbox/requests/compute/update_account.rb +0 -1
- data/lib/fog/brightbox/requests/compute/update_api_client.rb +0 -1
- data/lib/fog/brightbox/requests/compute/update_application.rb +0 -1
- data/lib/fog/brightbox/requests/compute/update_cloud_ip.rb +0 -1
- data/lib/fog/brightbox/requests/compute/update_database_server.rb +0 -1
- data/lib/fog/brightbox/requests/compute/update_database_snapshot.rb +0 -1
- data/lib/fog/brightbox/requests/compute/update_firewall_policy.rb +0 -1
- data/lib/fog/brightbox/requests/compute/update_firewall_rule.rb +0 -1
- data/lib/fog/brightbox/requests/compute/update_image.rb +0 -1
- data/lib/fog/brightbox/requests/compute/update_load_balancer.rb +0 -1
- data/lib/fog/brightbox/requests/compute/update_scoped_account.rb +0 -1
- data/lib/fog/brightbox/requests/compute/update_server.rb +0 -1
- data/lib/fog/brightbox/requests/compute/update_server_group.rb +0 -1
- data/lib/fog/brightbox/requests/compute/update_user.rb +0 -1
- data/lib/fog/brightbox/requests/storage/copy_object.rb +8 -10
- data/lib/fog/brightbox/requests/storage/delete_container.rb +1 -3
- data/lib/fog/brightbox/requests/storage/delete_multiple_objects.rb +7 -9
- data/lib/fog/brightbox/requests/storage/delete_object.rb +1 -3
- data/lib/fog/brightbox/requests/storage/delete_static_large_object.rb +7 -9
- data/lib/fog/brightbox/requests/storage/get_container.rb +3 -5
- data/lib/fog/brightbox/requests/storage/get_containers.rb +4 -6
- data/lib/fog/brightbox/requests/storage/get_object.rb +1 -3
- data/lib/fog/brightbox/requests/storage/get_object_https_url.rb +5 -10
- data/lib/fog/brightbox/requests/storage/head_container.rb +2 -4
- data/lib/fog/brightbox/requests/storage/head_containers.rb +3 -5
- data/lib/fog/brightbox/requests/storage/head_object.rb +4 -6
- data/lib/fog/brightbox/requests/storage/post_set_meta_temp_url_key.rb +2 -6
- data/lib/fog/brightbox/requests/storage/put_container.rb +3 -5
- data/lib/fog/brightbox/requests/storage/put_dynamic_obj_manifest.rb +2 -4
- data/lib/fog/brightbox/requests/storage/put_object.rb +1 -2
- data/lib/fog/brightbox/requests/storage/put_object_manifest.rb +0 -2
- data/lib/fog/brightbox/requests/storage/put_static_obj_manifest.rb +2 -4
- data/lib/fog/brightbox/storage.rb +26 -29
- data/lib/fog/brightbox/storage/connection.rb +10 -10
- data/lib/fog/brightbox/version.rb +1 -1
- data/spec/fog/brightbox/link_helper_spec.rb +18 -0
- data/spec/fog/brightbox/storage/authentication_request_spec.rb +16 -16
- data/spec/fog/brightbox/storage/connection_spec.rb +12 -12
- data/spec/fog/compute/brightbox/database_server_spec.rb +31 -0
- data/spec/fog/compute/brightbox/server_spec.rb +31 -0
- data/spec/fog/storage/brightbox_spec.rb +3 -3
- data/spec/spec_helper.rb +1 -1
- metadata +28 -10
- data/spec/model_helper.rb +0 -22
@@ -1,12 +1,10 @@
|
|
1
|
-
require
|
2
|
-
require
|
1
|
+
require "fog/core/collection"
|
2
|
+
require "fog/brightbox/models/storage/file"
|
3
3
|
|
4
4
|
module Fog
|
5
5
|
module Storage
|
6
6
|
class Brightbox
|
7
|
-
|
8
7
|
class Files < Fog::Collection
|
9
|
-
|
10
8
|
attribute :directory
|
11
9
|
attribute :limit
|
12
10
|
attribute :marker
|
@@ -18,10 +16,10 @@ module Fog
|
|
18
16
|
def all(options = {})
|
19
17
|
requires :directory
|
20
18
|
options = {
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
19
|
+
"limit" => limit,
|
20
|
+
"marker" => marker,
|
21
|
+
"path" => path,
|
22
|
+
"prefix" => prefix
|
25
23
|
}.merge!(options)
|
26
24
|
merge_attributes(options)
|
27
25
|
parent = directory.collection.get(
|
@@ -29,23 +27,23 @@ module Fog
|
|
29
27
|
options
|
30
28
|
)
|
31
29
|
if parent
|
32
|
-
load(parent.files.map {|file| file.attributes})
|
30
|
+
load(parent.files.map { |file| file.attributes })
|
33
31
|
else
|
34
32
|
nil
|
35
33
|
end
|
36
34
|
end
|
37
35
|
|
38
|
-
|
36
|
+
alias_method :each_file_this_page, :each
|
39
37
|
def each
|
40
38
|
if !block_given?
|
41
39
|
self
|
42
40
|
else
|
43
41
|
subset = dup.all
|
44
42
|
|
45
|
-
subset.each_file_this_page {|f| yield f}
|
46
|
-
while subset.length == (subset.limit ||
|
43
|
+
subset.each_file_this_page { |f| yield f }
|
44
|
+
while subset.length == (subset.limit || 10_000)
|
47
45
|
subset = subset.all(:marker => subset.last.key)
|
48
|
-
subset.each_file_this_page {|f| yield f}
|
46
|
+
subset.each_file_this_page { |f| yield f }
|
49
47
|
end
|
50
48
|
|
51
49
|
self
|
@@ -55,10 +53,10 @@ module Fog
|
|
55
53
|
def get(key, &block)
|
56
54
|
requires :directory
|
57
55
|
data = service.get_object(directory.key, key, &block)
|
58
|
-
file_data = data.headers.merge(
|
59
|
-
|
60
|
-
|
61
|
-
|
56
|
+
file_data = data.headers.merge(
|
57
|
+
:body => data.body,
|
58
|
+
:key => key
|
59
|
+
)
|
62
60
|
new(file_data)
|
63
61
|
rescue Fog::Storage::Brightbox::NotFound
|
64
62
|
nil
|
@@ -66,8 +64,8 @@ module Fog
|
|
66
64
|
|
67
65
|
def get_url(key)
|
68
66
|
requires :directory
|
69
|
-
if
|
70
|
-
"#{
|
67
|
+
if directory.public_url
|
68
|
+
"#{directory.public_url}/#{Fog::Storage::Brightbox.escape(key, "/")}"
|
71
69
|
end
|
72
70
|
end
|
73
71
|
|
@@ -81,12 +79,12 @@ module Fog
|
|
81
79
|
service.get_object_https_url(directory.key, key, expires, options)
|
82
80
|
end
|
83
81
|
|
84
|
-
def head(key,
|
82
|
+
def head(key, _options = {})
|
85
83
|
requires :directory
|
86
84
|
data = service.head_object(directory.key, key)
|
87
|
-
file_data = data.headers.merge(
|
88
|
-
|
89
|
-
|
85
|
+
file_data = data.headers.merge(
|
86
|
+
:key => key
|
87
|
+
)
|
90
88
|
new(file_data)
|
91
89
|
rescue Fog::Storage::Brightbox::NotFound
|
92
90
|
nil
|
@@ -96,9 +94,7 @@ module Fog
|
|
96
94
|
requires :directory
|
97
95
|
super({ :directory => directory }.merge!(attributes))
|
98
96
|
end
|
99
|
-
|
100
97
|
end
|
101
|
-
|
102
98
|
end
|
103
99
|
end
|
104
100
|
end
|
data/lib/fog/brightbox/oauth2.rb
CHANGED
@@ -6,165 +6,164 @@
|
|
6
6
|
module Fog
|
7
7
|
module Brightbox
|
8
8
|
module OAuth2
|
9
|
-
|
10
|
-
|
11
|
-
# based on the arguments passed in
|
12
|
-
#
|
13
|
-
# @param [Fog::Core::Connection] connection
|
14
|
-
# @param [CredentialSet] credentials
|
15
|
-
#
|
16
|
-
# @return [Excon::Response]
|
17
|
-
def request_access_token(connection, credentials)
|
18
|
-
token_strategy = credentials.best_grant_strategy
|
19
|
-
|
20
|
-
header_content = "#{credentials.client_id}:#{credentials.client_secret}"
|
21
|
-
encoded_credentials = Base64.encode64(header_content).chomp
|
22
|
-
|
23
|
-
connection.request(
|
24
|
-
:path => "/token",
|
25
|
-
:expects => 200,
|
26
|
-
:headers => {
|
27
|
-
'Authorization' => "Basic #{encoded_credentials}",
|
28
|
-
'Content-Type' => 'application/json'
|
29
|
-
},
|
30
|
-
:method => 'POST',
|
31
|
-
:body => Fog::JSON.encode(token_strategy.authorization_body_data)
|
32
|
-
)
|
33
|
-
end
|
34
|
-
|
35
|
-
# Encapsulates credentials required to request access tokens from the
|
36
|
-
# Brightbox authorisation servers
|
37
|
-
#
|
38
|
-
# @todo Interface to update certain credentials (after password change)
|
39
|
-
#
|
40
|
-
class CredentialSet
|
41
|
-
attr_reader :client_id, :client_secret, :username, :password
|
42
|
-
attr_reader :access_token, :refresh_token, :expires_in
|
9
|
+
# This builds the simplest form of requesting an access token
|
10
|
+
# based on the arguments passed in
|
43
11
|
#
|
44
|
-
# @param [
|
45
|
-
# @param [
|
46
|
-
# @param [Hash] options
|
47
|
-
# @option options [String] :username
|
48
|
-
# @option options [String] :password
|
12
|
+
# @param [Fog::Core::Connection] connection
|
13
|
+
# @param [CredentialSet] credentials
|
49
14
|
#
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
15
|
+
# @return [Excon::Response]
|
16
|
+
def request_access_token(connection, credentials)
|
17
|
+
token_strategy = credentials.best_grant_strategy
|
18
|
+
|
19
|
+
header_content = "#{credentials.client_id}:#{credentials.client_secret}"
|
20
|
+
encoded_credentials = Base64.encode64(header_content).chomp
|
21
|
+
|
22
|
+
connection.request(
|
23
|
+
:path => "/token",
|
24
|
+
:expects => 200,
|
25
|
+
:headers => {
|
26
|
+
"Authorization" => "Basic #{encoded_credentials}",
|
27
|
+
"Content-Type" => "application/json"
|
28
|
+
},
|
29
|
+
:method => "POST",
|
30
|
+
:body => Fog::JSON.encode(token_strategy.authorization_body_data)
|
31
|
+
)
|
58
32
|
end
|
59
33
|
|
60
|
-
#
|
61
|
-
#
|
62
|
-
|
63
|
-
|
64
|
-
|
34
|
+
# Encapsulates credentials required to request access tokens from the
|
35
|
+
# Brightbox authorisation servers
|
36
|
+
#
|
37
|
+
# @todo Interface to update certain credentials (after password change)
|
38
|
+
#
|
39
|
+
class CredentialSet
|
40
|
+
attr_reader :client_id, :client_secret, :username, :password
|
41
|
+
attr_reader :access_token, :refresh_token, :expires_in
|
42
|
+
#
|
43
|
+
# @param [String] client_id
|
44
|
+
# @param [String] client_secret
|
45
|
+
# @param [Hash] options
|
46
|
+
# @option options [String] :username
|
47
|
+
# @option options [String] :password
|
48
|
+
#
|
49
|
+
def initialize(client_id, client_secret, options = {})
|
50
|
+
@client_id = client_id
|
51
|
+
@client_secret = client_secret
|
52
|
+
@username = options[:username]
|
53
|
+
@password = options[:password]
|
54
|
+
@access_token = options[:access_token]
|
55
|
+
@refresh_token = options[:refresh_token]
|
56
|
+
@expires_in = options[:expires_in]
|
57
|
+
end
|
65
58
|
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
59
|
+
# Returns true if user details are available
|
60
|
+
# @return [Boolean]
|
61
|
+
def user_details?
|
62
|
+
!!(@username && @password)
|
63
|
+
end
|
70
64
|
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
65
|
+
# Is an access token available for these credentials?
|
66
|
+
def access_token?
|
67
|
+
!!@access_token
|
68
|
+
end
|
69
|
+
|
70
|
+
# Is a refresh token available for these credentials?
|
71
|
+
def refresh_token?
|
72
|
+
!!@refresh_token
|
73
|
+
end
|
75
74
|
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
75
|
+
# Updates the credentials with newer tokens
|
76
|
+
def update_tokens(access_token, refresh_token = nil, expires_in = nil)
|
77
|
+
@access_token = access_token
|
78
|
+
@refresh_token = refresh_token
|
79
|
+
@expires_in = expires_in
|
80
|
+
end
|
81
|
+
|
82
|
+
# Based on available credentials returns the best strategy
|
83
|
+
#
|
84
|
+
# @todo Add a means to dictate which should or shouldn't be used
|
85
|
+
#
|
86
|
+
def best_grant_strategy
|
87
|
+
if refresh_token?
|
88
|
+
RefreshTokenStrategy.new(self)
|
89
|
+
elsif user_details?
|
90
|
+
UserCredentialsStrategy.new(self)
|
91
|
+
else
|
92
|
+
ClientCredentialsStrategy.new(self)
|
93
|
+
end
|
94
|
+
end
|
81
95
|
end
|
82
96
|
|
83
|
-
#
|
97
|
+
# This strategy class is the basis for OAuth2 grant types
|
98
|
+
#
|
99
|
+
# @abstract Need to implement {#authorization_body_data} to return a
|
100
|
+
# Hash matching the expected parameter form for the OAuth request
|
84
101
|
#
|
85
|
-
# @todo
|
102
|
+
# @todo Strategies should be able to validate if credentials are suitable
|
103
|
+
# so just client credentials cannot be used with user strategies
|
86
104
|
#
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
elsif user_details?
|
91
|
-
UserCredentialsStrategy.new(self)
|
92
|
-
else
|
93
|
-
ClientCredentialsStrategy.new(self)
|
105
|
+
class GrantTypeStrategy
|
106
|
+
def initialize(credentials)
|
107
|
+
@credentials = credentials
|
94
108
|
end
|
95
|
-
end
|
96
|
-
end
|
97
109
|
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
# Hash matching the expected parameter form for the OAuth request
|
102
|
-
#
|
103
|
-
# @todo Strategies should be able to validate if credentials are suitable
|
104
|
-
# so just client credentials cannot be used with user strategies
|
105
|
-
#
|
106
|
-
class GrantTypeStrategy
|
107
|
-
def initialize(credentials)
|
108
|
-
@credentials = credentials
|
109
|
-
end
|
110
|
-
|
111
|
-
def authorization_body_data
|
112
|
-
raise "Not implemented"
|
110
|
+
def authorization_body_data
|
111
|
+
raise "Not implemented"
|
112
|
+
end
|
113
113
|
end
|
114
|
-
end
|
115
114
|
|
116
|
-
|
117
|
-
|
118
|
-
|
119
|
-
|
120
|
-
|
121
|
-
|
122
|
-
|
123
|
-
|
124
|
-
|
125
|
-
|
115
|
+
# This implements client based authentication/authorization
|
116
|
+
# based on the existing trust relationship using the `none`
|
117
|
+
# grant type.
|
118
|
+
#
|
119
|
+
class ClientCredentialsStrategy < GrantTypeStrategy
|
120
|
+
def authorization_body_data
|
121
|
+
{
|
122
|
+
"grant_type" => "none",
|
123
|
+
"client_id" => @credentials.client_id
|
124
|
+
}
|
125
|
+
end
|
126
126
|
end
|
127
|
-
end
|
128
127
|
|
129
|
-
|
130
|
-
|
131
|
-
|
132
|
-
|
133
|
-
|
134
|
-
|
135
|
-
|
136
|
-
|
137
|
-
|
138
|
-
|
139
|
-
|
140
|
-
|
128
|
+
# This passes user details through so the returned token
|
129
|
+
# carries the privileges of the user not account limited
|
130
|
+
# by the client
|
131
|
+
#
|
132
|
+
class UserCredentialsStrategy < GrantTypeStrategy
|
133
|
+
def authorization_body_data
|
134
|
+
{
|
135
|
+
"grant_type" => "password",
|
136
|
+
"client_id" => @credentials.client_id,
|
137
|
+
"username" => @credentials.username,
|
138
|
+
"password" => @credentials.password
|
139
|
+
}
|
140
|
+
end
|
141
141
|
end
|
142
|
-
end
|
143
142
|
|
144
|
-
|
145
|
-
|
146
|
-
|
147
|
-
|
148
|
-
|
149
|
-
|
150
|
-
|
151
|
-
|
152
|
-
|
153
|
-
|
143
|
+
# This strategy attempts to use a refresh_token gained during an earlier
|
144
|
+
# request to reuse the credentials given originally
|
145
|
+
#
|
146
|
+
class RefreshTokenStrategy < GrantTypeStrategy
|
147
|
+
def authorization_body_data
|
148
|
+
{
|
149
|
+
"grant_type" => "refresh_token",
|
150
|
+
"client_id" => @credentials.client_id,
|
151
|
+
"refresh_token" => @credentials.refresh_token
|
152
|
+
}
|
153
|
+
end
|
154
154
|
end
|
155
|
-
end
|
156
155
|
|
157
|
-
|
156
|
+
private
|
158
157
|
|
159
|
-
|
160
|
-
|
161
|
-
|
162
|
-
|
163
|
-
|
164
|
-
|
165
|
-
|
166
|
-
|
158
|
+
# This updates the current credentials if passed a valid response
|
159
|
+
#
|
160
|
+
# @param [CredentialSet] credentials Credentials to update
|
161
|
+
# @param [Excon::Response] response Response object to parse value from
|
162
|
+
#
|
163
|
+
def update_credentials_from_response(credentials, response)
|
164
|
+
response_data = Fog::JSON.decode(response.body)
|
165
|
+
credentials.update_tokens(response_data["access_token"], response_data["refresh_token"], response_data["expires_in"])
|
166
|
+
end
|
167
167
|
end
|
168
168
|
end
|
169
169
|
end
|
170
|
-
end
|