-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #7 from amezcua/develop
Develop
- Loading branch information
Showing
10 changed files
with
136 additions
and
35 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
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,3 @@ | ||
defmodule Jwt.TimeUtils do | ||
def get_system_time(), do: :os.system_time(:seconds) | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,57 +1,118 @@ | ||
defmodule JwtPlugTest do | ||
use ExUnit.Case, async: true | ||
use Plug.Test | ||
require Logger | ||
|
||
@opts Jwt.Plug.init([]) | ||
@op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HamWxs9D83c4JpOsioNOMb6-LBinzOg7qdxr1D4NvHD6VSXBTbyXiOBjK2elLU1iCz_Hz_BH-R1IYCdTRr5PczRWdSCgoTdZ7ds1nTTglfuXlGNbaEhhzsFxX8OCR4uNK6vbWXQ" | ||
@test_token_exp_value 1473229464 # Expiration timestamp for the token avobe | ||
@ten_minutes 10 * 60 | ||
@four_minutes 4 * 60 | ||
|
||
test "Missing authorization header returns 401" do | ||
Application.put_env(:jwt, :current_time_for_test, :os.system_time(:seconds)) | ||
conn = conn(:get, "/protected") | ||
|
||
conn = Jwt.Plug.call(conn, @opts) | ||
conn = Jwt.Plug.call(conn, Jwt.Plug.init(@opts)) | ||
|
||
assert conn.state == :sent | ||
assert conn.status == 401 | ||
assert conn.resp_body == "" | ||
end | ||
|
||
test "Empty authorization header returns 401" do | ||
conn = conn(:get, "/protected") | ||
conn = put_req_header conn, "authorization", "" | ||
Application.put_env(:jwt, :current_time_for_test, :os.system_time(:seconds)) | ||
conn = conn(:get, "/protected") | ||
conn = put_req_header conn, "authorization", "" | ||
|
||
conn = Jwt.Plug.call(conn, @opts) | ||
conn = Jwt.Plug.call(conn, Jwt.Plug.init(@opts)) | ||
|
||
assert conn.state == :sent | ||
assert conn.status == 401 | ||
assert conn.resp_body == "" | ||
assert conn.state == :sent | ||
assert conn.status == 401 | ||
assert conn.resp_body == "" | ||
end | ||
|
||
test "Invalid token in authorization header returns 401" do | ||
conn = conn(:get, "/protected") | ||
conn = put_req_header conn, "authorization", "token" | ||
Application.put_env(:jwt, :current_time_for_test, :os.system_time(:seconds)) | ||
conn = conn(:get, "/protected") | ||
conn = put_req_header conn, "authorization", "token" | ||
|
||
conn = Jwt.Plug.call(conn, @opts) | ||
conn = Jwt.Plug.call(conn, Jwt.Plug.init(@opts)) | ||
|
||
assert conn.state == :sent | ||
assert conn.status == 401 | ||
assert conn.resp_body == "" | ||
assert conn.state == :sent | ||
assert conn.status == 401 | ||
assert conn.resp_body == "" | ||
end | ||
|
||
test "Val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9D83c4JpOsioNOMb6-LBinzOg7qdxr1D4NvHD6VSXBTbyXiOBjK2elLU1iCz_Hz_BH-R1IYCdTRr5PczRWdSCgoTdZ7ds1nTTglfuXlGNbaEhhzsFxX8OCR4uNK6vbWXQ" | ||
valid_token = header <> "." <> claims <> "." <> signature | ||
|
||
Application.put_env(:jwt, :current_time_for_test, :os.system_time(:seconds)) | ||
valid_token = @test_header <> "." <> @test_claims <> "." <> @test_signature | ||
auth_header= "Bearer " <> valid_token | ||
|
||
conn = conn(:get, "/protected") | ||
conn = put_req_header conn, "authorization", auth_header | ||
conn = Jwt.Plug.call(conn, Jwt.Plug.init([true, 5 * 60])) | ||
|
||
claims = conn.assigns[:jwtclaims] | ||
assert claims != nil | ||
assert claims["name"] == "Alejandro Mezcua" | ||
end | ||
|
||
test "Expired token is rejected by default" do | ||
Application.put_env(:jwt, :current_time_for_test, :os.system_time(:seconds)) | ||
expired_token = @test_header <> "." <> @test_claims <> "." <> @test_signature | ||
auth_header= "Bearer " <> expired_token | ||
|
||
conn = conn(:get, "/protected") | ||
conn = put_req_header conn, "authorization", auth_header | ||
conn = Jwt.Plug.call(conn, Jwt.Plug.init(@opts)) | ||
|
||
assert conn.state == :sent | ||
assert conn.status == 401 | ||
assert conn.resp_body == "" | ||
end | ||
|
||
test "Token expiration allowed below but outside time window" do | ||
Application.put_env(:jwt, :current_time_for_test, @test_token_exp_value - @ten_minutes) | ||
expired_in_window_token = @test_header <> "." <> @test_claims <> "." <> @test_signature | ||
auth_header= "Bearer " <> expired_in_window_token | ||
|
||
conn = Jwt.Plug.call(conn, @opts) | ||
conn = conn(:get, "/protected") | ||
conn = put_req_header conn, "authorization", auth_header | ||
conn = Jwt.Plug.call(conn, Jwt.Plug.init(@opts)) | ||
|
||
claims = conn.assigns[:jwtclaims] | ||
Logger.debug fn -> "Claims: " <> inspect(claims) end | ||
assert claims != nil | ||
assert claims["name"] == "Alejandro Mezcua" | ||
end | ||
|
||
test "Token expiration not allowed below but within time window" do | ||
Application.put_env(:jwt, :current_time_for_test, @test_token_exp_value - @four_minutes) | ||
expired_in_window_token = @test_header <> "." <> @test_claims <> "." <> @test_signature | ||
auth_header= "Bearer " <> expired_in_window_token | ||
|
||
conn = conn(:get, "/protected") | ||
conn = put_req_header conn, "authorization", auth_header | ||
conn = Jwt.Plug.call(conn, Jwt.Plug.init(@opts)) | ||
|
||
assert conn.state == :sent | ||
assert conn.status == 401 | ||
assert conn.resp_body == "" | ||
end | ||
|
||
test "Token expiration not allowed avobe expiration time" do | ||
Application.put_env(:jwt, :current_time_for_test, @test_token_exp_value + 1) | ||
expired_in_window_token = @test_header <> "." <> @test_claims <> "." <> @test_signature | ||
auth_header= "Bearer " <> expired_in_window_token | ||
|
||
conn = conn(:get, "/protected") | ||
conn = put_req_header conn, "authorization", auth_header | ||
conn = Jwt.Plug.call(conn, Jwt.Plug.init(@opts)) | ||
|
||
assert conn.state == :sent | ||
assert conn.status == 401 | ||
assert conn.resp_body == "" | ||
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,5 @@ | ||
defmodule Jwt.TimeUtils.Mock do | ||
|
||
def get_system_time(), do: Application.get_env(:jwt, :current_time_for_test) | ||
|
||
end |