Skip to content

Commit ba4f19f

Browse files
authored
Add OAuth log in via new log_in_oauth method (#42)
1 parent efd92ba commit ba4f19f

File tree

4 files changed

+61
-0
lines changed

4 files changed

+61
-0
lines changed

README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,14 @@ iex> conn = Hunter.log_in(app, "[email protected]", "your_password", "https://exa
6868
bearer_token: "123456"}
6969
```
7070

71+
Or, if you want to use [OAuth code](https://docs.joinmastodon.org/methods/apps/oauth/) for authentication:
72+
73+
```elixir
74+
iex> conn = Hunter.log_in_oauth(app, "123456code", "https://example.com")
75+
%Hunter.Client{base_url: "https://example.com",
76+
bearer_token: "123456"}
77+
```
78+
7179
Now you can use `conn` in any API request.
7280

7381
If you don't want to register an application but you already know your

lib/hunter/api.ex

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -681,6 +681,22 @@ defmodule Hunter.Api do
681681
base_url :: String.t()
682682
) :: Hunter.Client.t()
683683

684+
@doc """
685+
Retrieve access token using OAuth access code
686+
687+
## Parameters
688+
689+
* `app` - application details, see: `Hunter.Application.create_app/5` for more details.
690+
* `oauth_code` - oauth authentication code
691+
* `base_url` - API base url, default: `https://mastodon.social`
692+
693+
"""
694+
@callback log_in_oauth(
695+
app :: Hunter.Application.t(),
696+
oauth_code :: String.t(),
697+
base_url :: String.t()
698+
) :: Hunter.Client.t()
699+
684700
@doc """
685701
Fetch user's blocked domains
686702

lib/hunter/api/http_client.ex

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -319,6 +319,26 @@ defmodule Hunter.Api.HTTPClient do
319319
%Hunter.Client{base_url: base_url, bearer_token: response["access_token"]}
320320
end
321321

322+
def log_in_oauth(
323+
%Hunter.Application{client_id: client_id, client_secret: client_secret},
324+
oauth_code,
325+
base_url
326+
) do
327+
payload = %{
328+
client_id: client_id,
329+
client_secret: client_secret,
330+
grant_type: "authorization_code",
331+
code: oauth_code
332+
}
333+
334+
response =
335+
"/oauth/token"
336+
|> process_url(base_url)
337+
|> request!(nil, :post, payload)
338+
339+
%Hunter.Client{base_url: base_url, bearer_token: response["access_token"]}
340+
end
341+
322342
def blocked_domains(conn, options) do
323343
"/api/v1/domain_blocks"
324344
|> process_url(conn)

lib/hunter/client.ex

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,4 +51,21 @@ defmodule Hunter.Client do
5151
base_url = base_url || Config.api_base_url()
5252
Config.hunter_api().log_in(app, username, password, base_url)
5353
end
54+
55+
@doc """
56+
Retrieve access token
57+
58+
## Parameters
59+
60+
* `app` - application details, see: `Hunter.Application.create_app/5` for more details.
61+
* `oauth_code` - oauth authentication code
62+
* `base_url` - API base url, default: `https://mastodon.social`
63+
64+
"""
65+
@spec log_in_oauth(Hunter.Application.t(), String.t(), String.t()) :: Hunter.Client.t()
66+
def log_in_oauth(app, oauth_code, base_url \\ "https://mastodon.social") do
67+
base_url = base_url || Config.api_base_url()
68+
Config.hunter_api().log_in_oauth(app, oauth_code, base_url)
69+
end
70+
5471
end

0 commit comments

Comments
 (0)