class Tide::API::AuthServer

Receives an authorization grant code from the OAuth2 callback.

Constants

CLIENT_ID

This is the ID shown in the authorization page of the mobile app

RESPONSE

Complete HTTP response

Attributes

port[R]

@api private

server[R]

@api private

Public Class Methods

new() click to toggle source
# File lib/tide/api/auth_server.rb, line 18
def initialize
  @server = TCPServer.new('localhost', 0)
  @port = server.addr[1]
end

Public Instance Methods

run() click to toggle source

Runs a tiny HTTP server to receive OAuth2 callbacks.

@return [String] The authentication grant code from Tide.

# File lib/tide/api/auth_server.rb, line 27
def run
  puts 'Waiting for the authorization code...'
  request_auth_nonce

  socket = server.accept
  auth_nonce = extract_auth_nonce(socket.gets)

  puts 'Authorization code received.'

  socket.print RESPONSE
  socket.close

  auth_nonce
end

Private Instance Methods

auth_page_url() click to toggle source

@api private

# File lib/tide/api/auth_server.rb, line 55
def auth_page_url
  "https://api.tide.co/tide-backend/oauth/index.html?redirect_url=#{redirect_url}"
end
extract_auth_nonce(http_response_line) click to toggle source

@api private

# File lib/tide/api/auth_server.rb, line 60
def extract_auth_nonce(http_response_line)
  http_response_line.split('=').last.split.first
end
redirect_url() click to toggle source

@api private

# File lib/tide/api/auth_server.rb, line 50
def redirect_url
  "http://localhost:#{port}&client_id=#{CLIENT_ID}"
end
request_auth_nonce() click to toggle source

@api private

# File lib/tide/api/auth_server.rb, line 45
def request_auth_nonce
  system("open '#{auth_page_url}'")
end