Module: ADN::Auth

Defined in:
lib/adn/auth.rb

Constant Summary collapse

TOKEN_FILE =
File.expand_path("~/.adn-cli-token")
CLIENT_ID =
'bQQrxrhNXGVNrnZ6dTs6LDHfSfUFSX9Q'
SCOPES =
['stream', 'write_post']
AUTH_URL =
"https://alpha.app.net/oauth/authenticate?client_id=#{CLIENT_ID}&response_type=token&redirect_uri=http://localhost:9229/authorize/&scope=#{SCOPES.join(',')}"
PUBLIC =
File.expand_path(File.dirname(__FILE__)) + "/../../public"

Class Method Summary collapse

Class Method Details

.has_token?Boolean

Returns:

  • (Boolean)


26
27
28
# File 'lib/adn/auth.rb', line 26

def has_token?
  File.exists?(TOKEN_FILE)
end

.launch_browser(url) ⇒ Object



40
41
42
# File 'lib/adn/auth.rb', line 40

def launch_browser(url)
  system "open \"#{url}\""
end

.retrieve_tokenObject



34
35
36
37
38
# File 'lib/adn/auth.rb', line 34

def retrieve_token
  puts "Retrieve OAuth2 token"
  launch_browser(AUTH_URL)
  server.start
end

.serverObject



44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/adn/auth.rb', line 44

def server
  @server ||= begin
    httpd = WEBrick::HTTPServer.new(
      Port: 9229,
      Logger: WEBrick::Log.new("/dev/null"),
      AccessLog: [nil, nil]
    )

    httpd.tap { |s|
      s.mount "/authorize", WEBrick::HTTPServlet::FileHandler, ADN::Auth::PUBLIC
      s.mount "/save-token", ADN::TokenSaver
    }
  end
end

.tokenObject



30
31
32
# File 'lib/adn/auth.rb', line 30

def token
  @token ||= IO.read(TOKEN_FILE)
end