Skip to content

Commit

Permalink
Compiler warnings (#22)
Browse files Browse the repository at this point in the history
* Updates docs

* Bumps Bypass version to 2.0

* Allow poison version 4

* Updates readme
  • Loading branch information
anfly0 authored Dec 12, 2020
1 parent 082c5fe commit 1112b33
Show file tree
Hide file tree
Showing 4 changed files with 98 additions and 117 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ adding ```{:ex_bank_id, "~> 0.2.1", hex: :exBankID}``` to your list of dependenc
This library depends on an implementation of ```ExBankID.Http.Client``` and ```ExBankID.Json.Handler``` to be available. If no custom implementations are declared in the config or in the opts passed to the functions in ```ExBankID``` the default implementations will be used.
__For the defaults to work__ the add this to your list of dependencies in ```mix.exs```
```elixir
{:poison, "~> 3.1"} # Add this to your deps if you want to use the default json handler
{:poison, "~> 4.0"} # Add this to your deps if you want to use the default json handler
{:httpoison, "~> 1.7"} # Add this to your deps if you want to use the default http client
```

Expand Down
177 changes: 78 additions & 99 deletions lib/ex_bank_id/payload_helpers.ex
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ defmodule ExBankID.PayloadHelpers do
"""

@doc """
Returns {:ok, ip_addres} or {:error, reason} for IP address validity
Returns {:ok, ip_address} or {:error, reason} for IP address validity
## Examples
iex> ExBankID.PayloadHelpers.check_ip_address("1.1.1.1")
Expand All @@ -16,7 +16,7 @@ defmodule ExBankID.PayloadHelpers do
{:error, "Invalid ip address: 345.0.0.0"}
"""
def check_ip_address(ip_address)
when is_binary(ip_address) do
when is_binary(ip_address) do
ip_address_cl = String.to_charlist(ip_address)

case :inet.parse_strict_address(ip_address_cl) do
Expand All @@ -29,17 +29,20 @@ defmodule ExBankID.PayloadHelpers do
end

@doc """
Returns {:ok, personal_number} or {:error, reason} for personal number
Returns {:ok, personal_number}, {:ok, nil} or {:error, reason} for personal number
## Examples
iex> ExBankID.PayloadHelpers.check_personal_number("190000000000")
{:ok, "190000000000"}
iex> ExBankID.PayloadHelpers.check_personal_number("42")
{:error, "Invalid personal number: 42"}
iex> ExBankID.PayloadHelpers.check_personal_number(nil)
{:ok, nil}
"""
def check_personal_number(personal_number)
when is_binary(personal_number) do
when is_binary(personal_number) do
# TODO: Implement relevant personal number check.
case String.length(personal_number) do
12 ->
Expand All @@ -50,36 +53,31 @@ defmodule ExBankID.PayloadHelpers do
end
end

@doc """
Returns {:ok, nil} when personal number is omitted
## Examples
iex> ExBankID.PayloadHelpers.check_personal_number(nil)
{:ok, nil}
"""
def check_personal_number(nil) do
{:ok, nil}
end

@doc """
Returns {:ok, nil} when requirement is omitted
Returns:
* {:ok, nil} when requirement is omitted
* {:ok, requirement} when all requirement's keys are valid
* {:error, "Invalid requirement"} when requirement is anything besides a map or nil
## Examples
iex> ExBankID.PayloadHelpers.check_requirement(nil)
{:ok, nil}
"""
def check_requirement(nil), do: {:ok, nil}

@doc """
Returns {:ok, requirement} when all requirement's keys are valid
## Examples
iex> ExBankID.PayloadHelpers.check_requirement(%{allowFingerprint: :true, cardReader: "class1"})
{:ok, %{allowFingerprint: :true, cardReader: "class1"}}
iex> ExBankID.PayloadHelpers.check_requirement(%{tokenStartRequired: :false, notRealRequirement: "fails"})
{:error, "Invalid requirement"}
iex> ExBankID.PayloadHelpers.check_requirement("something")
{:error, "Invalid requirement"}
"""
def check_requirement(nil), do: {:ok, nil}

def check_requirement(%{} = requirement) do
if Enum.all?(
Enum.map(
Expand All @@ -94,69 +92,73 @@ defmodule ExBankID.PayloadHelpers do
end
end

@doc """
Returns {:ok, nil} when requirement is anything besides a map or nil
## Examples
iex> ExBankID.PayloadHelpers.check_requirement("something")
{:error, "Invalid requirement"}
"""
def check_requirement(_), do: {:error, "Invalid requirement"}

def check_requirement(:allowFingerprint, value) when is_boolean(value), do: {:ok, nil}

@deprecated "Will not be possible to use in future versions of the RP API. Use tokenStartRequired."
@doc """
Returns {:ok, nil} when autoStartTokenRequired is a boolean
Returns
* {:ok, nil} when autoStartTokenRequired is a boolean
* {:ok, nil} when cardReader is "class1" or "class2"
* {:error, "Invalid requirement"} when certificatePolicies is an empty list
* {:ok, nil} when certificatePolicies is a list of oid's
* {:error, "Invalid requirement"} when issuerCn is an empty list
* {:ok, nil} when issuerCn is a list of strings
* {:ok, nil} when tokenStartRequired is a boolean
* {:error, "Invalid requirement"} when key, value is anything else besides above permitted
## Examples
iex> ExBankID.PayloadHelpers.check_requirement(:autoStartTokenRequired, :false)
{:ok, nil}
iex> ExBankID.PayloadHelpers.check_requirement(:autoStartTokenRequired, "true")
{:error, "Invalid requirement"}
"""
def check_requirement(:autoStartTokenRequired, value) when is_boolean(value), do: {:ok, nil}

@doc """
Returns {:ok, nil} when cardReader is "class1" or "class2"
## Examples
iex> ExBankID.PayloadHelpers.check_requirement(:cardReader, "class1")
{:ok, nil}
iex> ExBankID.PayloadHelpers.check_requirement(:cardReader, "class3")
{:error, "Invalid requirement"}
"""
def check_requirement(:cardReader, value) when is_binary(value) do
case value do
"class1" -> {:ok, nil}
"class2" -> {:ok, nil}
_ -> {:error, "Invalid requirement"}
end
end
@doc """
Returns {:error, "Invalid requirement"} when certificatePolicies is an empty list
iex> ExBankID.PayloadHelpers.check_requirement(:certificatePolicies, [])
{:error, "Invalid requirement"}
iex> ExBankID.PayloadHelpers.check_requirement(:certificatePolicies, ["1.2.752.78.*", "1.2.752.78.1.5"])
{:ok, nil}
iex> ExBankID.PayloadHelpers.check_requirement(:certificatePolicies, ["not an oid"])
{:error, "Invalid requirement"}
## Examples
iex> ExBankID.PayloadHelpers.check_requirement(:certificatePolicies, [])
{:error, "Invalid requirement"}
"""
def check_requirement(:certificatePolicies, []), do: {:error, "Invalid requirement"}
@doc """
Returns {:ok, nil} when certificatePolicies is a list of oid's
iex> ExBankID.PayloadHelpers.check_requirement(:issuerCn, ["Nordea CA for Smartcard users 12", "Nordea Test CA for Softcert users 13"])
{:ok, nil}
One wildcard "*" is allowed from position 5 and forward ie. 1.2.752.78.*
iex> ExBankID.PayloadHelpers.check_requirement(:issuerCn, "Nordea Test CA for Softcert users 13")
{:error, "Invalid requirement"}
## Examples
iex> ExBankID.PayloadHelpers.check_requirement(:certificatePolicies, ["1.2.752.78.*", "1.2.752.78.1.5"])
iex> ExBankID.PayloadHelpers.check_requirement(:tokenStartRequired, :false)
{:ok, nil}
iex> ExBankID.PayloadHelpers.check_requirement(:certificatePolicies, ["not an oid"])
iex> ExBankID.PayloadHelpers.check_requirement(:tokenStartRequired, "true")
{:error, "Invalid requirement"}
iex> ExBankID.PayloadHelpers.check_requirement(:foo, "bar")
{:error, "Invalid requirement"}
"""
def check_requirement(:autoStartTokenRequired, value) when is_boolean(value), do: {:ok, nil}

def check_requirement(:cardReader, value) when is_binary(value) do
case value do
"class1" -> {:ok, nil}
"class2" -> {:ok, nil}
_ -> {:error, "Invalid requirement"}
end
end

def check_requirement(:certificatePolicies, []), do: {:error, "Invalid requirement"}

def check_requirement(:certificatePolicies, value) when is_list(value) do
if Enum.all?(value, fn oid -> oid?(oid) end) do
{:ok, nil}
Expand All @@ -165,25 +167,8 @@ defmodule ExBankID.PayloadHelpers do
end
end

@doc """
Returns {:error, "Invalid requirement"} when issuerCn is an empty list
## Examples
iex> ExBankID.PayloadHelpers.check_requirement(:certificatePolicies, [])
{:error, "Invalid requirement"}
"""
def check_requirement(:issuerCn, []), do: {:error, "Invalid requirement"}

@doc """
Returns {:ok, nil} when issuerCn is a list of strings
## Examples
iex> ExBankID.PayloadHelpers.check_requirement(:issuerCn, ["Nordea CA for Smartcard users 12", "Nordea Test CA for Softcert users 13"])
{:ok, nil}
iex> ExBankID.PayloadHelpers.check_requirement(:issuerCn, "Nordea Test CA for Softcert users 13")
{:error, "Invalid requirement"}
"""
def check_requirement(:issuerCn, value) when is_list(value) do
if Enum.all?(value, fn cn -> is_binary(cn) end) do
{:ok, nil}
Expand All @@ -192,25 +177,8 @@ defmodule ExBankID.PayloadHelpers do
end
end

@doc """
Returns {:ok, nil} when tokenStartRequired is a boolean
## Examples
iex> ExBankID.PayloadHelpers.check_requirement(:tokenStartRequired, :false)
{:ok, nil}
iex> ExBankID.PayloadHelpers.check_requirement(:tokenStartRequired, "true")
{:error, "Invalid requirement"}
"""
def check_requirement(:tokenStartRequired, value) when is_boolean(value), do: {:ok, nil}

@doc """
Returns {:error, "Invalid requirement"} when key, value is anything else besides above permitted
## Examples
iex> ExBankID.PayloadHelpers.check_requirement(:foo, "bar")
{:error, "Invalid requirement"}
"""
def check_requirement(_, _), do: {:error, "Invalid requirement"}

@doc """
Expand All @@ -219,35 +187,46 @@ defmodule ExBankID.PayloadHelpers do
One wildcard "*" is allowed from position 5 and forward ie. 1.2.752.78.*
## Examples
iex> ExBankID.PayloadHelpers.oid?("1.2.752.78.*")
iex> ExBankID.PayloadHelpers.oid?("1.2.752.78.1.1")
true
iex> ExBankID.PayloadHelpers.oid?("1.2.3.4.5.6.7.8.9.10.11.13")
iex> ExBankID.PayloadHelpers.oid?("1.2.752.78.1.2")
true
iex> ExBankID.PayloadHelpers.oid?("1.2.3.4.5.6.7.8.9.10.11.13.14")
false
iex> ExBankID.PayloadHelpers.oid?("1.2.752.78.1.5")
true
iex> ExBankID.PayloadHelpers.oid?("1.2.752.71.1.3")
true
iex> ExBankID.PayloadHelpers.oid?("1.2.3.4.5")
true
iex> ExBankID.PayloadHelpers.oid?("1.2.3.4.*")
iex> ExBankID.PayloadHelpers.oid?("1.2.3.4.10")
true
iex> ExBankID.PayloadHelpers.oid?("1.2.3.*")
iex> ExBankID.PayloadHelpers.oid?("1.2.3.4.25")
true
iex> ExBankID.PayloadHelpers.oid?("1.2.752.71.1.3")
true
iex> ExBankID.PayloadHelpers.oid?("1.2.752.60.1.6")
true
iex> ExBankID.PayloadHelpers.oid?("1.2.752.*.1.6")
false
iex> ExBankID.PayloadHelpers.oid?("1.2.752.60.*")
true
iex> ExBankID.PayloadHelpers.oid?("1.2.752.60.1.*")
true
"""
def oid?(value) when is_binary(value) do
value =~ ~r/^([1-9][0-9]{0,3}|0)(\.([1-9][0-9]{0,3}|0)){5,13}$/ or
value =~ ~r/^([1-9][0-9]{0,3}|0)(\.([1-9][0-9]{0,3}|0)){3}(\.([1-9][0-9]{0,3}|0|\*)){0,9}$/
end

@doc """
Returns false when arg is anything other than a string
## Examples
iex> ExBankID.PayloadHelpers.oid?(:foo)
false
"""
def oid?(_), do: false
end
4 changes: 2 additions & 2 deletions mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,11 @@ defmodule ExBankID.MixProject do
defp deps do
[
{:httpoison, "~> 1.7", optional: true},
{:poison, "~> 3.1", optional: true},
{:poison, "~> 4.0 or ~> 3.1", optional: true},
{:uuid, "~> 1.1"},
{:credo, "~> 1.4", only: [:dev], runtime: false},
{:dialyxir, "~> 1.0", only: [:dev], runtime: false},
{:bypass, "~> 1.0", only: :test},
{:bypass, "~> 2.0", only: :test},
{:excoveralls, "~> 0.10", only: :test},
{:ex_doc, ">= 0.0.0", only: :dev, runtime: false},
{:nimble_options, "~> 0.3.0"}
Expand Down
Loading

0 comments on commit 1112b33

Please sign in to comment.