Method: Jamf::Connection::Connect#connect
- Defined in:
- lib/jamf/api/connection/connect.rb
#connect(url = nil, **params) ⇒ String Also known as: login
Connect to the both the Classic and Jamf Pro APIs
IMPORTANT: http (non-SSL, unencrypted) connections are not allowed.
The first parameter may be a URL (must be https) from which the host & port will be used, and if present, the user and password E.g.
connect 'https://myuser:[email protected]:8443'
which is the same as:
connect host: 'host.domain.edu', port: 8443, user: 'myuser', pw: 'pass'
When using a URL, other parameters below may be specified, however host: and port: parameters will be ignored, since they came from the URL, as will user: and :pw, if they are present in the URL. If the URL doesn’t contain user and pw, they can be provided via the parameters, or left to default values.
### Passwords
The pw: parameter also accepts the symbols :prompt, and :stdin
If :prompt, the user is promted on the commandline to enter the password for the :user.
If :stdin, the password is read from the first line of stdin
If :stdinX, (where X is an integer) the password is read from the Xth line of stdin.see Jamf.stdin
If omitted, and running from an interactive terminal, the user is prompted as with :prompt
### Tokens Instead of a user and password, you may specify a valid ‘token:’, which is either:
A Jamf::Connection::Token object, which can be extracted from an active Jamf::Connection via its #token method
or
A valid token string e.g. “eyJhdXR…6EKoo” from any source can also be used.
When using an existing token or token string, the username used to create the token will be read from the server. However, if you don’t also provide the users password using the pw: parameter, then the pw_fallback option will always be false.
### Default values
Any values available via JSS.config will be used if they are not provided in the parameters. See Jamf::Configuration. If there are no config values then a built-in default is used if available.
### API Clients
As of Jamf Pro 10.49, API connections can be made using “API Clients” which are assigned to various “API Roles”, as well as regular Jamf Pro accounts.
Connections made with API Client credentials are different from regular connections:
- Their expiration period can vary based on the Client definition
- The expirations are usually quite short, less than the default 30 min. session timeout
- They cannot be kept alive, they will become invalid when the expiration time arrives.
- The API endpoints and data exchange used for making API Client connections are
different from those used by normal connections.
To make a connection using an API Client, pass in the client_id: and client_secret: instead of user: and pw:
170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 |
# File 'lib/jamf/api/connection/connect.rb', line 170 def connect(url = nil, **params) raise ArgumentError, 'No url or connection parameters provided' if url.nil? && params.empty? # reset all values, flush caches disconnect # If there's a Token object in :token, this sets @token, # and adds host, port, user from that token parse_token params # Get host, port, user and pw from a URL, add to params if needed parse_url url, params # apply defaults from config, client, and then ruby-jss itself. apply_default_params params # Once we're here, all params have been parsed & defaulted into the # params hash, so make sure we have the minimum needed params for a connection verify_basic_params params # it there's no @token yet, get one from a token string or a password create_token_if_needed(params) # We have to have a usable connection to do this, so it has to come after # all the stuff above verify_server_version @timeout = params[:timeout] @open_timeout = params[:open_timeout] @connect_time = Time.now @name ||= "#{user}@#{host}:#{port}" @c_base_url = base_url + Jamf::Connection::CAPI_RSRC_BASE @jp_base_url = base_url + Jamf::Connection::JPAPI_RSRC_BASE # the faraday connection objects @c_cnx = create_classic_connection @jp_cnx = create_jp_connection # set the connection objects to sticky if desired. enforce booleans self.sticky_session = params[:sticky_session] ? true : false @connected = true to_s end |