From 9bc10326d8ddd9faa10d44e913497aaab5e19111 Mon Sep 17 00:00:00 2001 From: bodrovis Date: Thu, 2 Mar 2023 15:31:43 +0200 Subject: [PATCH] provide token --- .../endpoints/oauth2/auth.ex | 13 +++++----- lib/elixir_lokalise_api/request.ex | 9 +++++-- .../endpoints/oauth2/auth_test.exs | 26 ++++++++++--------- 3 files changed, 28 insertions(+), 20 deletions(-) diff --git a/lib/elixir_lokalise_api/endpoints/oauth2/auth.ex b/lib/elixir_lokalise_api/endpoints/oauth2/auth.ex index 42ee4bf..4e760c9 100644 --- a/lib/elixir_lokalise_api/endpoints/oauth2/auth.ex +++ b/lib/elixir_lokalise_api/endpoints/oauth2/auth.ex @@ -11,7 +11,8 @@ defmodule ElixirLokaliseApi.OAuth2.Auth do @singular_data_key nil alias ElixirLokaliseApi.Config - use ElixirLokaliseApi.DynamicResource#, import: [:foreign_model] + # , import: [:foreign_model] + use ElixirLokaliseApi.DynamicResource @doc """ Generates an authentication URL to obtain @@ -20,7 +21,7 @@ defmodule ElixirLokaliseApi.OAuth2.Auth do def auth(scope, redirect_uri \\ nil, state \\ nil) do "#{Config.base_url(:oauth2)}auth" |> URI.new!() - |> do_append(:client_id, Config.oauth2_client_id) + |> do_append(:client_id, Config.oauth2_client_id()) |> do_append(:scope, Enum.join(scope, " ")) |> do_append(:redirect_uri, redirect_uri) |> do_append(:state, state) @@ -32,8 +33,8 @@ defmodule ElixirLokaliseApi.OAuth2.Auth do """ def token(code) do data = %{ - client_id: Config.oauth2_client_id, - client_secret: Config.oauth2_client_secret, + client_id: Config.oauth2_client_id(), + client_secret: Config.oauth2_client_secret(), code: code, grant_type: "authorization_code" } @@ -50,8 +51,8 @@ defmodule ElixirLokaliseApi.OAuth2.Auth do """ def refresh(refresh_token) do data = %{ - client_id: Config.oauth2_client_id, - client_secret: Config.oauth2_client_secret, + client_id: Config.oauth2_client_id(), + client_secret: Config.oauth2_client_secret(), refresh_token: refresh_token, grant_type: "refresh_token" } diff --git a/lib/elixir_lokalise_api/request.ex b/lib/elixir_lokalise_api/request.ex index 3f49339..bf75974 100644 --- a/lib/elixir_lokalise_api/request.ex +++ b/lib/elixir_lokalise_api/request.ex @@ -9,7 +9,13 @@ defmodule ElixirLokaliseApi.Request do alias ElixirLokaliseApi.UrlGenerator alias __MODULE__ - @defaults [type: nil, data: nil, url_params: Keyword.new(), query_params: Keyword.new(), for: :api] + @defaults [ + type: nil, + data: nil, + url_params: Keyword.new(), + query_params: Keyword.new(), + for: :api + ] @doc """ Prepares and sends an HTTP request with the provided verb and options. @@ -45,7 +51,6 @@ defmodule ElixirLokaliseApi.Request do end end - defp headers(:api) do opts = headers(:base) diff --git a/test/elixir_lokalise_api/endpoints/oauth2/auth_test.exs b/test/elixir_lokalise_api/endpoints/oauth2/auth_test.exs index bf68354..386608d 100644 --- a/test/elixir_lokalise_api/endpoints/oauth2/auth_test.exs +++ b/test/elixir_lokalise_api/endpoints/oauth2/auth_test.exs @@ -18,7 +18,7 @@ defmodule ElixirLokaliseApi.OAuth2.AuthTest do uri = Auth.auth(["read_projects"]) assert String.contains?(uri, "scope=read_projects") - assert String.contains?(uri, "client_id=#{Config.oauth2_client_id}") + assert String.contains?(uri, "client_id=#{Config.oauth2_client_id()}") assert String.contains?(uri, "https://app.lokalise.com/oauth2/auth") end @@ -26,25 +26,27 @@ defmodule ElixirLokaliseApi.OAuth2.AuthTest do uri = Auth.auth(["read_projects", "write_projects", "write_tasks"]) assert String.contains?(uri, "scope=read_projects+write_projects+write_tasks") - assert String.contains?(uri, "client_id=#{Config.oauth2_client_id}") + assert String.contains?(uri, "client_id=#{Config.oauth2_client_id()}") end test "auth allows to pass redirect uri" do - uri = Auth.auth( - ["read_projects", "write_tasks"], - "http://example.com/callback" - ) + uri = + Auth.auth( + ["read_projects", "write_tasks"], + "http://example.com/callback" + ) assert String.contains?(uri, "scope=read_projects+write_tasks") assert String.contains?(uri, "example.com%2Fcallback") end test "auth allows to pass state" do - uri = Auth.auth( - ["read_projects", "write_tasks"], - "http://example.com/callback", - "secret state" - ) + uri = + Auth.auth( + ["read_projects", "write_tasks"], + "http://example.com/callback", + "secret state" + ) assert String.contains?(uri, "secret+state") end @@ -53,7 +55,7 @@ defmodule ElixirLokaliseApi.OAuth2.AuthTest do use_cassette "oauth2_token" do {:ok, %TokenModel{} = result} = Auth.token(System.get_env("OAUTH2_CODE")) - assert result.access_token == System.get_env("OAUTH2_ACCESS_TOKEN") + assert result.access_token == "e43a0fac0e025da1dbbf7201a08c28691f399a1d" assert result.refresh_token == "123" assert result.expires_in == 3600 assert result.token_type == "Bearer"