Skip to content

Commit e49cfd7

Browse files
committed
Add type spec
1 parent 10a24cc commit e49cfd7

File tree

6 files changed

+35
-9
lines changed

6 files changed

+35
-9
lines changed

lib/phoenix_oauth2_provider.ex

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,13 @@ defmodule PhoenixOauth2Provider do
1313
library.
1414
"""
1515
alias Mix.Phoenix
16+
alias Plug.Conn
17+
alias Ecto.Schema
1618

1719
@doc """
1820
Will get current resource owner from endpoint
1921
"""
22+
@spec current_resource_owner(Conn.t()) :: Schema.t() | no_return
2023
def current_resource_owner(conn) do
2124
resource_owner_key = Keyword.get(config(), :current_resource_owner, :current_user)
2225

@@ -27,11 +30,13 @@ defmodule PhoenixOauth2Provider do
2730
end
2831

2932
@doc false
33+
@spec config :: Keyword.t()
3034
def config do
3135
Application.get_env(:phoenix_oauth2_provider, PhoenixOauth2Provider, [])
3236
end
3337

3438
@doc false
39+
@spec router_helpers :: module()
3540
def router_helpers do
3641
module = Keyword.get(config(), :router, router_module())
3742

lib/phoenix_oauth2_provider/view_helpers.ex

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,23 @@ defmodule PhoenixOauth2Provider.ViewHelpers do
33
Helper functions for PhoenixOauth2Provider Views.
44
"""
55
use Phoenix.HTML
6+
alias Ecto.Changeset
7+
alias Phoenix.HTML
68

79
@doc """
810
Generates tag for inlined form input errors.
911
"""
12+
@spec error_tag(Changeset.t(), atom()) :: HTML.safe() | nil
1013
def error_tag(form, field) do
1114
if error = form.errors[field] do
12-
content_tag :span, translate_error(error), class: "help-block"
15+
content_tag(:span, translate_error(error), class: "help-block")
1316
end
1417
end
1518

1619
@doc """
1720
Translates an error message using gettext.
1821
"""
22+
@spec translate_error({binary(), Keyword.t()}) :: binary()
1923
def translate_error({msg, opts}) do
2024
# Because error messages were defined within Ecto, we must
2125
# call the Gettext module passing our Gettext backend. We
@@ -28,6 +32,7 @@ defmodule PhoenixOauth2Provider.ViewHelpers do
2832
Gettext.dngettext(PhoenixOauth2Provider.Web.Gettext, "errors", msg, msg, opts[:count] || 0, opts)
2933
end
3034

35+
@spec translate_error(binary()) :: binary()
3136
def translate_error(msg) do
3237
Gettext.dgettext(PhoenixOauth2Provider.Web.Gettext, "errors", msg)
3338
end

lib/phoenix_oauth2_provider/web/controllers/application_controller.ex

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@ defmodule PhoenixOauth2Provider.ApplicationController do
33
use PhoenixOauth2Provider.Web, :controller
44

55
alias ExOauth2Provider.OauthApplications
6+
alias Plug.Conn
67

8+
@spec index(Conn.t(), map()) :: Conn.t()
79
def index(conn, _params) do
810
applications = conn
911
|> PhoenixOauth2Provider.current_resource_owner()
@@ -12,12 +14,14 @@ defmodule PhoenixOauth2Provider.ApplicationController do
1214
render(conn, "index.html", applications: applications)
1315
end
1416

17+
@spec new(Conn.t(), map()) :: Conn.t()
1518
def new(conn, _params) do
1619
changeset = OauthApplications.change_application(%OauthApplications.OauthApplication{})
1720

1821
render(conn, "new.html", changeset: changeset)
1922
end
2023

24+
@spec create(Conn.t(), map()) :: Conn.t()
2125
def create(conn, %{"oauth_application" => application_params}) do
2226
conn
2327
|> PhoenixOauth2Provider.current_resource_owner()
@@ -33,19 +37,22 @@ defmodule PhoenixOauth2Provider.ApplicationController do
3337
end
3438
end
3539

40+
@spec show(Conn.t(), map()) :: Conn.t()
3641
def show(conn, %{"uid" => uid}) do
3742
application = get_application_for!(conn, uid)
3843

3944
render(conn, "show.html", application: application)
4045
end
4146

47+
@spec edit(Conn.t(), map()) :: Conn.t()
4248
def edit(conn, %{"uid" => uid}) do
4349
application = get_application_for!(conn, uid)
4450
changeset = OauthApplications.change_application(application)
4551

4652
render(conn, "edit.html", application: application, changeset: changeset)
4753
end
4854

55+
@spec update(Conn.t(), map()) :: Conn.t()
4956
def update(conn, %{"uid" => uid, "oauth_application" => application_params}) do
5057
application = get_application_for!(conn, uid)
5158

@@ -60,6 +67,7 @@ defmodule PhoenixOauth2Provider.ApplicationController do
6067
end
6168
end
6269

70+
@spec delete(Conn.t(), map()) :: Conn.t()
6371
def delete(conn, %{"uid" => uid}) do
6472
{:ok, _application} = conn
6573
|> get_application_for!(uid)

lib/phoenix_oauth2_provider/web/controllers/authorization_controller.ex

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@ defmodule PhoenixOauth2Provider.AuthorizationController do
33
use PhoenixOauth2Provider.Web, :controller
44

55
alias ExOauth2Provider.Authorization
6+
alias Plug.Conn
67

8+
@spec new(Conn.t(), map()) :: Conn.t()
79
def new(conn, params) do
810
conn
911
|> PhoenixOauth2Provider.current_resource_owner()
@@ -22,20 +24,23 @@ defmodule PhoenixOauth2Provider.AuthorizationController do
2224
end
2325
end
2426

27+
@spec create(Conn.t(), map()) :: Conn.t()
2528
def create(conn, params) do
2629
conn
2730
|> PhoenixOauth2Provider.current_resource_owner()
2831
|> Authorization.authorize(params)
2932
|> redirect_or_render(conn)
3033
end
3134

35+
@spec delete(Conn.t(), map()) :: Conn.t()
3236
def delete(conn, params) do
3337
conn
3438
|> PhoenixOauth2Provider.current_resource_owner()
3539
|> Authorization.deny(params)
3640
|> redirect_or_render(conn)
3741
end
3842

43+
@spec show(Conn.t(), map()) :: Conn.t()
3944
def show(conn, %{"code" => code}) do
4045
render(conn, "show.html", code: code)
4146
end
@@ -44,7 +49,7 @@ defmodule PhoenixOauth2Provider.AuthorizationController do
4449
redirect(conn, external: redirect_uri)
4550
end
4651
defp redirect_or_render({:native_redirect, payload}, conn) do
47-
json conn, payload
52+
json(conn, payload)
4853
end
4954
defp redirect_or_render({:error, error, status}, conn) do
5055
conn

lib/phoenix_oauth2_provider/web/controllers/authorized_application_controller.ex

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ defmodule PhoenixOauth2Provider.AuthorizedApplicationController do
33
use PhoenixOauth2Provider.Web, :controller
44
alias ExOauth2Provider.OauthApplications
55

6+
@spec index(Conn.t(), map()) :: Conn.t()
67
def index(conn, _params) do
78
applications = conn
89
|> PhoenixOauth2Provider.current_resource_owner()
@@ -11,6 +12,7 @@ defmodule PhoenixOauth2Provider.AuthorizedApplicationController do
1112
render(conn, "index.html", applications: applications)
1213
end
1314

15+
@spec delete(Conn.t(), map()) :: Conn.t()
1416
def delete(conn, %{"uid" => uid}) do
1517
application = OauthApplications.get_application!(uid)
1618
{:ok, _application} = OauthApplications.revoke_all_access_tokens_for(application, PhoenixOauth2Provider.current_resource_owner(conn))

lib/phoenix_oauth2_provider/web/controllers/token_controller.ex

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,28 @@
11
defmodule PhoenixOauth2Provider.TokenController do
22
@moduledoc false
33
use PhoenixOauth2Provider.Web, :controller
4-
54
alias ExOauth2Provider.Token
65

6+
@spec create(Conn.t(), map()) :: Conn.t()
77
def create(conn, params) do
88
case Token.grant(params) do
99
{:ok, access_token} ->
10-
conn
11-
|> json(access_token)
12-
{:error, error, status} ->
10+
json(conn, access_token)
11+
12+
{:error, error, status} ->
1313
conn
1414
|> put_status(status)
1515
|> json(error)
1616
end
1717
end
1818

19+
@spec revoke(Conn.t(), map()) :: Conn.t()
1920
def revoke(conn, params) do
2021
case Token.revoke(params) do
2122
{:ok, response} ->
22-
conn
23-
|> json(response)
24-
{:error, error, status} ->
23+
json(conn, response)
24+
25+
{:error, error, status} ->
2526
conn
2627
|> put_status(status)
2728
|> json(error)

0 commit comments

Comments
 (0)