sinatra_sockets 0.0.5 → 0.0.6

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 15ceae9b0ffb57cf0402226553bbf1d0cead1365
4
- data.tar.gz: 1c6991d6bff221887618e35f20946e61b7320161
3
+ metadata.gz: 4d379215d2220dba3ac97637d938c45e7695624d
4
+ data.tar.gz: c1a7fc1db29382e26218b6913be59094f72e9ddb
5
5
  SHA512:
6
- metadata.gz: 8d9bfe79ce004845abb884862540554e14ed3713b2c11fb2594ab0600138aa048b6d9500a6726ae40b12ec19652a97c12aa8d2de3df084b2aca4d833c18ca38d
7
- data.tar.gz: b5daeb4d873e9f103fbf0fb4f50173adec5e18040a6baf508fac4c86c5577aeb56107f5001ab9fa1f5ea1804924a9fcbd1e17b26e2c2f145859e4377569adcf0
6
+ metadata.gz: 7306077f348c70d269327f3bc70e5fd2b52a419ec8c0f81e9235e73257ce74d95b72f62760e7b2d1e03cfd263ace17f23572c5b45623850cd3739cfdfb40edf2
7
+ data.tar.gz: a39381a435fcea01b45610b981d05ee2b527a0db1b42e7b2914e6c7bcfc6a17569a1b21b87c017ced25dba3558bf198b5e0560e3e1158c41c73272a62d94dd78
@@ -8,73 +8,30 @@ GEM
8
8
  tzinfo (~> 1.1)
9
9
  addressable (2.5.0)
10
10
  public_suffix (~> 2.0, >= 2.0.2)
11
- awesome_print (1.7.0)
12
11
  byebug (9.0.6)
13
- coderay (1.1.1)
14
- coffee-script (2.4.1)
15
- coffee-script-source
16
- execjs
17
- coffee-script-source (1.12.2)
18
- colored (1.2)
19
12
  concurrent-ruby (1.0.4)
20
- corefines (1.9.0)
21
13
  daemons (1.2.4)
22
14
  dotenv (2.2.0)
23
- engtagger (0.2.1)
24
15
  eventmachine (1.2.2)
25
- execjs (2.7.0)
26
16
  faraday (0.11.0)
27
17
  multipart-post (>= 1.2, < 3)
28
18
  faye-websocket (0.10.6)
29
19
  eventmachine (>= 0.12.0)
30
20
  websocket-driver (>= 0.5.1)
31
- ffi (1.9.17)
32
- gemmyrb (0.1.6)
33
- activesupport
34
- awesome_print
35
- byebug
36
- colored
37
- corefines
38
- engtagger
39
- minitest
40
- odyssey
41
- pry
42
- sentence_interpreter
43
- thor
44
21
  i18n (0.8.0)
45
- listen (3.1.5)
46
- rb-fsevent (~> 0.9, >= 0.9.4)
47
- rb-inotify (~> 0.9, >= 0.9.7)
48
- ruby_dep (~> 1.2)
49
- method_source (0.8.2)
50
22
  minitest (5.10.1)
51
23
  multipart-post (2.0.0)
52
24
  octokit (4.6.2)
53
25
  sawyer (~> 0.8.0, >= 0.5.3)
54
- odyssey (0.2.0)
55
- require_all
56
- pry (0.10.4)
57
- coderay (~> 1.1.0)
58
- method_source (~> 0.8.1)
59
- slop (~> 3.4)
60
26
  public_suffix (2.0.5)
61
27
  rack (1.6.5)
62
28
  rack-protection (1.5.3)
63
29
  rack
64
30
  rack-proxy (0.6.0)
65
31
  rack
66
- rb-fsevent (0.9.8)
67
- rb-inotify (0.9.8)
68
- ffi (>= 0.5.0)
69
- require_all (1.4.0)
70
- rerun (0.11.0)
71
- listen (~> 3.0)
72
- ruby_dep (1.5.0)
73
- sass (3.4.23)
74
32
  sawyer (0.8.1)
75
33
  addressable (>= 2.3.5, < 2.6)
76
34
  faraday (~> 0.8, < 1.0)
77
- sentence_interpreter (0.0.7)
78
35
  sinatra (1.4.8)
79
36
  rack (~> 1.5)
80
37
  rack-protection (~> 1.4)
@@ -83,16 +40,10 @@ GEM
83
40
  sinatra_auth_github (1.2.0)
84
41
  sinatra (~> 1.0)
85
42
  warden-github (~> 1.2.0)
86
- slim (3.0.7)
87
- temple (~> 0.7.6)
88
- tilt (>= 1.3.3, < 2.1)
89
- slop (3.6.0)
90
- temple (0.7.7)
91
43
  thin (1.7.0)
92
44
  daemons (~> 1.0, >= 1.0.9)
93
45
  eventmachine (~> 1.0, >= 1.0.4)
94
46
  rack (>= 1, < 3)
95
- thor (0.19.4)
96
47
  thread_safe (0.3.5)
97
48
  tilt (2.0.6)
98
49
  tzinfo (1.2.2)
@@ -111,17 +62,13 @@ PLATFORMS
111
62
  ruby
112
63
 
113
64
  DEPENDENCIES
114
- coffee-script
65
+ byebug
115
66
  dotenv
116
67
  faye-websocket
117
- gemmyrb
118
68
  rack-proxy
119
- rerun
120
- sass
121
69
  sinatra
122
70
  sinatra-cross_origin
123
71
  sinatra_auth_github
124
- slim
125
72
  thin
126
73
 
127
74
  BUNDLED WITH
@@ -1,27 +1,29 @@
1
1
  class Routes::Ws
2
2
 
3
+ # Opens a new websocket connection from a request
3
4
  def self.run(request)
4
5
  return unless Faye::WebSocket.websocket?(request.env)
5
- socket = Faye::WebSocket.new(req.env)
6
- socket.onopen { onopen(request, socket) }
7
- socket.onmessage { |msg| onmessage(request, ws, msg) }
8
- socket.onclose { onclose(request, ws) }
6
+ socket = Faye::WebSocket.new(request.env)
7
+ socket.onopen = Proc.new { |e| onopen(request, socket) }
8
+ socket.onmessage = Proc.new { |e| onmessage(request, socket, e.data) }
9
+ socket.onclose = Proc.new { |e| onclose(request, socket) }
9
10
  socket.rack_response
10
11
  end
11
12
 
12
13
  def self.onopen(request, ws)
13
14
  token = request.params["token"]
14
- unless token
15
- ws.send({
16
- msg: "no valid token was sent with websocket; invalid"
17
- }.to_json)
15
+ if token
16
+ Sockets[token] = ws
17
+ else
18
18
  ws.close
19
- return
20
19
  end
21
- Sockets[token] = ws
22
20
  end
23
21
 
24
- def self.onmessage(request, ws, msg)
22
+ def self.onmessage(request, ws, msg_data)
23
+ data = JSON.parse msg_data
24
+ if data["action"] == "try_authenticate"
25
+ try_authenticate(ws, data["token"])
26
+ end
25
27
  end
26
28
 
27
29
  def self.onclose(request, ws)
@@ -32,6 +34,15 @@ class Routes::Ws
32
34
 
33
35
  private
34
36
 
37
+ def try_authenticate(ws, token)
38
+ if username = AuthenticatedTokens[token]
39
+ ws.send({
40
+ action: "logged_in",
41
+ username: username
42
+ }.to_json)
43
+ end
44
+ end
45
+
35
46
  def send_json_message(ws, msg)
36
47
  EM.next_tick { ws.send msg.to_json }
37
48
  end
@@ -94,13 +94,20 @@ class Server < Sinatra::Base
94
94
  end
95
95
 
96
96
  # Then they authenticate with Github
97
- # TODO render a proper HTML page after this not just plaintext
97
+ #
98
+ # If the client refreshes the page after logging in, they should have stored
99
+ # the token in a cookie. If they hit this route with the same token, it
100
+ # keeps them logged in.
101
+ #
102
+ # This needs to be clicked like a regular link - no AJAX
103
+ #
104
+ # TODO render a proper HTML page after authenticating not just plaintext
98
105
  # saying they can close the window.
99
106
 
100
107
  get '/authenticate' do
101
108
  if token = params["token"]
102
109
  if socket = Sockets[token]
103
- if !AuthenticatedTokens[token]
110
+ unless username = AuthenticatedTokens[token]
104
111
  authenticate!
105
112
  username = get_username
106
113
  Users[username] << (token)
@@ -119,20 +126,23 @@ class Server < Sinatra::Base
119
126
  end
120
127
  end
121
128
 
129
+ # This is hit over AJAX
130
+ # This closes the websocket connection
131
+ # Clients should request a new token and reconnect to ws after logging out
132
+
122
133
  get '/logout' do
134
+ cross_origin allow_origin: "http://localhost:8080"
123
135
  token = params[:token]
124
136
  if token
125
137
  if username = AuthenticatedTokens[token]
126
138
  logout!
127
- Sockets[token].send({
128
- action: "logged_out"
129
- }.to_json)
139
+ AuthenticatedTokens.delete token
140
+ Users[username].delete token
141
+ Sockets[token].close
142
+ Sockets.delete token
143
+ { success: "logged out" }.to_json
130
144
  else
131
- if ws = Sockets[token]
132
- ws.send({msg: "can't find user to log out"}.to_json)
133
- else
134
- {error: "can't find user to log out"}.to_json
135
- end
145
+ { error: "can't find user to log out" }.to_json
136
146
  end
137
147
  else
138
148
  { error: 'cant log out; no token provided' }.to_json
data/lib/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module SinatraSockets
2
- VERSION = '0.0.5'
2
+ VERSION = '0.0.6'
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sinatra_sockets
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.5
4
+ version: 0.0.6
5
5
  platform: ruby
6
6
  authors:
7
7
  - maxpleaner