@uppercut-labs/android-app-links 0.0.0-stage → 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +70 -2
- package/dist/index.d.ts +62 -0
- package/dist/index.js +136 -0
- package/package.json +47 -4
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Uppercut Labs
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,3 +1,71 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @uppercut-labs/android-app-links
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Serves Android App Links for a web product from one function. It answers two paths and returns `null` for every other:
|
|
4
|
+
|
|
5
|
+
- `GET /.well-known/assetlinks.json`: the Digital Asset Links statements for the product's debug and release Android apps, built from package names and signing-certificate fingerprints.
|
|
6
|
+
- `GET /android/callback`: the sign-in return path an installed app intercepts as a verified App Link. In a browser, where the app is not installed, it is a branded page that says to open the app. The page is built only from the brand you pass, never from the URL, so it cannot reflect the authorization code or state it was sent.
|
|
7
|
+
|
|
8
|
+
```sh
|
|
9
|
+
npm install @uppercut-labs/android-app-links
|
|
10
|
+
```
|
|
11
|
+
|
|
12
|
+
## What runs where
|
|
13
|
+
|
|
14
|
+
TypeScript compiled to ESM JavaScript. It is server code that takes a standard `Request` and returns a `Response`, so it runs in Node 18 or later, Deno, Bun and Cloudflare Workers with Node compatibility turned on (it imports `node:crypto` to hash the page style). It runs on any OS. Nothing here is PowerShell or Bash. The commands under Working on it work in PowerShell on Windows and in Bash on macOS and Linux.
|
|
15
|
+
|
|
16
|
+
## Example
|
|
17
|
+
|
|
18
|
+
```ts
|
|
19
|
+
import { handleAndroidLinks, type CallbackBrand } from '@uppercut-labs/android-app-links';
|
|
20
|
+
|
|
21
|
+
const brand: CallbackBrand = {
|
|
22
|
+
name: 'Notes',
|
|
23
|
+
logo: '/icon.svg',
|
|
24
|
+
homeHref: '/',
|
|
25
|
+
colors: { base: '#0c1016', glow: '#1b2b43', button: '#dceafd', buttonBorder: '#cadcf5', buttonText: '#0c203c', focus: '#8db8f2' },
|
|
26
|
+
};
|
|
27
|
+
|
|
28
|
+
export default {
|
|
29
|
+
async fetch(request: Request, env: Record<string, string>) {
|
|
30
|
+
const answered = handleAndroidLinks(request, {
|
|
31
|
+
debugPackage: env.ANDROID_DEBUG_PACKAGE,
|
|
32
|
+
debugCertSha256: env.ANDROID_DEBUG_CERT_SHA256,
|
|
33
|
+
releasePackage: env.ANDROID_RELEASE_PACKAGE,
|
|
34
|
+
releaseCertSha256: env.ANDROID_RELEASE_CERT_SHA256,
|
|
35
|
+
}, brand);
|
|
36
|
+
if (answered) return answered; // null for every other path
|
|
37
|
+
return new Response('Not found', { status: 404 });
|
|
38
|
+
},
|
|
39
|
+
};
|
|
40
|
+
```
|
|
41
|
+
|
|
42
|
+
`assetlinks.json` lists each build whose package and fingerprints are both set. It is 404 when none is, and 503 when a value is malformed. A build with a package but no fingerprints yet (a release key that does not exist yet) is left out. The callback page carries a Content-Security-Policy that allows only its own hashed style; keep that policy when you add your own headers. Both paths answer `GET` and `HEAD`; any other method gets 405.
|
|
43
|
+
|
|
44
|
+
## Exports
|
|
45
|
+
|
|
46
|
+
| Export | What it is |
|
|
47
|
+
| --- | --- |
|
|
48
|
+
| `handleAndroidLinks(request, settings, brand)` | Answers the two paths, or returns `null` |
|
|
49
|
+
| `androidTargets(settings)` | The apps to declare, as `{ packageName, fingerprints }`. Throws `AndroidLinksConfigError` on a malformed package name or fingerprint |
|
|
50
|
+
| `assetLinks(targets)` | The Digital Asset Links statement list for those apps |
|
|
51
|
+
| `parseFingerprints(value)` | Splits, upper-cases and de-duplicates a comma-separated list of SHA-256 fingerprints (`AA:BB:...`, 32 bytes); throws on a malformed entry |
|
|
52
|
+
| `callbackPage(brand)` | `{ html, styleHash }`: the page and the base64 SHA-256 of its inline style, for a CSP |
|
|
53
|
+
| `ASSET_LINKS_PATH`, `ANDROID_CALLBACK_PATH` | The two paths |
|
|
54
|
+
| `AndroidLinksConfigError` | Thrown for malformed configuration |
|
|
55
|
+
|
|
56
|
+
Types: `AndroidAppSettings`, `AndroidAppTarget`, `CallbackBrand`.
|
|
57
|
+
|
|
58
|
+
The logo is shown at its own proportions, with no rounding, mask or clipping. The brand values are HTML-escaped.
|
|
59
|
+
|
|
60
|
+
## Working on it
|
|
61
|
+
|
|
62
|
+
From the repository root:
|
|
63
|
+
|
|
64
|
+
```sh
|
|
65
|
+
npm install
|
|
66
|
+
npm run check
|
|
67
|
+
```
|
|
68
|
+
|
|
69
|
+
## License
|
|
70
|
+
|
|
71
|
+
MIT
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
export declare const ASSET_LINKS_PATH = "/.well-known/assetlinks.json";
|
|
2
|
+
export declare const ANDROID_CALLBACK_PATH = "/android/callback";
|
|
3
|
+
export interface AndroidAppSettings {
|
|
4
|
+
/** Debug build application id, for example com.example.app.debug. */
|
|
5
|
+
debugPackage?: string;
|
|
6
|
+
/** Comma-separated SHA-256 signing-certificate fingerprints for the debug build. */
|
|
7
|
+
debugCertSha256?: string;
|
|
8
|
+
/** Release build application id, for example com.example.app. */
|
|
9
|
+
releasePackage?: string;
|
|
10
|
+
/** Comma-separated SHA-256 signing-certificate fingerprints for the release build. */
|
|
11
|
+
releaseCertSha256?: string;
|
|
12
|
+
}
|
|
13
|
+
export interface AndroidAppTarget {
|
|
14
|
+
packageName: string;
|
|
15
|
+
fingerprints: string[];
|
|
16
|
+
}
|
|
17
|
+
export interface CallbackBrand {
|
|
18
|
+
/** Product name, for example Notes. */
|
|
19
|
+
name: string;
|
|
20
|
+
/** Same-origin logo path, shown at its own proportions. */
|
|
21
|
+
logo: string;
|
|
22
|
+
/** Where the page's link goes, normally "/". */
|
|
23
|
+
homeHref: string;
|
|
24
|
+
/** Colours of the product theme: page background, glow, button and focus ring. */
|
|
25
|
+
colors: {
|
|
26
|
+
base: string;
|
|
27
|
+
glow: string;
|
|
28
|
+
button: string;
|
|
29
|
+
buttonBorder: string;
|
|
30
|
+
buttonText: string;
|
|
31
|
+
focus: string;
|
|
32
|
+
};
|
|
33
|
+
}
|
|
34
|
+
export declare class AndroidLinksConfigError extends Error {
|
|
35
|
+
}
|
|
36
|
+
/** Splits and normalises a comma-separated fingerprint list; throws on any malformed entry. */
|
|
37
|
+
export declare function parseFingerprints(value: string | undefined): string[];
|
|
38
|
+
/**
|
|
39
|
+
* The apps to declare: one statement per build whose package and fingerprints are both configured.
|
|
40
|
+
* A build with a package but no fingerprints yet (the release key before it exists) is left out.
|
|
41
|
+
*/
|
|
42
|
+
export declare function androidTargets(settings: AndroidAppSettings): AndroidAppTarget[];
|
|
43
|
+
/** The Digital Asset Links statement list for the given apps. */
|
|
44
|
+
export declare function assetLinks(targets: AndroidAppTarget[]): {
|
|
45
|
+
relation: string[];
|
|
46
|
+
target: {
|
|
47
|
+
namespace: string;
|
|
48
|
+
package_name: string;
|
|
49
|
+
sha256_cert_fingerprints: string[];
|
|
50
|
+
};
|
|
51
|
+
}[];
|
|
52
|
+
/** The page a browser shows at /android/callback. Built only from the brand, never from the URL. */
|
|
53
|
+
export declare function callbackPage(brand: CallbackBrand): {
|
|
54
|
+
html: string;
|
|
55
|
+
styleHash: string;
|
|
56
|
+
};
|
|
57
|
+
/**
|
|
58
|
+
* Answers the two App Link paths, or returns null for any other path. Responses carry their own
|
|
59
|
+
* content type, caching and (for the page) content security policy; callers may add their usual
|
|
60
|
+
* security headers but should keep this CSP, which allows only the page's own hashed style.
|
|
61
|
+
*/
|
|
62
|
+
export declare function handleAndroidLinks(request: Request, settings: AndroidAppSettings, brand: CallbackBrand): Response | null;
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,136 @@
|
|
|
1
|
+
// Android App Links for a web product: Digital Asset Links and the sign-in return page.
|
|
2
|
+
//
|
|
3
|
+
// Each product passes its own package names, certificate fingerprints and brand. It serves:
|
|
4
|
+
// GET /.well-known/assetlinks.json Digital Asset Links naming the product's Android apps
|
|
5
|
+
// GET /android/callback the sign-in return path the app intercepts as a verified App
|
|
6
|
+
// Link; in a browser (app not installed) a branded page saying to
|
|
7
|
+
// open the app, which never reflects the code or state it was sent.
|
|
8
|
+
import { createHash } from "node:crypto";
|
|
9
|
+
export const ASSET_LINKS_PATH = "/.well-known/assetlinks.json";
|
|
10
|
+
export const ANDROID_CALLBACK_PATH = "/android/callback";
|
|
11
|
+
const PACKAGE = /^[a-zA-Z][a-zA-Z0-9_]*(\.[a-zA-Z][a-zA-Z0-9_]*)+$/;
|
|
12
|
+
const FINGERPRINT = /^([0-9A-F]{2}:){31}[0-9A-F]{2}$/;
|
|
13
|
+
export class AndroidLinksConfigError extends Error {
|
|
14
|
+
}
|
|
15
|
+
/** Splits and normalises a comma-separated fingerprint list; throws on any malformed entry. */
|
|
16
|
+
export function parseFingerprints(value) {
|
|
17
|
+
const list = (value ?? "")
|
|
18
|
+
.split(",")
|
|
19
|
+
.map((entry) => entry.trim().toUpperCase())
|
|
20
|
+
.filter(Boolean);
|
|
21
|
+
for (const entry of list)
|
|
22
|
+
if (!FINGERPRINT.test(entry))
|
|
23
|
+
throw new AndroidLinksConfigError("A signing-certificate fingerprint is malformed");
|
|
24
|
+
return [...new Set(list)];
|
|
25
|
+
}
|
|
26
|
+
/**
|
|
27
|
+
* The apps to declare: one statement per build whose package and fingerprints are both configured.
|
|
28
|
+
* A build with a package but no fingerprints yet (the release key before it exists) is left out.
|
|
29
|
+
*/
|
|
30
|
+
export function androidTargets(settings) {
|
|
31
|
+
const targets = [];
|
|
32
|
+
for (const [rawPackage, rawPrints] of [
|
|
33
|
+
[settings.debugPackage, settings.debugCertSha256],
|
|
34
|
+
[settings.releasePackage, settings.releaseCertSha256],
|
|
35
|
+
]) {
|
|
36
|
+
const packageName = rawPackage?.trim();
|
|
37
|
+
const fingerprints = parseFingerprints(rawPrints);
|
|
38
|
+
if (!packageName || !fingerprints.length)
|
|
39
|
+
continue;
|
|
40
|
+
if (!PACKAGE.test(packageName))
|
|
41
|
+
throw new AndroidLinksConfigError("An Android package name is malformed");
|
|
42
|
+
targets.push({ packageName, fingerprints });
|
|
43
|
+
}
|
|
44
|
+
return targets;
|
|
45
|
+
}
|
|
46
|
+
/** The Digital Asset Links statement list for the given apps. */
|
|
47
|
+
export function assetLinks(targets) {
|
|
48
|
+
return targets.map((target) => ({
|
|
49
|
+
relation: ["delegate_permission/common.handle_all_urls"],
|
|
50
|
+
target: {
|
|
51
|
+
namespace: "android_app",
|
|
52
|
+
package_name: target.packageName,
|
|
53
|
+
sha256_cert_fingerprints: target.fingerprints,
|
|
54
|
+
},
|
|
55
|
+
}));
|
|
56
|
+
}
|
|
57
|
+
const escape = (value) => value.replace(/[&<>"']/g, (c) => ({ "&": "&", "<": "<", ">": ">", '"': """, "'": "'" })[c]);
|
|
58
|
+
/** The page a browser shows at /android/callback. Built only from the brand, never from the URL. */
|
|
59
|
+
export function callbackPage(brand) {
|
|
60
|
+
const c = brand.colors;
|
|
61
|
+
const style = `
|
|
62
|
+
:root{color-scheme:dark}
|
|
63
|
+
*{box-sizing:border-box}
|
|
64
|
+
body{margin:0}
|
|
65
|
+
.page{min-height:100vh;min-height:100svh;display:flex;flex-direction:column;align-items:center;justify-content:center;padding:max(40px,env(safe-area-inset-top)) 24px max(24px,env(safe-area-inset-bottom));background:radial-gradient(ellipse at 55% 30%,${c.glow} 0,transparent 57%),${c.base};color:#eef3f9;font-family:Inter,-apple-system,BlinkMacSystemFont,"Segoe UI",sans-serif;text-align:center}
|
|
66
|
+
.card{width:100%;max-width:420px;display:flex;flex-direction:column;align-items:center;padding:32px 0 40px}
|
|
67
|
+
.logo{display:block;width:104px;height:104px;object-fit:contain;border-radius:0;clip-path:none;mask:none}
|
|
68
|
+
h1{margin:22px 0 0;font-size:54px;line-height:1.05;letter-spacing:-2px;font-weight:700;color:#f4f7fb}
|
|
69
|
+
.lead{margin:14px 0 0;font-size:19px;line-height:1.45;font-weight:500;color:#c3d1e3;text-wrap:balance}
|
|
70
|
+
.note{margin:16px 0 0;font-size:15px;line-height:1.5;color:#aab6c6}
|
|
71
|
+
.home{align-self:stretch;display:flex;align-items:center;justify-content:center;min-height:54px;margin-top:32px;padding:10px 16px;border:1px solid ${c.buttonBorder};border-radius:10px;background:${c.button};color:${c.buttonText};font-size:15px;font-weight:650;text-decoration:none}
|
|
72
|
+
.home:focus-visible{outline:3px solid ${c.focus};outline-offset:3px}
|
|
73
|
+
@media (max-width:380px){h1{font-size:44px}}
|
|
74
|
+
`.trim();
|
|
75
|
+
const name = escape(brand.name);
|
|
76
|
+
const html = `<!doctype html>
|
|
77
|
+
<html lang="en">
|
|
78
|
+
<head>
|
|
79
|
+
<meta charset="utf-8">
|
|
80
|
+
<meta name="viewport" content="width=device-width, initial-scale=1">
|
|
81
|
+
<meta name="robots" content="noindex">
|
|
82
|
+
<meta name="referrer" content="no-referrer">
|
|
83
|
+
<title>Open the ${name} app</title>
|
|
84
|
+
<style>${style}</style>
|
|
85
|
+
</head>
|
|
86
|
+
<body>
|
|
87
|
+
<main class="page">
|
|
88
|
+
<div class="card">
|
|
89
|
+
<img class="logo" src="${escape(brand.logo)}" alt="" width="104" height="104">
|
|
90
|
+
<h1>${name}</h1>
|
|
91
|
+
<p class="lead">Open the ${name} app on this phone to finish signing in.</p>
|
|
92
|
+
<p class="note">This link is for the ${name} Android app. If the app isn't on this phone, you can keep using ${name} in your browser.</p>
|
|
93
|
+
<a class="home" href="${escape(brand.homeHref)}">Go to ${name}</a>
|
|
94
|
+
</div>
|
|
95
|
+
</main>
|
|
96
|
+
</body>
|
|
97
|
+
</html>
|
|
98
|
+
`;
|
|
99
|
+
return { html, styleHash: createHash("sha256").update(style).digest("base64") };
|
|
100
|
+
}
|
|
101
|
+
/**
|
|
102
|
+
* Answers the two App Link paths, or returns null for any other path. Responses carry their own
|
|
103
|
+
* content type, caching and (for the page) content security policy; callers may add their usual
|
|
104
|
+
* security headers but should keep this CSP, which allows only the page's own hashed style.
|
|
105
|
+
*/
|
|
106
|
+
export function handleAndroidLinks(request, settings, brand) {
|
|
107
|
+
const path = new URL(request.url).pathname;
|
|
108
|
+
if (path !== ASSET_LINKS_PATH && path !== ANDROID_CALLBACK_PATH)
|
|
109
|
+
return null;
|
|
110
|
+
if (request.method !== "GET" && request.method !== "HEAD")
|
|
111
|
+
return new Response(null, { status: 405, headers: { allow: "GET, HEAD" } });
|
|
112
|
+
const head = request.method === "HEAD";
|
|
113
|
+
if (path === ASSET_LINKS_PATH) {
|
|
114
|
+
let targets;
|
|
115
|
+
try {
|
|
116
|
+
targets = androidTargets(settings);
|
|
117
|
+
}
|
|
118
|
+
catch {
|
|
119
|
+
return Response.json({ error: "App links are misconfigured" }, { status: 503, headers: { "cache-control": "no-store" } });
|
|
120
|
+
}
|
|
121
|
+
if (!targets.length)
|
|
122
|
+
return new Response(null, { status: 404, headers: { "cache-control": "no-store" } });
|
|
123
|
+
return new Response(head ? null : JSON.stringify(assetLinks(targets)), {
|
|
124
|
+
headers: { "content-type": "application/json", "cache-control": "public, max-age=3600" },
|
|
125
|
+
});
|
|
126
|
+
}
|
|
127
|
+
const page = callbackPage(brand);
|
|
128
|
+
return new Response(head ? null : page.html, {
|
|
129
|
+
headers: {
|
|
130
|
+
"content-type": "text/html; charset=utf-8",
|
|
131
|
+
"cache-control": "no-store",
|
|
132
|
+
"x-robots-tag": "noindex",
|
|
133
|
+
"content-security-policy": `default-src 'none'; img-src 'self'; style-src 'sha256-${page.styleHash}'; base-uri 'none'; form-action 'none'; frame-ancestors 'none'`,
|
|
134
|
+
},
|
|
135
|
+
});
|
|
136
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,49 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@uppercut-labs/android-app-links",
|
|
3
|
-
"version": "
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "Serve Android App Links for a web product: the assetlinks.json Digital Asset Links file from package names and signing fingerprints, and a branded sign-in return page, as a function from Request to Response.",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"author": "Uppercut Labs <packages@uppercut-labs.com>",
|
|
7
|
+
"type": "module",
|
|
8
|
+
"exports": {
|
|
9
|
+
".": {
|
|
10
|
+
"types": "./dist/index.d.ts",
|
|
11
|
+
"default": "./dist/index.js"
|
|
12
|
+
}
|
|
13
|
+
},
|
|
14
|
+
"main": "./dist/index.js",
|
|
15
|
+
"types": "./dist/index.d.ts",
|
|
16
|
+
"files": [
|
|
17
|
+
"dist",
|
|
18
|
+
"README.md",
|
|
19
|
+
"LICENSE"
|
|
20
|
+
],
|
|
21
|
+
"sideEffects": false,
|
|
22
|
+
"scripts": {
|
|
23
|
+
"build": "tsc -p tsconfig.json",
|
|
24
|
+
"prepack": "node -e \"require('fs').copyFileSync('../../LICENSE','LICENSE')\""
|
|
25
|
+
},
|
|
26
|
+
"keywords": [
|
|
27
|
+
"android",
|
|
28
|
+
"app-links",
|
|
29
|
+
"assetlinks",
|
|
30
|
+
"digital-asset-links",
|
|
31
|
+
"oauth",
|
|
32
|
+
"fetch"
|
|
33
|
+
],
|
|
34
|
+
"repository": {
|
|
35
|
+
"type": "git",
|
|
36
|
+
"url": "git+https://github.com/uppercut-labs/js-kit.git",
|
|
37
|
+
"directory": "packages/android-app-links"
|
|
38
|
+
},
|
|
39
|
+
"bugs": {
|
|
40
|
+
"url": "https://github.com/uppercut-labs/js-kit/issues"
|
|
41
|
+
},
|
|
42
|
+
"homepage": "https://github.com/uppercut-labs/js-kit/tree/main/packages/android-app-links#readme",
|
|
43
|
+
"engines": {
|
|
44
|
+
"node": ">=18"
|
|
45
|
+
},
|
|
46
|
+
"publishConfig": {
|
|
47
|
+
"access": "public"
|
|
48
|
+
}
|
|
49
|
+
}
|