MapkitJS
Elixir helper library for generating MapKit JS authorization tokens.
It is designed for simple server-side token issuance for browser clients that call mapkit.init/1.
Installation
Add to your mix.exs:
defp deps do
[
{:mapkit_js, "~> 0.1.0"}
]
endConfiguration
config :mapkit_js,
team_id: System.get_env("APPLE_TEAM_ID"),
key_id: System.get_env("MAPKIT_KEY_ID"),
private_key_path: System.get_env("MAPKIT_PRIVATE_KEY_PATH"),
origin: System.get_env("MAPKIT_ORIGIN")Supported options:
team_id- Apple Developer Team IDkey_id- Maps key IDprivate_key- inline.p8contentsprivate_key_path- path to the.p8fileorigin- optional exact browser origin to constrain the tokentoken_ttl_seconds- defaults to300
Quick Start
# Generate a token
{:ok, token} = MapkitJS.token()
# Override origin per call
{:ok, token} = MapkitJS.generate_token(origin: "https://example.com")
# Build a payload for a browser-side authorization callback
{:ok, payload} = MapkitJS.authorization_callback_payload()Browser Usage
mapkit.init({
authorizationCallback: async function(done) {
const response = await fetch("/token")
const payload = await response.json()
done(payload.token)
}
})Local Demo
This project includes a local demo page and token server.
Run it with:
cd mapkit_js
source .env && elixir demo_server.exsThen open:
http://127.0.0.1:4002Live Smoke Test
cd mapkit_js
source .env && elixir test_live.exsNotes
-
The
originclaim must match the browser origin exactly when you use it. -
GitHub Pages works fine as an origin, for example
https://dl-alexandre.github.io. -
Tokens are cached in
MapkitJS.TokenCachewith a 60-second refresh buffer.
License
MIT