Module: AppleDEPClient::Auth

Defined in:
lib/apple_dep_client/auth.rb

Constant Summary collapse

OAUTH_PATH =

Apple requires a quirky OAuth 1.0a authentication to get a temporary X-ADM-Auth-Session key to make requests; this takes care of that

"/session"

Class Method Summary collapse

Class Method Details

.get_session_tokenObject



16
17
18
19
20
21
22
23
24
# File 'lib/apple_dep_client/auth.rb', line 16

def self.get_session_token
  options = { method: :get, headers: AppleDEPClient::Request::DEFAULT_HEADERS }
  request = Typhoeus::Request.new(AppleDEPClient::Request.make_url(OAUTH_PATH), options)
  request.options[:headers].merge!({ "Authorization" => oauth_header(request) })
  request.run
  response = request.response
  AppleDEPClient::Error.check_request_error(response, auth: true)
  parse_response response
end

.oauth_header(request) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/apple_dep_client/auth.rb', line 26

def self.oauth_header(request)
  consumer = OAuth::Consumer.new(
    AppleDEPClient.consumer_key,
    AppleDEPClient.consumer_secret,
    site: AppleDEPClient::Request.make_url(OAUTH_PATH),
  )
  token = OAuth::AccessToken.new(
    consumer,
    AppleDEPClient.access_token,
    AppleDEPClient.access_secret,
  )
  oauth_params = {
    consumer: consumer,
    realm: "ADM",
    token: token,
  }
  oauth_helper = OAuth::Client::Helper.new request, oauth_params.merge(request_uri: AppleDEPClient::Request.make_url(OAUTH_PATH))
  oauth_helper.header
end

.parse_response(response) ⇒ Object



46
47
48
49
# File 'lib/apple_dep_client/auth.rb', line 46

def self.parse_response(response)
  body = JSON.parse response.response_body
  auth_session_token = body["auth_session_token"]
end