Class: Faria::Launchpad::Service

Inherits:
Object
  • Object
show all
Defined in:
lib/faria/launchpad/service.rb

Constant Summary collapse

LAUNCHPAD_NAME =
"Launchpad"
DEFAULTS =
{
  expires_in: 60 # 1 minute
}

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(endpoint, options = {}) ⇒ Service

Returns a new instance of Service.



26
27
28
29
30
31
32
33
34
# File 'lib/faria/launchpad/service.rb', line 26

def initialize(endpoint, options = {})
  @endpoint = endpoint
  @my_key = options[:keys][:local]
  @remote_key = options[:keys][:remote]
  @source = options[:source]
  @app_name = options[:source][:name]

  @options = DEFAULTS.merge(options)
end

Class Method Details

.noauth(endpoint, quiet: false) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
# File 'lib/faria/launchpad/service.rb', line 10

def self.noauth(endpoint, quiet: false)
  unless quiet
    puts "************************************************************************\n" \
    "\007\007\007NOTICE: noauth is only intended as a somewhat easy way to call `ping`\n" \
      "and `pubkey`. Nothing else is going to work since keys are required for\n" \
      "general API usage.\n" \
      "************************************************************************\n"
    sleep 2
  end
  new(endpoint, keys: { local: nil, remote: nil }, source: {name: "No one"})
end

Instance Method Details

#approve_session(session_id, data = {}) ⇒ Object

data is intended to be JSON encoded data if passed



64
65
66
67
# File 'lib/faria/launchpad/service.rb', line 64

def approve_session(session_id, data = {})
  params = data.empty? ? {} : { data: data }
  post "authentication_sessions/#{session_id}/approve", params
end

#decline_session(session_id, data = {}) ⇒ Object

data is intended to be JSON encoded data if passed



70
71
72
73
# File 'lib/faria/launchpad/service.rb', line 70

def decline_session(session_id, data = {})
  params = data.empty? ? {} : { data: data }
  post "authentication_sessions/#{session_id}/decline", params
end

#echo(params = {}) ⇒ Object



53
54
55
# File 'lib/faria/launchpad/service.rb', line 53

def echo(params={})
  put "echo", params
end

#get(url, params = {}) ⇒ Object



110
111
112
113
# File 'lib/faria/launchpad/service.rb', line 110

def get(url, params = {})
  resp = raw_request(:get, url, params)
  parse_response(resp)
end

#get_without_auth(url, params = {}) ⇒ Object

lower-level HTTP code



136
137
138
# File 'lib/faria/launchpad/service.rb', line 136

def get_without_auth(url, params={})
  parse_response raw_get_without_auth(url, params)
end

#infoObject

utils requiring auth



49
50
51
# File 'lib/faria/launchpad/service.rb', line 49

def info
  get "info"
end

#pairing_complete_urlObject



173
174
175
# File 'lib/faria/launchpad/service.rb', line 173

def pairing_complete_url
  rooted_url "third/pairing/complete"
end

#pairing_request_urlObject

url helpers



169
170
171
# File 'lib/faria/launchpad/service.rb', line 169

def pairing_request_url
  rooted_url "third/pairing/request"
end

#parse_response(resp) ⇒ Object



125
126
127
128
129
130
131
132
# File 'lib/faria/launchpad/service.rb', line 125

def parse_response(resp)
  hash = JSON.parse(resp.body)
  # be railsy if we can
  hash = hash.with_indifferent_access if hash.respond_to?(:with_indifferent_access)
  hash
rescue JSON::ParserError
  raise JSON::ParserError, resp.body
end

#patch(url, params = {}) ⇒ Object



120
121
122
123
# File 'lib/faria/launchpad/service.rb', line 120

def patch(url, params = {})
  resp = raw_request(:patch, url, params)
  parse_response(resp)
end

#pingObject

utils



38
39
40
# File 'lib/faria/launchpad/service.rb', line 38

def ping
  get_without_auth "ping"
end

#post(url, params = {}) ⇒ Object

direct methods (for undocumented api?)



105
106
107
108
# File 'lib/faria/launchpad/service.rb', line 105

def post(url, params = {})
  resp = raw_request(:post, url, params)
  parse_response(resp)
end

#provision(params = {}) ⇒ Object

final provisioning step (server side)



96
97
98
99
100
101
# File 'lib/faria/launchpad/service.rb', line 96

def provision(params = {})
  raise "you need an :approval_code" if params[:approval_code].blank?
  raise "you need an :identity" if params[:identity].blank?

  post("pairing/provision", params)
end

#pubkeyObject



42
43
44
45
# File 'lib/faria/launchpad/service.rb', line 42

def pubkey
  resp = raw_get_without_auth("pubkey")
  return resp.body if resp.code == '200'
end

#put(url, params = {}) ⇒ Object



115
116
117
118
# File 'lib/faria/launchpad/service.rb', line 115

def put(url, params = {})
  resp = raw_request(:put, url, params)
  parse_response(resp)
end

#raw_get_without_auth(url, params = {}) ⇒ Object



140
141
142
143
# File 'lib/faria/launchpad/service.rb', line 140

def raw_get_without_auth(url, params={})
  uri = full_url(url)
  Net::HTTP.get_response(URI(uri))
end

#raw_request(verb, url, params = {}) ⇒ Object



145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
# File 'lib/faria/launchpad/service.rb', line 145

def raw_request(verb, url, params = {})
  uri = full_url(url)
  a = Addressable::URI.parse(uri)
  http = Net::HTTP.new(a.host, a.inferred_port)
  http.use_ssl = a.scheme == 'https'
  # http.verify_mode = OpenSSL::SSL::VERIFY_NONE
  http.start do |http|
    request = verb_to_http_class(verb).new a.request_uri
    payload = encrypt_payload(params, a)
    if verb == :get
      request['Faria-JWE'] = payload
      debug "Faria-JWE: #{payload}"
    else
      request['Content-Type'] = "application/jwe"
      debug "Content-Type: application/jwe"
      debug "POST BODY: #{payload}"
      request.body = payload
    end
    http.request request
  end
end

#retrieve_session(session_id, params = {}) ⇒ Object

sessions



59
60
61
# File 'lib/faria/launchpad/service.rb', line 59

def retrieve_session(session_id, params = {})
  get "authentication_sessions/#{session_id}", params
end

#show_identity(uuid) ⇒ Object

identities



77
78
79
# File 'lib/faria/launchpad/service.rb', line 77

def show_identity(uuid)
  get "identities/#{uuid}"
end

#show_identity_by_pairing_value(pairing_value) ⇒ Object

by_value allows the unique pairing value to be used to perform queries or updates instead of Launchpad’s internal UUID



87
88
89
# File 'lib/faria/launchpad/service.rb', line 87

def show_identity_by_pairing_value(pairing_value)
  get "identities/by_pairing_value/#{pairing_value}"
end

#update_identity(identity_representation, uuid) ⇒ Object



81
82
83
# File 'lib/faria/launchpad/service.rb', line 81

def update_identity(identity_representation, uuid)
  patch "identities/#{uuid}", identity: identity_representation
end

#update_identity_by_pairing_value(identity_representation, pairing_value) ⇒ Object



91
92
93
# File 'lib/faria/launchpad/service.rb', line 91

def update_identity_by_pairing_value(identity_representation, pairing_value)
  patch "identities/by_pairing_value/#{pairing_value}", identity: identity_representation
end