Skip to content
This repository has been archived by the owner on Oct 30, 2023. It is now read-only.

Commit

Permalink
fix: minor refactorings
Browse files Browse the repository at this point in the history
  • Loading branch information
madclaws committed Oct 29, 2023
1 parent f6ddfd8 commit c9b41c0
Show file tree
Hide file tree
Showing 8 changed files with 31 additions and 21 deletions.
8 changes: 4 additions & 4 deletions lib/ex_ucan.ex
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,18 @@ defmodule ExUcan do
@moduledoc """
Documentation for `ExUcan`.
"""
alias ExUcan.Keymaterial.Ed25519.Keypair
alias ExUcan.Core.Token
alias ExUcan.Core.Structs.Ucan
alias ExUcan.Core.Token
alias ExUcan.Keymaterial.Ed25519.Keypair

@doc """
Creates a default keypair with EdDSA algorithm
This keypair can be later used for create UCAN tokens
Keypair generated with different algorithms like RSA will be coming soon..
"""
@spec create_default_keypair() :: Keypair.t()
def create_default_keypair() do
@spec create_default_keypair :: Keypair.t()
def create_default_keypair do
Keypair.create()
end

Expand Down
6 changes: 3 additions & 3 deletions lib/ex_ucan/builder.ex
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ defmodule ExUcan.Builder do
@moduledoc """
Builder functions for UCAN tokens
"""
alias ExUcan.Core.Capability
alias ExUcan.Core.Structs.UcanPayload
alias ExUcan.Core.Token
alias ExUcan.Keymaterial.Ed25519.Keypair
alias ExUcan.Core.Capability

@type t :: %__MODULE__{
issuer: Keypair,
Expand Down Expand Up @@ -38,8 +38,8 @@ defmodule ExUcan.Builder do
- `with_lifetime` or `with_expiration`.
To finalise the builder, call its `build` or `build_parts` method.
"""
@spec default() :: __MODULE__.t()
def default() do
@spec default :: __MODULE__.t()
def default do
%__MODULE__{
issuer: nil,
audience: nil,
Expand Down
10 changes: 5 additions & 5 deletions lib/ex_ucan/core/token.ex
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@ defmodule ExUcan.Core.Token do
@moduledoc """
Creates and manages UCAN tokens
"""
alias ExUcan.Keymaterial.Ed25519.Keypair
alias ExUcan.Core.Utils
alias ExUcan.Builder
alias ExUcan.Keymaterial.Ed25519.Crypto
alias ExUcan.Core.Structs.UcanHeader
alias ExUcan.Keymaterial
alias ExUcan.Core.Structs.Ucan
alias ExUcan.Core.Structs.UcanHeader
alias ExUcan.Core.Structs.UcanPayload
alias ExUcan.Core.Utils
alias ExUcan.Keymaterial
alias ExUcan.Keymaterial.Ed25519.Crypto
alias ExUcan.Keymaterial.Ed25519.Keypair

@token_type "JWT"
@version %{major: 0, minor: 10, patch: 0}
Expand Down
10 changes: 9 additions & 1 deletion lib/ex_ucan/core/utils.ex
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,15 @@ defmodule ExUcan.Core.Utils do
"""
@chars "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"

# TODO: docs
@doc """
Generate random string to use as a nonce
## Examples
iex> ExUcan.Core.Utils.generate_nonce() |> String.length
6
iex> ExUcan.Core.Utils.generate_nonce(10) |> String.length
10
"""
def generate_nonce(len \\ 6)

def generate_nonce(len) do
Expand Down
4 changes: 2 additions & 2 deletions lib/ex_ucan/keymaterial/ed25519/keypair.ex
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ defmodule ExUcan.Keymaterial.Ed25519.Keypair do
This keypair can be later used for create UCAN tokens
"""
@spec create() :: __MODULE__.t()
def create() do
@spec create :: __MODULE__.t()
def create do
{pub, priv} = :crypto.generate_key(:eddsa, :ed25519)

%__MODULE__{
Expand Down
2 changes: 1 addition & 1 deletion test/builder_test.exs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
defmodule BuilderTest do
alias ExUcan.Builder
alias ExUcan.Core.Capability
alias ExUcan.Core.Structs.UcanPayload
alias ExUcan.Builder
use ExUnit.Case

setup do
Expand Down
10 changes: 6 additions & 4 deletions test/ex_ucan_test.exs
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
defmodule ExUcanTest do
alias ExUcan.Builder
alias ExUcan.Core.Utils
alias ExUcan.Keymaterial.Ed25519.Keypair
use ExUnit.Case
doctest ExUcan
doctest Utils

setup do
keypair = ExUcan.create_default_keypair()
Expand All @@ -26,7 +28,7 @@ defmodule ExUcanTest do
|> ExUcan.sign(meta.keypair)
|> ExUcan.encode()

assert :ok = ExUcan.validate_token(token)
assert :ok = ExUcan.validate_token(token)
end

@tag :exucan
Expand All @@ -40,7 +42,7 @@ defmodule ExUcanTest do
|> ExUcan.sign(meta.keypair)
|> ExUcan.encode()

assert {:error, "Ucan token is already expired"} = ExUcan.validate_token(token)
assert {:error, "Ucan token is already expired"} = ExUcan.validate_token(token)
end

@tag :exucan
Expand All @@ -50,11 +52,11 @@ defmodule ExUcanTest do
|> Builder.issued_by(meta.keypair)
|> Builder.for_audience("did:key:z6MkwDK3M4PxU1FqcSt4quXghquH1MoWXGzTrNkNWTSy2NLD")
|> Builder.with_expiration((DateTime.utc_now() |> DateTime.to_unix()) + 86_400)
|> Builder.not_before((DateTime.utc_now() |> DateTime.to_unix()) + (div(86400, 2)))
|> Builder.not_before((DateTime.utc_now() |> DateTime.to_unix()) + div(86_400, 2))
|> Builder.build!()
|> ExUcan.sign(meta.keypair)
|> ExUcan.encode()

assert {:error, "Ucan token is not yet active"} = ExUcan.validate_token(token)
assert {:error, "Ucan token is not yet active"} = ExUcan.validate_token(token)
end
end
2 changes: 1 addition & 1 deletion test/keymaterial/keypair_test.exs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
defmodule Keymaterial.KeypairTest do
alias ExUcan.Keymaterial.Ed25519.Keypair
alias ExUcan.Keymaterial
alias ExUcan.Keymaterial.Ed25519.Keypair
use ExUnit.Case

test "creating edDSA keypair" do
Expand Down

0 comments on commit c9b41c0

Please sign in to comment.