Skip to content

Commit d27b1ef

Browse files
authored
Merge pull request #1248 from code-corps/update-guardian
Update guardian
2 parents 13e49ff + d2a42d0 commit d27b1ef

File tree

54 files changed

+176
-151
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

54 files changed

+176
-151
lines changed

config/config.exs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,11 @@ config :mime, :types, %{
3131
"application/vnd.api+json" => ["json-api"]
3232
}
3333

34-
config :guardian, Guardian,
34+
config :code_corps, CodeCorps.Guardian,
3535
issuer: "CodeCorps",
3636
ttl: { 30, :days },
3737
verify_issuer: true, # optional
38-
secret_key: System.get_env("GUARDIAN_SECRET_KEY"),
39-
serializer: CodeCorpsWeb.GuardianSerializer
38+
secret_key: System.get_env("GUARDIAN_SECRET_KEY")
4039

4140
# Configures ex_aws with credentials
4241
config :ex_aws, :code_corps,

config/dev.exs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ config :code_corps, CodeCorps.Repo,
4545
# CORS allowed origins
4646
config :code_corps, allowed_origins: ["http://localhost:4200"]
4747

48-
config :guardian, Guardian,
48+
config :code_corps, CodeCorps.Guardian,
4949
secret_key: "e62fb6e2746f6b1bf8b5b735ba816c2eae1d5d76e64f18f3fc647e308b0c159e"
5050

5151
config :code_corps, :analytics, CodeCorps.Analytics.InMemoryAPI

config/prod.exs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ config :code_corps, allowed_origins: [
3636
"https://www.codecorps.org"
3737
]
3838

39-
config :guardian, Guardian,
39+
config :code_corps, CodeCorps.Guardian,
4040
secret_key: System.get_env("GUARDIAN_SECRET_KEY")
4141

4242
# Timber logging

config/remote-development.exs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ config :code_corps, CodeCorps.Repo,
2828
# CORS allowed origins
2929
config :code_corps, allowed_origins: "*"
3030

31-
config :guardian, Guardian,
31+
config :code_corps, CodeCorps.Guardian,
3232
secret_key: System.get_env("GUARDIAN_SECRET_KEY")
3333

3434
# Do not print debug messages in production

config/staging.exs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ config :code_corps, allowed_origins: [
3535
"https://www.pbqrpbecf.org"
3636
]
3737

38-
config :guardian, Guardian,
38+
config :code_corps, CodeCorps.Guardian,
3939
secret_key: System.get_env("GUARDIAN_SECRET_KEY")
4040

4141
# Timber logging

config/test.exs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ config :comeonin, :pbkdf2_rounds, 1
2727
# CORS allowed origins
2828
config :code_corps, allowed_origins: ["http://localhost:4200"]
2929

30-
config :guardian, Guardian,
30+
config :code_corps, CodeCorps.Guardian,
3131
secret_key: "e62fb6e2746f6b1bf8b5b735ba816c2eae1d5d76e64f18f3fc647e308b0c159e"
3232

3333
config :code_corps, :analytics, CodeCorps.Analytics.TestAPI
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
defmodule CodeCorps.Auth.BearerAuthPipeline do
2+
use Guardian.Plug.Pipeline, otp_app: :code_corps,
3+
module: CodeCorps.Guardian,
4+
error_handler: CodeCorps.Auth.ErrorHandler
5+
6+
plug Guardian.Plug.VerifyHeader, realm: "Bearer"
7+
plug Guardian.Plug.LoadResource, allow_blank: true
8+
end
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
defmodule CodeCorps.Auth.EnsureAuthPipeline do
2+
use Guardian.Plug.Pipeline, otp_app: :code_corps,
3+
module: CodeCorps.Guardian,
4+
error_handler: CodeCorps.Auth.ErrorHandler
5+
6+
plug Guardian.Plug.EnsureAuthenticated
7+
end
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
defmodule CodeCorps.Auth.ErrorHandler do
2+
use CodeCorpsWeb, :controller
3+
4+
def auth_error(conn, {type, _reason}, _opts) do
5+
conn
6+
|> put_status(401)
7+
|> render(CodeCorpsWeb.TokenView, "401.json", message: to_string(type))
8+
end
9+
end

lib/code_corps/guardian.ex

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
defmodule CodeCorps.Guardian do
2+
use Guardian, otp_app: :code_corps
3+
4+
alias CodeCorps.{Project, Repo, User}
5+
6+
def subject_for_token(project = %Project{}, _claims) do
7+
{:ok, "Project:#{project.id}"}
8+
end
9+
def subject_for_token(user = %User{}, _claims) do
10+
{:ok, "User:#{user.id}"}
11+
end
12+
def subject_for_token(_, _) do
13+
{:error, :unknown_resource_type}
14+
end
15+
16+
def resource_from_claims(%{"sub" => sub}), do: resource_from_subject(sub)
17+
def resource_from_claims(_), do: {:error, :missing_subject}
18+
19+
defp resource_from_subject("Project:" <> id), do: {:ok, Repo.get(Project, id)}
20+
defp resource_from_subject("User:" <> id) do
21+
user = Repo.get(User, id)
22+
23+
if user do
24+
name = full_name(user)
25+
%Timber.Contexts.UserContext{id: user.id, email: user.email, name: name}
26+
|> Timber.add_context()
27+
end
28+
29+
{:ok, user}
30+
end
31+
defp resource_from_subject(_), do: {:error, :unknown_resource_type}
32+
33+
defp full_name(%User{first_name: nil, last_name: nil}), do: ""
34+
defp full_name(%User{first_name: first_name, last_name: nil}), do: first_name
35+
defp full_name(%User{first_name: nil, last_name: last_name}), do: last_name
36+
defp full_name(%User{first_name: first_name, last_name: last_name}) do
37+
first_name <> " " <> last_name
38+
end
39+
defp full_name(_), do: ""
40+
end

0 commit comments

Comments
 (0)