class Faria::Launchpad::Service

Constants

DEFAULTS
VALID_VERBS

Public Class Methods

new(endpoint, options = {}) click to toggle source
# 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
noauth(endpoint, quiet: false) click to toggle source
# 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

Public Instance Methods

approve_session(session_id, data = {}) click to toggle source

data is intended to be JSON encoded data if passed

# 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 = {}) click to toggle source

data is intended to be JSON encoded data if passed

# 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={}) click to toggle source
# File lib/faria/launchpad/service.rb, line 51
def echo(params={})
  put "echo", params
end
get(url, params = {}) click to toggle source
# 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={}) click to toggle source

lower-level HTTP code

# 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) click to toggle source
# 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
info() click to toggle source

utils requiring auth

# File lib/faria/launchpad/service.rb, line 47
def info
  get "info"
end
pairing_complete_url() click to toggle source
# File lib/faria/launchpad/service.rb, line 172
def pairing_complete_url
  rooted_url "third/pairing/complete"
end
pairing_request_url() click to toggle source

url helpers

# File lib/faria/launchpad/service.rb, line 168
def pairing_request_url
  rooted_url "third/pairing/request"
end
parse_response(resp) click to toggle source
# 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 = {}) click to toggle source
# File lib/faria/launchpad/service.rb, line 122
def patch(url, params = {})
  resp = raw_request(:patch, url, params)
  parse_response(resp)
end
ping() click to toggle source

utils

# File lib/faria/launchpad/service.rb, line 36
def ping
  get_without_auth "ping"
end
post(url, params = {}) click to toggle source

direct methods (for undocumented api?)

# File lib/faria/launchpad/service.rb, line 107
def post(url, params = {})
  resp = raw_request(:post, url, params)
  parse_response(resp)
end
provision(params = {}) click to toggle source

final provisioning step (server side)

# 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
pubkey() click to toggle source
# 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 = {}) click to toggle source
# 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={}) click to toggle source
# 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 = {}) click to toggle source
# 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 = {}) click to toggle source

sessions

# File lib/faria/launchpad/service.rb, line 57
def retrieve_session(session_id, params = {})
  get "authentication_sessions/#{session_id}", params
end
show_identity(uuid) click to toggle source

identities

# File lib/faria/launchpad/service.rb, line 75
def show_identity(uuid)
  get "identities/#{uuid}"
end
show_identity_by_pairing_value(pairing_value) click to toggle source

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

# 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) click to toggle source
# 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) click to toggle source
# 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

Private Instance Methods

base_url(url) click to toggle source
# File lib/faria/launchpad/service.rb, line 205
def base_url(url)
  url.gsub(%r{/api/v[^/]+/$},"")
end
encrypt_payload(params, address) click to toggle source
# File lib/faria/launchpad/service.rb, line 188
def encrypt_payload(params, address)
  Faria::Launchpad::Packet.encrypt(
    params,
    {
      api_url: address.normalize.to_s,
      source: @source,
      expires_in: @options[:expires_in]
    },
    remote_key: @remote_key,
    local_key: @my_key
  )
end
full_url(url) click to toggle source
# File lib/faria/launchpad/service.rb, line 209
def full_url(url)
  File.join(@endpoint, url)
end
rooted_url(url) click to toggle source
# File lib/faria/launchpad/service.rb, line 201
def rooted_url(url)
  File.join(base_url(@endpoint), url)
end
verb_to_http_class(verb) click to toggle source

can't guarantee we have Rails or AS so we use eval vs constantize/classify, etc

# File lib/faria/launchpad/service.rb, line 182
def verb_to_http_class(verb)
  raise "#{verb} is not a valid HTTP verb." unless VALID_VERBS.include?(verb.to_s)

  Net::HTTP.const_get(verb.to_s.capitalize)
end