Add the jerl Erlang library in mix.exs file
{:jwerl, "~> 1.1"}
defmodule GithubJwt do
def github_api_test do
jwt = create_jwt("/path/to/pemfile", APP_ID)
test_token(jwt)
end
defp test_token(jwt) do
Application.ensure_all_started(:inets)
Application.ensure_all_started(:ssl)
auth_header_val = to_charlist("Bearer #{jwt}")
request =
{'https://api.github.com/app',
[
{'Authorization', auth_header_val},
{'Accept', 'application/vnd.github.machine-man-preview+json'},
{'User-Agent', 'ElixirTestApp/1.0.0'}
]}
:httpc.request(:get, request, [], [])
end
defp create_jwt(filepath, app_id, exp_time_min \\ 10) do
{:ok, private_pem} = File.read(filepath)
now = DateTime.to_unix(DateTime.utc_now())
exp_time = now + exp_time_min * 60
Jwerl.sign([iat: now, exp: exp_time, iss: app_id], :rs256, private_pem)
end
end