-
-
Notifications
You must be signed in to change notification settings - Fork 319
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add channels auth via JWT verification (#99)
- Loading branch information
Showing
16 changed files
with
537 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,16 @@ | ||
use Mix.Config | ||
|
||
config :realtime, | ||
secure_channels: false | ||
|
||
# We don't run a server during test. If one is required, | ||
# you can enable the server option below. | ||
config :realtime, RealtimeWeb.Endpoint, | ||
http: [port: 4002], | ||
server: false | ||
|
||
config :joken, | ||
current_time_adapter: RealtimeWeb.Joken.CurrentTime.Mock | ||
|
||
# Print only warnings and errors during test | ||
config :logger, level: :warn |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
15 changes: 15 additions & 0 deletions
15
server/lib/realtime_web/channels/auth/channels_authorization.ex
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
defmodule RealtimeWeb.ChannelsAuthorization do | ||
alias RealtimeWeb.JwtVerification | ||
|
||
def authorize(token) when is_binary(token) do | ||
token | ||
|> clean_token() | ||
|> JwtVerification.verify() | ||
end | ||
|
||
def authorize(_token), do: :error | ||
|
||
defp clean_token(token) do | ||
Regex.replace(~r/\s|\n/, URI.decode(token), "") | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
defmodule RealtimeWeb.JwtVerification do | ||
defmodule JwtAuthToken do | ||
use Joken.Config | ||
|
||
@impl true | ||
def token_config do | ||
Application.fetch_env!(:realtime, :jwt_claim_validators) | ||
|> Enum.reduce(%{}, fn {claim_key, expected_val}, claims -> | ||
add_claim_validator(claims, claim_key, expected_val) | ||
end) | ||
|> add_claim_validator("exp") | ||
end | ||
|
||
defp add_claim_validator(claims, "exp") do | ||
add_claim(claims, "exp", nil, &(&1 > current_time())) | ||
end | ||
|
||
defp add_claim_validator(claims, claim_key, expected_val) do | ||
add_claim(claims, claim_key, nil, &(&1 == expected_val)) | ||
end | ||
end | ||
|
||
@hs_algorithms ["HS256", "HS384", "HS512"] | ||
|
||
def verify(token) when is_binary(token) do | ||
with :ok <- check_claims_format(token), | ||
{:ok, header} <- check_header_format(token), | ||
{:ok, signer} <- generate_signer(header) do | ||
JwtAuthToken.verify_and_validate(token, signer) | ||
else | ||
_ -> :error | ||
end | ||
end | ||
|
||
def verify(_token), do: :error | ||
|
||
defp check_header_format(token) do | ||
case Joken.peek_header(token) do | ||
{:ok, header} when is_map(header) -> {:ok, header} | ||
_ -> :error | ||
end | ||
end | ||
|
||
defp check_claims_format(token) do | ||
case Joken.peek_claims(token) do | ||
{:ok, claims} when is_map(claims) -> :ok | ||
_ -> :error | ||
end | ||
end | ||
|
||
defp generate_signer(%{"typ" => "JWT", "alg" => alg}) when alg in @hs_algorithms do | ||
{:ok, | ||
Joken.Signer.create( | ||
alg, | ||
Application.fetch_env!(:realtime, :jwt_secret) | ||
)} | ||
end | ||
|
||
defp generate_signer(_header), do: :error | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.