Mooncore.Auth.Token (mooncore v0.2.4)

Copy Markdown

JWT token creation and verification.

Uses RS256 (RSA) signing via Joken. Configuration is read from:

config :mooncore,
  jwt: [
    key: "-----BEGIN PRIVATE KEY-----\n...\n-----END PRIVATE KEY-----",
    issuer: "myapp"
  ]

Token Claims

Tokens contain:

  • "user" — user identifier
  • "app" — app key (for routing to correct action module)
  • "dkey" — domain/tenant key
  • "scope" — scope for data isolation
  • "roles" — Base58-encoded bitmask of roles
  • "aud" — audience ("api")
  • "iss" — issuer (from config)
  • "exp" — expiry (18 hours default)

Summary

Functions

Combines generate_claims/1 and encode_and_sign/2

Create a new token with claims. Returns {:ok, token} or {:error, reason}.

Create a new token with role bitmask encoding.

Verify and decode a JWT token.

Functions

create(claims \\ %{})

generate_and_sign(extra_claims \\ %{}, key \\ __default_signer__())

@spec generate_and_sign(Joken.claims(), Joken.signer_arg()) ::
  {:ok, Joken.bearer_token(), Joken.claims()} | {:error, Joken.error_reason()}

Combines generate_claims/1 and encode_and_sign/2

generate_and_sign!(extra_claims \\ %{}, key \\ __default_signer__())

@spec generate_and_sign!(Joken.claims(), Joken.signer_arg()) :: Joken.bearer_token()

Same as generate_and_sign/2 but raises if error

new_token(claims \\ %{})

Create a new token with claims. Returns {:ok, token} or {:error, reason}.

new_token(claims, app_roles, client_roles)

Create a new token with role bitmask encoding.

app_roles is the ordered list of all possible roles for the app. client_roles is the list of roles this user has.

signer()

solve(token)

Verify and decode a JWT token.

Returns {:ok, claims} with roles decoded from bitmask back to string list, or {:error, reason}.

verify_and_validate(bearer_token, key \\ __default_signer__(), context \\ %{})

@spec verify_and_validate(Joken.bearer_token(), Joken.signer_arg(), term()) ::
  {:ok, Joken.claims()} | {:error, Joken.error_reason()}

Combines verify/2 and validate/2

verify_and_validate!(bearer_token, key \\ __default_signer__(), context \\ %{})

@spec verify_and_validate!(Joken.bearer_token(), Joken.signer_arg(), term()) ::
  Joken.claims()

Same as verify_and_validate/2 but raises if error