Skip to content

Commit db56014

Browse files
author
Renier Morales
committed
Add token/password authentication to ruby client
1 parent 449da16 commit db56014

3 files changed

Lines changed: 62 additions & 8 deletions

File tree

CHANGELOG.textile

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
*3.2*
2+
* Add password-based authentication with `SoftLayer::Client.with_password(username: '...', password: '...', ...)`.
3+
14
*3.0*
25
* Substantially rewrote the ObjectFilter class. ObjectFilters used to be hashes which made it easy to manipulate their content incorrectly. The new implementation has a strict interface that makes it harder to manipulate filters incorrectly.
36
* Added a model for Virtual Server Image Templates (SoftLayer::ImageTemplate) - VirtualServerOrder now requires an instance of this class rather than allowing you to provide the global_id of an image

lib/softlayer/Client.rb

Lines changed: 58 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,32 @@ def self.default_client=(new_default)
5858
@@default_client = new_default
5959
end
6060

61+
##
62+
# This will be using your username and password to get a portal
63+
# token with which to authenticate client calls.
64+
# This is a wrapper around Client.new. You can pass it the same
65+
# parameters as with Client.new, with the exception that this will
66+
# be expecting a password in the options hash.
67+
def self.with_password(options = {})
68+
if options[:username].nil? || options[:username].empty?
69+
raise 'A username is required to create this client'
70+
end
71+
72+
if options[:password].nil? || options[:password].empty?
73+
raise 'A password is required to create this client'
74+
end
75+
76+
service = SoftLayer::Service.new('SoftLayer_User_Customer')
77+
token = service.getPortalLoginToken(
78+
options[:username], options[:password]
79+
)
80+
81+
options[:userId] = token['userId']
82+
options[:authToken] = token['hash']
83+
84+
SoftLayer::Client.new(options)
85+
end
86+
6187
##
6288
#
6389
# Clients are built with a number of settings:
@@ -81,6 +107,10 @@ def initialize(options = {})
81107
# do a similar thing for the api key
82108
@api_key = settings[:api_key] || ""
83109

110+
# grab token pair
111+
@userId = settings[:userId]
112+
@authToken = settings[:authToken]
113+
84114
# and the endpoint url
85115
@endpoint_url = settings[:endpoint_url] || API_PUBLIC_ENDPOINT
86116

@@ -90,19 +120,40 @@ def initialize(options = {})
90120
# and assign a time out if the settings offer one
91121
@network_timeout = settings[:timeout] if settings.has_key?(:timeout)
92122

93-
raise "A SoftLayer Client requires a username" if !@username || @username.empty?
94-
raise "A SoftLayer Client requires an api_key" if !@api_key || @api_key.empty?
123+
if token_based?
124+
raise "A SoftLayer Client requires a userId" if !@userId
125+
raise "A SoftLayer Client requires an authToken" if !@authToken || @api_key.empty?
126+
else
127+
raise "A SoftLayer Client requires a username" if !@username || @username.empty?
128+
raise "A SoftLayer Client requires an api_key" if !@api_key || @api_key.empty?
129+
end
130+
95131
raise "A SoftLayer Client requires an endpoint URL" if !@endpoint_url || @endpoint_url.empty?
96132
end
97133

134+
# return whether this client is using token-based authentication
135+
def token_based?
136+
@userId && @authToken && !@authToken.empty?
137+
end
138+
98139
# return a hash of the authentication headers for the client
99140
def authentication_headers
100-
{
101-
"authenticate" => {
102-
"username" => @username,
103-
"apiKey" => @api_key
141+
if token_based?
142+
{
143+
'authenticate' => {
144+
'complexType' => 'PortalLoginToken',
145+
'userId' => @userId,
146+
'authToken' => @authToken
147+
}
104148
}
105-
}
149+
else
150+
{
151+
'authenticate' => {
152+
'username' => @username,
153+
'apiKey' => @api_key
154+
}
155+
}
156+
end
106157
end
107158

108159
# Returns a service with the given name.

lib/softlayer/base.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
module SoftLayer
1313
# The version number (including major, minor, and bugfix numbers)
1414
# This should change in accordance with the concept of Semantic Versioning
15-
VERSION = "3.1.1" # version history in the CHANGELOG.textile file at the root of the source
15+
VERSION = "3.2.0" # version history in the CHANGELOG.textile file at the root of the source
1616

1717
# The base URL of the SoftLayer API available to the public internet.
1818
API_PUBLIC_ENDPOINT = 'https://api.softlayer.com/xmlrpc/v3/'

0 commit comments

Comments
 (0)