-
Notifications
You must be signed in to change notification settings - Fork 2
/
router.ex
148 lines (131 loc) · 4.23 KB
/
router.ex
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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
148
defmodule POABackend.Auth.Router do
use Plug.Router
@moduledoc false
alias POABackend.Auth
alias POABackend.CustomHandler.REST
alias POABackend.Auth.Models.User
import Plug.Conn
@token_default_ttl {1, :hour}
plug REST.Plugs.Accept, ["application/json", "application/msgpack"]
plug Plug.Parsers, parsers: [Msgpax.PlugParser, :json], pass: ["application/msgpack", "application/json"], json_decoder: Poison
plug :match
plug :dispatch
post "/session" do
with {"authorization", "Basic " <> base64} <- List.keyfind(conn.req_headers, "authorization", 0),
{:ok, decoded64} <- Base.decode64(base64),
[user_name, password] <- String.split(decoded64, ":"),
{:ok, user} <- Auth.authenticate_user(user_name, password)
do
ttl = Application.get_env(:poa_backend, :jwt_ttl, @token_default_ttl)
{:ok, token, _} = POABackend.Auth.Guardian.encode_and_sign(user, %{}, ttl: ttl)
{:ok, result} =
%{token: token}
|> Poison.encode
send_resp(conn, 200, result)
else
_error ->
conn
|> send_resp(401, "")
|> halt
end
end
post "/user" do
with {"authorization", "Basic " <> base64} <- List.keyfind(conn.req_headers, "authorization", 0),
{:ok, decoded64} <- Base.decode64(base64),
[admin_name, admin_password] <- String.split(decoded64, ":"),
{:ok, :valid} <- Auth.authenticate_admin(admin_name, admin_password)
do
user_name = Map.get(conn.params, "user-name", Auth.generate_user_name())
password = Map.get(conn.params, "password", Auth.generate_password())
case Auth.valid_user_name?(user_name) do
true ->
{:ok, _user} = Auth.create_user(user_name, password)
{:ok, result} =
%{:'user-name' => user_name,
:password => password}
|> Poison.encode
send_resp(conn, 200, result)
false ->
conn
|> send_resp(409, "")
|> halt
end
else
_error ->
conn
|> send_resp(401, "")
|> halt
end
end
get "/user" do
with {"authorization", "Basic " <> base64} <- List.keyfind(conn.req_headers, "authorization", 0),
{:ok, decoded64} <- Base.decode64(base64),
[admin_name, admin_password] <- String.split(decoded64, ":"),
{:ok, :valid} <- Auth.authenticate_admin(admin_name, admin_password)
do
result =
Auth.list_users()
|> Enum.map(&User.to_map(&1))
|> Poison.encode!
send_resp(conn, 200, result)
else
_error ->
conn
|> send_resp(401, "")
|> halt
end
end
post "/blacklist/user" do
with {"authorization", "Basic " <> base64} <- List.keyfind(conn.req_headers, "authorization", 0),
{:ok, decoded64} <- Base.decode64(base64),
[admin_name, admin_password] <- String.split(decoded64, ":"),
true <- conn.params["user"] != nil,
{:ok, :valid} <- Auth.authenticate_admin(admin_name, admin_password)
do
case Auth.get_user(conn.params["user"]) do
nil ->
send_resp(conn, 404, "")
user ->
{:ok, _} = Auth.deactivate_user(user)
send_resp(conn, 200, "")
end
else
false ->
conn
|> send_resp(404, "")
|> halt
_error ->
conn
|> send_resp(401, "")
|> halt
end
end
post "/blacklist/token" do
with {"authorization", "Basic " <> base64} <- List.keyfind(conn.req_headers, "authorization", 0),
{:ok, decoded64} <- Base.decode64(base64),
[admin_name, admin_password] <- String.split(decoded64, ":"),
true <- conn.params["token"] != nil,
{:ok, :valid} <- Auth.authenticate_admin(admin_name, admin_password)
do
case Auth.valid_token?(conn.params["token"]) do
false ->
send_resp(conn, 404, "")
true ->
{:ok, _} = Auth.create_banned_token(conn.params["token"])
send_resp(conn, 200, "")
end
else
false ->
conn
|> send_resp(404, "")
|> halt
_error ->
conn
|> send_resp(401, "")
|> halt
end
end
match _ do
send_resp(conn, 404, "")
end
end