-
Notifications
You must be signed in to change notification settings - Fork 70
/
Copy pathuser_from_auth.ex
220 lines (198 loc) · 6.81 KB
/
user_from_auth.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
defmodule PhoenixGuardian.UserFromAuth do
alias PhoenixGuardian.User
alias PhoenixGuardian.Authorization
alias Ueberauth.Auth
def get_or_insert(auth, current_user, repo) do
case auth_and_validate(auth, repo) do
{:error, :not_found} -> register_user_from_auth(auth, current_user, repo)
{:error, reason} -> {:error, reason}
authorization ->
if authorization.expires_at && authorization.expires_at < Guardian.Utils.timestamp do
replace_authorization(authorization, auth, current_user, repo)
else
user_from_authorization(authorization, current_user, repo)
end
end
end
# We need to check the pw for the identity provider
defp validate_auth_for_registration(%Auth{provider: :identity} = auth) do
pw = Map.get(auth.credentials.other, :password)
pwc = Map.get(auth.credentials.other, :password_confirmation)
email = auth.info.email
case pw do
nil ->
{:error, :password_is_null}
"" ->
{:error, :password_empty}
^pwc ->
validate_pw_length(pw, email)
_ ->
{:error, :password_confirmation_does_not_match}
end
end
# All the other providers are oauth so should be good
defp validate_auth_for_registration(auth), do: :ok
defp validate_pw_length(pw, email) when is_binary(pw) do
if String.length(pw) >= 8 do
validate_email(email)
else
{:error, :password_length_is_less_than_8}
end
end
defp validate_email(email) when is_binary(email) do
case Regex.run(~r/^[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/, email) do
nil ->
{:error, :invalid_email}
[email] ->
:ok
end
end
defp register_user_from_auth(auth, current_user, repo) do
case validate_auth_for_registration(auth) do
:ok ->
case repo.transaction(fn -> create_user_from_auth(auth, current_user, repo) end) do
{:ok, response} -> response
{:error, reason} -> {:error, reason}
end
{:error, reason} -> {:error, reason}
end
end
defp replace_authorization(authorization, auth, current_user, repo) do
case validate_auth_for_registration(auth) do
:ok ->
case user_from_authorization(authorization, current_user, repo) do
{:ok, user} ->
case repo.transaction(fn ->
repo.delete(authorization)
authorization_from_auth(user, auth, repo)
user
end) do
{:ok, user} -> {:ok, user}
{:error, reason} -> {:error, reason}
end
{:error, reason} -> {:error, reason}
end
{:error, reason} -> {:error, reason}
end
end
defp user_from_authorization(authorization, current_user, repo) do
case repo.one(Ecto.assoc(authorization, :user)) do
nil -> {:error, :user_not_found}
user ->
if current_user && current_user.id != user.id do
{:error, :user_does_not_match}
else
{:ok, user}
end
end
end
defp create_user_from_auth(auth, current_user, repo) do
user = current_user
if !user, do: user = repo.get_by(User, email: auth.info.email)
if !user, do: user = create_user(auth, repo)
authorization_from_auth(user, auth, repo)
{:ok, user}
end
defp create_user(auth, repo) do
name = name_from_auth(auth)
result = User.registration_changeset(%User{}, scrub(%{email: auth.info.email, name: name}))
|> repo.insert
case result do
{:ok, user} -> user
{:error, reason} -> repo.rollback(reason)
end
end
defp auth_and_validate(%{provider: :identity} = auth, repo) do
case repo.get_by(Authorization, uid: uid_from_auth(auth), provider: to_string(auth.provider)) do
nil -> {:error, :not_found}
authorization ->
case auth.credentials.other.password do
pass when is_binary(pass) ->
if Comeonin.Bcrypt.checkpw(auth.credentials.other.password, authorization.token) do
authorization
else
{:error, :password_does_not_match}
end
_ -> {:error, :password_required}
end
end
end
defp auth_and_validate(%{provider: service} = auth, repo) when service in [:google, :facebook, :github] do
case repo.get_by(Authorization, uid: uid_from_auth(auth), provider: to_string(auth.provider)) do
nil -> {:error, :not_found}
authorization ->
if authorization.uid == uid_from_auth(auth) do
authorization
else
{:error, :uid_mismatch}
end
end
end
defp auth_and_validate(auth, repo) do
case repo.get_by(Authorization, uid: uid_from_auth(auth), provider: to_string(auth.provider)) do
nil -> {:error, :not_found}
authorization ->
if authorization.token == auth.credentials.token do
authorization
else
{:error, :token_mismatch}
end
end
end
defp authorization_from_auth(user, auth, repo) do
authorization = Ecto.build_assoc(user, :authorizations)
result = Authorization.changeset(
authorization,
scrub(
%{
provider: to_string(auth.provider),
uid: uid_from_auth(auth),
token: token_from_auth(auth),
refresh_token: auth.credentials.refresh_token,
expires_at: auth.credentials.expires_at,
password: password_from_auth(auth),
password_confirmation: password_confirmation_from_auth(auth)
}
)
) |> repo.insert
case result do
{:ok, the_auth} -> the_auth
{:error, reason} -> repo.rollback(reason)
end
end
defp name_from_auth(auth) do
if auth.info.name do
auth.info.name
else
[auth.info.first_name, auth.info.last_name]
|> Enum.filter(&(&1 != nil and String.strip(&1) != ""))
|> Enum.join(" ")
end
end
defp token_from_auth(%{provider: :identity} = auth) do
case auth do
%{ credentials: %{ other: %{ password: pass } } } when not is_nil(pass) ->
Comeonin.Bcrypt.hashpwsalt(pass)
_ -> nil
end
end
defp token_from_auth(auth), do: auth.credentials.token
defp uid_from_auth(%{ provider: :slack } = auth), do: auth.credentials.other.user_id
defp uid_from_auth(auth), do: auth.uid
defp password_from_auth(%{provider: :identity} = auth), do: auth.credentials.other.password
defp password_from_auth(_), do: nil
defp password_confirmation_from_auth(%{provider: :identity} = auth) do
auth.credentials.other.password_confirmation
end
defp password_confirmation_from_auth(_), do: nil
# We don't have any nested structures in our params that we are using scrub with so this is a very simple scrub
defp scrub(params) do
result = Enum.filter(params, fn
{key, val} when is_binary(val) -> String.strip(val) != ""
{key, val} when is_nil(val) -> false
_ -> true
end)
|> Enum.into(%{})
result
end
end