Skip to content

Commit

Permalink
provide token
Browse files Browse the repository at this point in the history
  • Loading branch information
bodrovis committed Mar 2, 2023
1 parent f715cd5 commit 9bc1032
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 20 deletions.
13 changes: 7 additions & 6 deletions lib/elixir_lokalise_api/endpoints/oauth2/auth.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
Expand All @@ -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"
}
Expand All @@ -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"
}
Expand Down
9 changes: 7 additions & 2 deletions lib/elixir_lokalise_api/request.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -45,7 +51,6 @@ defmodule ElixirLokaliseApi.Request do
end
end


defp headers(:api) do
opts = headers(:base)

Expand Down
26 changes: 14 additions & 12 deletions test/elixir_lokalise_api/endpoints/oauth2/auth_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -18,33 +18,35 @@ 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

test "auth allows to pass a list of scopes" 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
Expand All @@ -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"
Expand Down

0 comments on commit 9bc1032

Please sign in to comment.