Skip to content

Commit

Permalink
Merge pull request #5 from resuelve/feature/return-error-invalid-token
Browse files Browse the repository at this point in the history
Devolver un error cuando el token sea inválido
  • Loading branch information
jaimerodas authored Jul 25, 2018
2 parents ba36d68 + 666e6c2 commit 47eb46d
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 8 deletions.
13 changes: 5 additions & 8 deletions lib/helpers/token_helper.ex
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,11 @@ defmodule ResuelveAuth.Helpers.TokenHelper do
"""
@spec verify_token(String.t, String.t) :: tuple
def verify_token(token, secret) do
[data, sign] = String.split(token, ".")
valid_sign = sign_data(data, secret)

case String.equivalent?(sign, valid_sign) do
true ->
parse_token_data(data)
false ->
{:error, "Unauthorized"}
with [data, sign] <- String.split(token, "."),
true <- String.equivalent?(sign, sign_data(data, secret)) do
parse_token_data(data)
else
_ -> {:error, "Unauthorized"}
end
end

Expand Down
6 changes: 6 additions & 0 deletions test/resuelve_auth_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,10 @@ defmodule ResuelveAuthTest do
"session" => "session",
"timestamp" => "timestamp"}}
end

test "Verify invalid token" do
assert TokenHelper.verify_token("invalid_token", "secret") == {
:error,
"Unauthorized"}
end
end

0 comments on commit 47eb46d

Please sign in to comment.