When to use a public client
#Register a browser, mobile or desktop (public) application when there is no server to keep a secret. The token endpoint and API allow cross-origin requests with Authorization headers and no cookies. Keep tokens in memory, avoid offline_access in browsers, and remove the code from the address bar after the callback. If your site has a backend, use the server-side pattern instead.
Complete example
#Register https://app.example.com/ (your page's exact address) as a redirect URL, enable profile:read and storage:quota:read, and replace the client ID.
<!-- index.html on https://app.example.com (public client, PKCE only) -->
<button id="connect">Connect OzVault</button>
<pre id="out"></pre>
<script type="module">
const OZVAULT = "https://ozvault.lol";
const API = "https://api.ozvault.lol";
const CLIENT_ID = "ovc_YOUR_PUBLIC_CLIENT_ID";
const REDIRECT_URI = location.origin + "/"; // registered exactly, e.g. https://app.example.com/
const b64url = (bytes) => btoa(String.fromCharCode(...new Uint8Array(bytes)))
.replace(/\+/g, "-").replace(/\//g, "_").replace(/=+$/, "");
let accessToken = null; // memory only: never localStorage
document.querySelector("#connect").onclick = async () => {
const verifier = b64url(crypto.getRandomValues(new Uint8Array(32)));
const state = b64url(crypto.getRandomValues(new Uint8Array(16)));
sessionStorage.setItem("ozv_pkce", JSON.stringify({ verifier, state }));
const challenge = b64url(await crypto.subtle.digest("SHA-256", new TextEncoder().encode(verifier)));
location.assign(OZVAULT + "/oauth/authorize?" + new URLSearchParams({
response_type: "code",
client_id: CLIENT_ID,
redirect_uri: REDIRECT_URI,
scope: "profile:read storage:quota:read",
state,
code_challenge: challenge,
code_challenge_method: "S256",
}));
};
const params = new URLSearchParams(location.search);
if (params.has("code") || params.has("error")) {
const saved = JSON.parse(sessionStorage.getItem("ozv_pkce") || "null");
sessionStorage.removeItem("ozv_pkce");
history.replaceState(null, "", location.pathname); // remove the code from the address bar
if (!saved || params.get("state") !== saved.state || params.get("iss") !== OZVAULT) throw new Error("Invalid callback");
if (params.has("error")) throw new Error(params.get("error_description"));
const r = await fetch(OZVAULT + "/oauth/token", {
method: "POST",
headers: { "Content-Type": "application/x-www-form-urlencoded" },
body: new URLSearchParams({
grant_type: "authorization_code",
client_id: CLIENT_ID, // public clients send client_id and no secret
code: params.get("code"),
redirect_uri: REDIRECT_URI,
code_verifier: saved.verifier,
}),
});
const tokens = await r.json();
if (!r.ok) throw new Error(tokens.error_description);
accessToken = tokens.access_token;
const quota = await (await fetch(API + "/v1/storage/quota", { headers: { Authorization: "Bearer " + accessToken } })).json();
document.querySelector("#out").textContent = (quota.available / 1e9).toFixed(2) + " GB available";
}
</script>Clear documentation is part of the product.