Current section
Files
Jump to
Current section
Files
lib/vault/auth/token.ex
defmodule Vault.Auth.Token do
@moduledoc """
Token Auth Adapter. Checks a provided token for validity, and saves if valid. Useful
for local dev, or writing a CLI that uses the `.vault-token` file in the home
directory.
[Vault Docs](https://www.vaultproject.io/api/auth/token/index.html#lookup-a-token-self-)
"""
@behaviour Vault.Auth.Adapter
@doc """
Log in with an existing vault token. Auth path not required.
## Examples
```
{:ok, token, ttl} = Vault.Auth.Token.login(vault, %{token: local_token})
```
"""
@impl true
def login(vault, params)
def login(%Vault{} = vault, %{token: token}) do
headers = [
{"X-Vault-Token", token},
{"Content-Type", "application/json"}
]
url = "auth/token/lookup-self"
with {:ok, body} <- Vault.HTTP.get(vault, url, headers: headers) do
case body do
%{"errors" => messages} ->
{:error, messages}
%{"data" => %{"id" => token, "ttl" => ttl}} ->
{:ok, token, ttl}
otherwise ->
{:error, ["Unexpected response from vault.", inspect(otherwise)]}
end
else
{:error, response} ->
{:error, ["Http adapter error", inspect(response)]}
end
end
end