Class: Faria::Launchpad::Service

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

Constant Summary collapse

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.



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

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



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

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



62
63
64
65
# File 'lib/faria/launchpad/service.rb', line 62

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



68
69
70
71
# File 'lib/faria/launchpad/service.rb', line 68

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

#echo(params = {}) ⇒ Object



51
52
53
# File 'lib/faria/launchpad/service.rb', line 51

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

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



112
113
114
115
# File 'lib/faria/launchpad/service.rb', line 112

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

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

lower-level HTTP code



138
139
140
# File 'lib/faria/launchpad/service.rb', line 138

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

#import_identities(api_key, identities) ⇒ Object



83
84
85
# File 'lib/faria/launchpad/service.rb', line 83

def import_identities(api_key, identities)
  post "identities/import", {school_api_key: api_key, identities: identities}
end

#infoObject

utils requiring auth



47
48
49
# File 'lib/faria/launchpad/service.rb', line 47

def info
  get "info"
end

#pairing_complete_urlObject



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

def pairing_complete_url
  rooted_url "third/pairing/complete"
end

#pairing_request_urlObject

url helpers



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

def pairing_request_url
  rooted_url "third/pairing/request"
end

#parse_response(resp) ⇒ Object



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

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



122
123
124
125
# File 'lib/faria/launchpad/service.rb', line 122

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

#pingObject

utils



36
37
38
# File 'lib/faria/launchpad/service.rb', line 36

def ping
  get_without_auth "ping"
end

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

direct methods (for undocumented api?)



107
108
109
110
# File 'lib/faria/launchpad/service.rb', line 107

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

#provision(params = {}) ⇒ Object

final provisioning step (server side)



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

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



40
41
42
43
# File 'lib/faria/launchpad/service.rb', line 40

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

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



117
118
119
120
# File 'lib/faria/launchpad/service.rb', line 117

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

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



142
143
144
145
# File 'lib/faria/launchpad/service.rb', line 142

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

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



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

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
    else
      request['Content-Type'] = "application/jwe"
      request.body = payload
    end
    http.request request
  end
end

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

sessions



57
58
59
# File 'lib/faria/launchpad/service.rb', line 57

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

#show_identity(uuid) ⇒ Object

identities



75
76
77
# File 'lib/faria/launchpad/service.rb', line 75

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



89
90
91
# File 'lib/faria/launchpad/service.rb', line 89

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

#update_identity(identity_representation, uuid) ⇒ Object



79
80
81
# File 'lib/faria/launchpad/service.rb', line 79

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

#update_identity_by_pairing_value(identity_representation, pairing_value) ⇒ Object



93
94
95
# File 'lib/faria/launchpad/service.rb', line 93

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