Skip to content

Commit

Permalink
Version 0.4.0
Browse files Browse the repository at this point in the history
  • Loading branch information
Oscar Olivera committed Jan 10, 2020
1 parent 708b7f2 commit 3b5c298
Show file tree
Hide file tree
Showing 8 changed files with 88 additions and 21 deletions.
29 changes: 29 additions & 0 deletions lib/api/account.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
defmodule Whatsapp.Api.Account do
@moduledoc """
Módulo para el manejo de la configuración de Whatsapp
"""

@parser Application.get_env(:wax, :parser)

@doc """
Registrar una cuenta en Whatsapp
"""
@spec create(tuple(), map) :: boolean
def create({url, auth_header}, data) do
url
|> Kernel.<>("/account")
|> WhatsappApiRequest.post!(data, [auth_header])
|> @parser.parse(:account)
end

@doc """
Verificar la cuenta con el código recibido en el móvil
"""
@spec create(tuple(), map) :: boolean
def verify({url, auth_header}, code) do
url
|> Kernel.<>("/account/verify")
|> WhatsappApiRequest.post!(%{"code" => code}, [auth_header])
|> @parser.parse(:account)
end
end
11 changes: 11 additions & 0 deletions lib/api/settings.ex
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,15 @@ defmodule Whatsapp.Api.Settings do
|> WhatsappApiRequest.rate_limit_request(:patch!, data, [auth_header])
|> @parser.parse(:settings_update_application)
end

@doc """
Actualiza la configuración de la cuenta
"""
@spec two_step(tuple(), String.t()) :: boolean
def two_step({url, auth_header}, pin) do
url
|> Kernel.<>("/settings/account/two-step")
|> WhatsappApiRequest.rate_limit_request(:post!, %{"pin" => pin}, [auth_header])
|> @parser.parse(:settings_two_step)
end
end
16 changes: 6 additions & 10 deletions lib/api/users.ex
Original file line number Diff line number Diff line change
Expand Up @@ -10,26 +10,22 @@ defmodule Whatsapp.Api.Users do
@doc """
Login para generar token de autenticación
"""
@spec login(String.t(), binary()) :: map
def login(url, token) do
headers = [{"Authorization", "Basic #{token}"}]

@spec login(tuple()) :: map
def login({url, auth_header}) do
url
|> Kernel.<>("/users/login")
|> WhatsappApiRequest.post!(nil, headers)
|> WhatsappApiRequest.post!(nil, [auth_header])
|> @parser.parse(:users_login)
end

@doc """
Logout de la sesión del usuario
"""
@spec logout(String.t(), binary()) :: map
def logout(url, token) do
headers = [{"Authorization", "Bearer #{token}"}]

@spec logout(tuple()) :: map
def logout({url, auth_header}) do
url
|> Kernel.<>("/users/logout")
|> WhatsappApiRequest.post(nil, headers)
|> WhatsappApiRequest.post!(nil, [auth_header])
|> @parser.parse(:users_logout)
end
end
18 changes: 11 additions & 7 deletions lib/auth/manager.ex
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ defmodule Whatsapp.Auth.Manager do
"""
@spec login(WhatsappProvider.t()) :: map
def login(%WhatsappProvider{} = provider) do
case Users.login(provider.url, generate_token(provider)) do
auth_header = generate_login_auth_header(provider.username, provider.password)
case Users.login({provider.url, auth_header}) do
%{"users" => [login_data]} ->
expires =
login_data
Expand All @@ -26,7 +27,8 @@ defmodule Whatsapp.Auth.Manager do

Map.put(login_data, "expires_after", expires)

_ ->
error ->
IO.inspect(error)
Logger.error(fn -> "Whatsapp login failed for #{provider.name}" end)
%{}
end
Expand All @@ -37,7 +39,8 @@ defmodule Whatsapp.Auth.Manager do
"""
@spec logout(WhatsappProvider.t(), binary()) :: :ok | :error
def logout(%WhatsappProvider{} = provider, token) do
case Users.logout(provider.url, token) do
auth_header = {"Authorization", "Bearer #{token}"}
case Users.logout({provider.url, auth_header}) do
{:ok, _} ->
Logger.info(fn -> "Logout #{provider.name} successful" end)
:ok
Expand All @@ -52,10 +55,11 @@ defmodule Whatsapp.Auth.Manager do
end

@doc """
Genera el token para autenticarse con el servicio de Whatsapp
Genera el header para autenticarse con el servicio de Whatsapp
"""
@spec generate_token(map) :: binary()
def generate_token(%{username: username, password: password}) do
Base.encode64("#{username}:#{password}")
@spec generate_login_auth_header(String.t(), String.t()) :: binary()
def generate_login_auth_header(username, password) do
token = Base.encode64("#{username}:#{password}")
{"Authorization", "Basic #{token}"}
end
end
6 changes: 3 additions & 3 deletions lib/auth/server.ex
Original file line number Diff line number Diff line change
Expand Up @@ -86,9 +86,9 @@ defmodule Whatsapp.Auth.Server do
tipo info para token check diario
"""
@spec handle_info(:token_check, any()) :: {:noreply, any()}
def handle_info(:token_check, %{tokens: tokens, providers: providers} = state) do
def handle_info(:token_check, %{providers: providers} = state) do
Logger.info("Checking tokens")
tokens = update_expired_tokens(providers, tokens)
tokens = update_expired_tokens(providers)
schedule_token_check()
{:noreply, Map.put(state, :tokens, tokens)}
end
Expand Down Expand Up @@ -121,7 +121,7 @@ defmodule Whatsapp.Auth.Server do
Process.send_after(self(), :token_check, @daily)
end

def update_expired_tokens(providers, credentials) do
def update_expired_tokens(providers) do
Enum.reduce(
providers,
%{},
Expand Down
26 changes: 26 additions & 0 deletions lib/whatsapp_api.ex
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ defmodule WhatsappApi do
alias Whatsapp.Api.Media
alias Whatsapp.Api.Settings
alias Whatsapp.Api.Health
alias Whatsapp.Api.Users
alias Whatsapp.Api.Account
alias Whatsapp.Auth.Server, as: AuthServer

@doc """
Expand Down Expand Up @@ -83,6 +85,12 @@ defmodule WhatsappApi do
|> Settings.update_application(data)
end

def two_step(pin, provider) do
provider
|> AuthServer.get_token_info()
|> Settings.two_step(pin)
end

@doc """
Check whatsapp health
"""
Expand All @@ -92,4 +100,22 @@ defmodule WhatsappApi do
|> AuthServer.get_token_info()
|> Health.get_summary()
end

def logout(provider) do
provider
|> AuthServer.get_token_info()
|> Users.logout()
end

def create_account(data, provider) do
provider
|> AuthServer.get_token_info()
|> Account.create(data)
end

def verify_account(code, provider) do
provider
|> AuthServer.get_token_info()
|> Account.verify(code)
end
end
1 change: 1 addition & 0 deletions lib/whatsapp_api_request.ex
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ defmodule WhatsappApiRequest do
end

def process_response_body(body) do
IO.inspect(body)
Jason.decode!(body)
end
end
2 changes: 1 addition & 1 deletion mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ defmodule WhatsappApi.MixProject do
[
app: :wax,
elixirc_paths: elixirc_paths(Mix.env()),
version: "0.3.1",
e version: "0.4.0",
description: "Whatsapp Elixir Client",
elixir: "~> 1.6",
package: package(),
Expand Down

0 comments on commit 3b5c298

Please sign in to comment.