forked from hexpm/hexpm
/
auth_helpers.ex
293 lines (234 loc) · 8.02 KB
/
auth_helpers.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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
defmodule HexpmWeb.AuthHelpers do
import Plug.Conn
import HexpmWeb.ControllerHelpers, only: [render_error: 3]
alias Hexpm.Accounts.{Auth, Key, Organization, Organizations, User}
alias Hexpm.Repository.{Package, Packages, PackageOwner, Repository}
def authorize(conn, opts) do
user_or_organization = conn.assigns.current_user || conn.assigns.current_organization
if user_or_organization || opts[:authentication] != :required do
authorized(conn, user_or_organization, opts[:fun], opts)
else
error(conn, {:error, :missing})
end
end
defp authorized(conn, %User{service: true}, _funs, _opts) do
conn
end
defp authorized(conn, user_or_organization, funs, opts) do
domain = Keyword.get(opts, :domain)
resource = Keyword.get(opts, :resource)
key = conn.assigns.key
email = conn.assigns.email
cond do
not verified_user?(user_or_organization, email, opts) ->
error(conn, {:error, :unconfirmed})
user_or_organization && !verify_permissions?(key, domain, resource) ->
error(conn, {:error, :domain})
funs ->
Enum.find_value(List.wrap(funs), fn fun ->
case apply_authorization_fun(fun, conn, user_or_organization, opts[:opts]) do
:ok -> nil
other -> error(conn, other)
end
end) || conn
true ->
conn
end
end
defp apply_authorization_fun(fun, conn, user_or_organization, _opts = nil) do
fun.(conn, user_or_organization)
end
defp apply_authorization_fun(fun, conn, user_or_organization, opts) do
fun.(conn, user_or_organization, opts)
end
defp verified_user?(%User{}, email, opts) do
allow_unconfirmed = Keyword.get(opts, :allow_unconfirmed, false)
allow_unconfirmed || (email && email.verified)
end
defp verified_user?(%Organization{}, _email, _opts) do
true
end
defp verified_user?(nil, _email, _opts) do
true
end
defp verify_permissions?(nil, _domain, _resource) do
true
end
defp verify_permissions?(_key, nil, _resource) do
true
end
defp verify_permissions?(key, domain, resource) do
Key.verify_permissions?(key, domain, resource)
end
def error(conn, error) do
case error do
{:error, :missing} ->
unauthorized(conn, "missing authentication information")
{:error, :invalid} ->
unauthorized(conn, "invalid authentication information")
{:error, :password} ->
unauthorized(conn, "invalid username and password combination")
{:error, :key} ->
unauthorized(conn, "invalid API key")
{:error, :revoked_key} ->
unauthorized(conn, "API key revoked")
{:error, :domain} ->
unauthorized(conn, "key not authorized for this action")
{:error, :unconfirmed} ->
forbidden(conn, "email not verified")
{:error, :auth} ->
forbidden(conn, "account not authorized for this action")
{:error, :auth, reason} ->
forbidden(conn, reason)
{:error, :not_found} ->
HexpmWeb.ControllerHelpers.not_found(conn)
end
end
def authenticate(conn) do
case get_req_header(conn, "authorization") do
["Basic " <> credentials] ->
basic_auth(credentials)
[key] ->
key_auth(key, conn)
_ ->
{:error, :missing}
end
end
defp basic_auth(credentials) do
with {:ok, decoded} <- Base.decode64(credentials),
[username_or_email, password] <- String.split(decoded, ":", parts: 2) do
case Auth.password_auth(username_or_email, password) do
{:ok, result} -> {:ok, result}
:error -> {:error, :password}
end
else
_ ->
{:error, :invalid}
end
end
defp key_auth(key, conn) do
case Auth.key_auth(key, usage_info(conn)) do
{:ok, result} -> {:ok, result}
:error -> {:error, :key}
:revoked -> {:error, :revoked_key}
end
end
defp usage_info(%{remote_ip: remote_ip} = conn) do
%{
ip: remote_ip,
used_at: DateTime.utc_now(),
user_agent: get_req_header(conn, "user-agent")
}
end
def unauthorized(conn, reason) do
conn
|> put_resp_header("www-authenticate", "Basic realm=hex")
|> render_error(401, message: reason)
end
def forbidden(conn, reason) do
render_error(conn, 403, message: reason)
end
def package_owner(conn, user_or_organization, opts \\ [])
def package_owner(%Plug.Conn{} = conn, user_or_organization, opts) do
package_owner(conn.assigns.repository, conn.assigns.package, user_or_organization, opts)
end
def package_owner(
%Repository{} = repository,
%Package{} = package,
%Organization{} = organization,
opts
) do
owner_level = opts[:owner_level] || "maintainer"
cond do
repository.organization_id == organization.id -> :ok
Packages.owner_with_access?(package, organization.user, owner_level) -> :ok
repository.id == 1 -> {:error, :auth}
true -> {:error, :not_found}
end
end
def package_owner(%Repository{} = repository, %Package{} = package, %User{} = user, opts) do
cond do
Packages.owner_with_access?(package, user, opts[:owner_level] || "maintainer") -> :ok
repository.id == 1 -> {:error, :auth}
true -> {:error, :not_found}
end
end
def package_owner(%Repository{} = repository, %Package{}, nil, _opts) do
if repository.id == 1 do
{:error, :auth}
else
{:error, :not_found}
end
end
def package_owner(
%Repository{} = repository,
nil = _package,
%Organization{} = organization,
_opts
) do
cond do
repository.id == 1 -> :ok
repository.organization_id == organization.id -> :ok
true -> {:error, :not_found}
end
end
def package_owner(%Repository{} = repository, nil = _package, %User{} = user, opts) do
expected_role = PackageOwner.level_to_organization_role(opts[:owner_level] || "maintainer")
actual_role = Organizations.get_role(repository.organization, user)
cond do
repository.id == 1 -> :ok
actual_role && actual_role in Organization.role_or_higher(expected_role) -> :ok
actual_role -> {:error, :auth}
true -> {:error, :not_found}
end
end
def package_owner(%Repository{} = repository, nil = _package, nil = _user, _opts) do
boolean_to_not_found(repository.id == 1)
end
def package_owner(nil = _repository, _package, _user, _opts) do
{:error, :not_found}
end
def organization_access(conn, user_or_organization, opts \\ [])
def organization_access(%Plug.Conn{} = conn, user_or_organization, opts) do
organization_access(conn.assigns.organization, user_or_organization, opts)
end
def organization_access(%Organization{id: 1}, _user_or_organization, opts) do
role = opts[:organization_role] || "read"
boolean_to_auth_error(role == "read")
end
def organization_access(nil = _organization, _user_or_organization, _opts) do
:ok
end
def organization_access(%Organization{} = organization, user_or_organization, opts) do
cond do
Organizations.access?(
organization,
user_or_organization,
opts[:organization_role] || "read"
) ->
:ok
organization.id == 1 ->
{:error, :auth}
true ->
{:error, :not_found}
end
end
def organization_billing_active(conn, user_or_organization, opts \\ [])
def organization_billing_active(%Plug.Conn{} = conn, _user_or_organization, _opts) do
organization_billing_active(conn.assigns.organization, nil)
end
def organization_billing_active(%Organization{} = organization, _user_or_organization, _opts) do
if organization.id == 1 or Organization.billing_active?(organization) do
:ok
else
{:error, :auth, "organization has no active billing subscription"}
end
end
def organization_billing_active(nil = _organization, _user_or_organization, _opts) do
:ok
end
defp boolean_to_auth_error(true), do: :ok
defp boolean_to_auth_error(false), do: {:error, :auth}
defp boolean_to_not_found(true), do: :ok
defp boolean_to_not_found(false), do: {:error, :not_found}
end