20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
|
# File 'lib/talknote/cli.rb', line 20
def init
state = ('a'..'z').to_a.sample(32).join
path = '/oauth/callback'
client = OAuth2::Client.new(
options['id'],
options['secret'],
site: 'https://oauth.talknote.com',
authorize_url: '/oauth/authorize',
token_url: 'oauth/token'
)
redirect_uri = "http://#{options['host']}:#{options['port']}#{path}"
scope = [
'talknote.timeline.read',
'talknote.timeline.write',
'talknote.timeline.message.read',
'talknote.timeline.message.write',
'talknote.timeline.unread',
'talknote.group',
'talknote.group.read',
'talknote.group.write',
'talknote.group.unread',
'talknote.group.message.read',
'talknote.group.message.write',
'talknote.direct_message',
'talknote.direct_message.read',
'talknote.direct_message.write',
'talknote.direct_message.unread',
'talknote.direct_message.message.read',
'talknote.direct_message.message.write',
'talknote.user.read',
'talknote.user.write',
'talknote.allfeed.read',
'talknote.allfeed.unread'
].join(' ')
code_args = {
redirect_uri: redirect_uri,
scope: scope,
state: state
}
url = client.auth_code.authorize_url(code_args)
puts ''
puts "Authorization URL: #{url}"
puts ''
begin
if RUBY_PLATFORM =~ /darwin/ system("open '#{url}'")
puts "Opening browser automatically..."
elsif RUBY_PLATFORM =~ /linux/
system("xdg-open '#{url}'")
puts "Opening browser automatically..."
elsif RUBY_PLATFORM =~ /win32|win64|\.NET|windows|cygwin|mingw32/i
system("start '#{url}'")
puts "Opening browser automatically..."
else
puts "Please manually open the URL above in your browser."
end
rescue => e
puts "Could not open browser automatically: #{e.message}"
puts "Please manually open the URL above in your browser."
end
puts 'Starting server - use Ctrl+C to stop'
puts ''
server_options = {
Port: options['port']
}
server = WEBrick::HTTPServer.new(server_options)
server.mount_proc('/') do |req, res|
unless req.path == path
res.status = 403
res.body = "Invalid callback path - expecting #{path}"
next
end
unless req.query['state'] == state
res.status = 400
res.body = 'Invalid state in callback'
next
end
begin
token = client.auth_code.get_token(
req.query['code'],
grant_type: 'authorization_code',
redirect_uri: redirect_uri
)
pp token.to_hash
puts ''
config_path = "#{Dir.home}/.config/talknote"
FileUtils.mkdir_p(config_path) unless Dir.exist?(config_path)
File.write("#{config_path}/token.json", token.to_hash.to_json)
res.status = 200
res.body = 'You may now close this tab'
server.shutdown
rescue OAuth2::Error => e
puts "OAuth2 Error: #{e.message}"
puts "Error Code: #{e.code}" if e.respond_to?(:code)
puts "Error Description: #{e.description}" if e.respond_to?(:description)
res.status = 400
res.body = "OAuth Error: #{e.message}"
rescue => e
puts "General Error: #{e.message}"
puts e.backtrace.join("\n")
res.status = 500
res.body = "Server Error: #{e.message}"
end
end
trap('INT') do
server.shutdown
end
server.start
end
|