Class: Tide::API::AuthServer

Inherits:
Object
  • Object
show all
Defined in:
lib/tide/api/auth_server.rb

Overview

Receives an authorization grant code from the OAuth2 callback.

Constant Summary collapse

CLIENT_ID =

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

'tide-api-gem'.freeze
RESPONSE =

Complete HTTP response

"HTTP/1.1 200 OK\r\n" \
"Content-Type: text/plain\r\n" \
"Content-Length: 31\r\n" \
"Connection: close\r\n" \
"\r\n" \
"You can now close this window.\n".freeze

Instance Method Summary collapse

Constructor Details

#initializeAuthServer

Returns a new instance of AuthServer.



18
19
20
21
# File 'lib/tide/api/auth_server.rb', line 18

def initialize
  @server = TCPServer.new('localhost', 0)
  @port = server.addr[1]
end

Instance Method Details

#runString

Runs a tiny HTTP server to receive OAuth2 callbacks.

Returns:

  • (String)

    The authentication grant code from Tide.



27
28
29
30
31
32
33
34
35
36
37
38
39
40
# 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