Page Menu
Home
Phabricator
Search
Configure Global Search
Log In
Files
F665049
irc_auth_sse_controller.ex
No One
Temporary
Actions
Download File
Edit File
Delete File
View Transforms
Subscribe
Mute Notifications
Award Token
Flag For Later
Size
1 KB
Subscribers
None
irc_auth_sse_controller.ex
View Options
defmodule
NolaWeb.IrcAuthSseController
do
use
NolaWeb
,
:controller
require
Logger
@ping_interval
20_000
@expire_delay
:timer
.
minutes
(
3
)
def
sse
(
conn
,
params
)
do
perks
=
if
uri
=
Map
.
get
(
params
,
"redirect_to"
)
do
{
:redirect
,
uri
}
else
nil
end
token
=
String
.
downcase
(
EntropyString
.
random_string
(
65
))
conn
|>
assign
(
:token
,
token
)
|>
assign
(
:perks
,
perks
)
|>
put_resp_header
(
"X-Accel-Buffering"
,
"no"
)
|>
put_resp_header
(
"content-type"
,
"text/event-stream"
)
|>
send_chunked
(
200
)
|>
subscribe
()
|>
send_sse_message
(
"token"
,
token
)
|>
sse_loop
end
def
subscribe
(
conn
)
do
:timer
.
send_interval
(
@ping_interval
,
{
:event
,
:ping
})
:timer
.
send_after
(
@expire_delay
,
{
:event
,
:expire
})
{
:ok
,
_
}
=
Registry
.
register
(
IRC.PubSub
,
"messages:private"
,
[])
conn
end
def
sse_loop
(
conn
)
do
{
type
,
event
,
exit
}
=
receive
do
{
:event
,
:ping
}
->
{
"ping"
,
"ping"
,
false
}
{
:event
,
:expire
}
->
{
"expire"
,
"expire"
,
true
}
{
:irc
,
:text
,
%{
account
:
account
,
text
:
token
}
=
m
}
->
if
String
.
downcase
(
String
.
trim
(
token
))
==
conn
.
assigns
.
token
do
path
=
Nola.AuthToken
.
new_path
(
account
.
id
,
conn
.
assigns
.
perks
)
m
.
replyfun
.
(
"ok!"
)
{
"authenticated"
,
path
,
true
}
else
{
nil
,
nil
,
false
}
end
_
->
{
nil
,
nil
,
false
}
end
conn
=
if
type
do
send_sse_message
(
conn
,
type
,
event
)
else
conn
end
if
exit
do
conn
else
sse_loop
(
conn
)
end
end
defp
send_sse_message
(
conn
,
type
,
data
)
do
{
:ok
,
conn
}
=
chunk
(
conn
,
"event:
#{
type
}
\n
data:
#{
data
}
\n\n
"
)
conn
end
end
File Metadata
Details
Attached
Mime Type
text/x-ruby
Expires
Fri, Feb 27, 7:24 AM (1 d, 7 h)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
87145
Default Alt Text
irc_auth_sse_controller.ex (1 KB)
Attached To
rNOLA Nola
Event Timeline
Log In to Comment