Skip to content

Commit d04e036

Browse files
committed
Merge feat/requester-display: authRequest.requesterDisplay typings and echo
2 parents f70a1f9 + 77f1ff6 commit d04e036

7 files changed

Lines changed: 224 additions & 3 deletions

File tree

‎CHANGELOG.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
### Added
88

9+
- **`pryv`: `authRequest.requesterDisplay`** (`{ name, logoUri?, purpose? }`, typed `AuthRequesterDisplay`; `name` and `purpose` are a text or texts by language code). How the auth page may name your app when the platform's app catalog does not know its `requestingAppId`: the page shows it as self-declared, with the app id and a "not verified" note, never in place of a catalog entry; the logo is shown only when its host is on the platform's `access:logoHosts` list. Sent as given by `Service.startAccessRequest` and the sign-in button. `startAccessRequest` returns the core's echo as `requesterDisplay` (its stored form: texts trimmed and normalised, `logoUri` in its canonical URL form, or without `logoUri` when the logo host is not allowed); the `NEED_SIGNIN` state carries it too. Absent echo: an older core, or a platform that does not show declared names; the request still succeeds and the page shows the app id as before. A malformed value is refused by a core that supports it (`400 invalid-parameters`). Needs open-pryv.io 2.0.0-rc.94 or later.
910
- **`@pryv/cmc` `errorIds`: `ACCEPT_WITHDRAWN`** (`'cmc-accept-withdrawn'`, on the accepter's accept trigger: a queued retry of an accept whose consent ended meanwhile, or that a newer accept for the same counterparty and scope replaced; nothing is minted or delivered again) **and `INCOMING_ACCEPT_SUPERSEDED`** (`'cmc-incoming-accept-superseded'`, on the requester's core: an accept received before the one the relationship is paired with, refused when retried). Both are permanent, never retried, and returned by open-pryv.io 2.0.0-rc.90 and later. Also in the `CmcErrorId` typing.
1011
- **`@pryv/delegation` typings: `DelegateRecord.origin` and `ControlledRecord.origin`** (`DelegationOrigin`, `'created' | 'attached'`): how the relationship was made, set by the core (open-pryv.io issue 174). Optional: absent from cores up to 2.0.0-rc.92. Types only, no runtime change.
1112
- **`pryv` typings: `AccountInformation.emails`** (`AccountEmail[]`, the result of `account.get` / `account.update` on cores with multiple emails per account): `value`, `primary`, `status` (`'pending' | 'verified'`), `verifiedAt`, `verificationMethod` (`AccountEmailVerificationMethod`) and `verificationCooldownUntil` (the time until which a new verification mail for the address is refused, `null` when no cooldown runs; optional, absent from cores up to 2.0.0-rc.92; a refused resend's `retryAfterSeconds` stays authoritative). Types only, no runtime change.

‎components/pryv/src/Auth/index.js‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,9 @@ module.exports = {
3434
* @param {string} [settings.authRequest.authUrl] Your own auth page for this request; honoured only
3535
* when it matches the platform's `access:trustedAuthUrls`
3636
* @param {string} [settings.authRequest.referer] To track registration source
37+
* @param {Object} [settings.authRequest.requesterDisplay] `{ name, logoUri?, purpose? }`: how the auth
38+
* page may name the app when the platform's catalog does not know it (shown as self-declared); sent
39+
* as given, see `Service.startAccessRequest`
3740
* @param {string} settings.spanButtonID set and <span> id in DOM to insert default login button or null for custom
3841
* @param {Function} settings.onStateChange
3942
* @param {string} serviceInfoUrl

‎components/pryv/src/Service.js‎

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -496,9 +496,23 @@ class Service {
496496
* @param {boolean} [authRequest.actAsManagedOnly] - true: the access must
497497
* be granted for an account the user manages; requires `actAs: 'allow'`
498498
* or a username.
499-
* @returns {Promise<{ key: string, authUrl: string, poll: string, pollRateMs: number, consent?: Object, cmcInvites?: Array<Object>, actAsManagedOnly?: true }>}
500-
* `consent`, `cmcInvites` and `actAsManagedOnly` are echoed back only by
501-
* a core that understood them, which is how you detect support.
499+
* @param {Object} [authRequest.requesterDisplay] - How the auth page may
500+
* name your app when the platform's app catalog does not know
501+
* `requestingAppId`. Shown as self-declared (with the app id and a
502+
* "not verified" note), never in place of a catalog entry.
503+
* @param {string|Object<string, string>} authRequest.requesterDisplay.name -
504+
* text, or texts by language code (1 to 64 characters each).
505+
* @param {string} [authRequest.requesterDisplay.logoUri] - https URL; shown
506+
* only when its host is on the platform's `access:logoHosts` list, else
507+
* dropped (the request still succeeds).
508+
* @param {string|Object<string, string>} [authRequest.requesterDisplay.purpose] -
509+
* one line of text, or texts by language code (1 to 140 characters each).
510+
* @returns {Promise<{ key: string, authUrl: string, poll: string, pollRateMs: number, consent?: Object, cmcInvites?: Array<Object>, actAsManagedOnly?: true, requesterDisplay?: Object }>}
511+
* `consent`, `cmcInvites`, `actAsManagedOnly` and `requesterDisplay` are
512+
* echoed back only by a core that understood them, which is how you
513+
* detect support. `requesterDisplay` comes back in the form the core
514+
* stored (without `logoUri` when the logo host is not allowed), and is
515+
* absent when the platform does not show declared names.
502516
* @throws {PryvError} on non-2xx
503517
*/
504518
async startAccessRequest (authRequest) {
@@ -532,6 +546,9 @@ class Service {
532546
// Echoed only by a core that understood `actAsManagedOnly`: an older core
533547
// drops the field, and the auth page then behaves per `actAs` alone.
534548
if (body.actAsManagedOnly === true) envelope.actAsManagedOnly = true;
549+
// Echoed in its stored form only by a core that stored it: absent from an
550+
// older core, or when the platform does not show declared names.
551+
if (body.requesterDisplay != null) envelope.requesterDisplay = body.requesterDisplay;
535552
// polling by key (pollAccessRequest, connectFromKey) then reaches the
536553
// core that holds the request
537554
pollUrls.remember(envelope.key, envelope.poll);

‎components/pryv/src/index.d.ts‎

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1199,6 +1199,13 @@ declare module 'pryv' {
11991199
* `authRequest.actAsManagedOnly`; absent otherwise.
12001200
*/
12011201
actAsManagedOnly?: true;
1202+
/**
1203+
* Echoed in its stored form (texts trimmed, `logoUri` dropped when its
1204+
* host is not allowed) only by a core that stored
1205+
* `authRequest.requesterDisplay`; absent from an older core or when the
1206+
* platform does not show declared names.
1207+
*/
1208+
requesterDisplay?: AuthRequesterDisplay;
12021209
}>;
12031210
/**
12041211
* Poll an access request once, by its full poll URL (recommended) or its
@@ -1310,6 +1317,34 @@ declare module 'pryv' {
13101317
accessName?: string;
13111318
};
13121319

1320+
/** A text, or texts by language code (`en`, `fr`, `de-CH`). */
1321+
export type AuthLocalizedText = string | { [languageCode: string]: string };
1322+
1323+
/**
1324+
* How the auth page may name the requesting app when the platform's app
1325+
* catalog does not know its `requestingAppId` (`authRequest.requesterDisplay`).
1326+
* The page shows it as self-declared: with the app id and a "not verified"
1327+
* note, never in place of a catalog entry, and not when it repeats the
1328+
* catalog name of another app. Not kept on the created access.
1329+
*
1330+
* A malformed value (wrong shape, a text too long or with control
1331+
* characters, a logo that is not https) is refused with
1332+
* `400 invalid-parameters`. A core that predates it, or a platform that
1333+
* does not show declared names, ignores it: no echo.
1334+
*/
1335+
export type AuthRequesterDisplay = {
1336+
/** 1 to 64 characters per text, no newline or control characters. */
1337+
name: AuthLocalizedText;
1338+
/**
1339+
* https URL (at most 2048 characters, no user information, no fragment).
1340+
* Shown only when its host is on the platform's `access:logoHosts` list;
1341+
* otherwise dropped and missing from the echo.
1342+
*/
1343+
logoUri?: string;
1344+
/** One line, 1 to 140 characters per text, shown in a fixed, attributed frame. */
1345+
purpose?: AuthLocalizedText;
1346+
};
1347+
13131348
/** An invite as the core echoes it back, with its defaults filled in. */
13141349
export type AuthCmcInvite = {
13151350
capabilityUrl: string;
@@ -1384,6 +1419,8 @@ declare module 'pryv' {
13841419
cmcInvites?: AuthCmcInvite[];
13851420
/** Present only when the request carried `actAsManagedOnly: true` and the core understood it. */
13861421
actAsManagedOnly?: true;
1422+
/** Present only when the request carried `requesterDisplay` and the core stored it (stored form). */
1423+
requesterDisplay?: AuthRequesterDisplay;
13871424
requestingAppId: string;
13881425
returnURL?: string | null;
13891426
serviceInfo?: ServiceInfo;
@@ -1474,6 +1511,7 @@ declare module 'pryv' {
14741511
consent?: AuthRequestConsentForm;
14751512
cmcInvites?: AuthCmcInvite[];
14761513
actAsManagedOnly?: true;
1514+
requesterDisplay?: AuthRequesterDisplay;
14771515
lang?: string;
14781516
returnURL?: string;
14791517
clientData?: KeyValue;
@@ -1569,6 +1607,12 @@ declare module 'pryv' {
15691607
* field and does not echo it (see `Service.startAccessRequest`).
15701608
*/
15711609
actAsManagedOnly?: boolean;
1610+
/**
1611+
* How the auth page may name your app when the platform's app catalog
1612+
* does not know `requestingAppId` (shown as self-declared). Sent as
1613+
* given; see {@link AuthRequesterDisplay}.
1614+
*/
1615+
requesterDisplay?: AuthRequesterDisplay;
15721616
};
15731617
};
15741618

Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
/**
2+
* @license
3+
* [BSD-3-Clause](https://github.com/pryv/lib-js/blob/master/LICENSE)
4+
*/
5+
/* global describe, it, afterEach, expect, pryv */
6+
7+
const AuthController = require('../src/Auth/AuthController');
8+
const AuthStates = require('../src/Auth/AuthStates');
9+
const utils = require('../src/utils');
10+
11+
/**
12+
* [RQLX] The `requesterDisplay` of an auth request travels untouched.
13+
*
14+
* It is how an app names itself on the auth page when the platform's app
15+
* catalog does not know it. This library posts it as given and hands back
16+
* the echo of a core that stored it; the core validates it and the auth
17+
* page decides how to show it. Driven through stubbed fetches, no platform.
18+
*/
19+
describe('[RQLX] auth-request requesterDisplay', function () {
20+
const SERVICE_INFO = {
21+
register: 'https://reg.test.local/',
22+
access: 'https://reg.test.local/access',
23+
api: 'https://{username}.test.local/',
24+
name: 'Test'
25+
};
26+
const DISPLAY = {
27+
name: { en: 'Cycle Intelligence', fr: 'Intelligence du cycle' },
28+
logoUri: 'https://cdn.test.local/logo.png',
29+
purpose: 'Tracks your cycle'
30+
};
31+
const REQUEST = {
32+
requestingAppId: 'cycle-intelligence',
33+
requestedPermissions: [{ streamId: 'diary', level: 'read', defaultName: 'Journal' }],
34+
requesterDisplay: DISPLAY
35+
};
36+
37+
let realFetch = null;
38+
let saved = null;
39+
let posted;
40+
41+
/** Stub fetch: serve service-info, capture the access POST, answer it. */
42+
function stubFetch (accessResponse) {
43+
if (realFetch == null) realFetch = global.fetch;
44+
posted = [];
45+
global.fetch = async function (url, options) {
46+
if (String(url).includes('/access')) {
47+
posted.push(JSON.parse(options.body));
48+
return { ok: true, status: 201, json: async () => accessResponse };
49+
}
50+
return { ok: true, status: 200, json: async () => SERVICE_INFO };
51+
};
52+
}
53+
54+
afterEach(function () {
55+
if (realFetch != null) global.fetch = realFetch;
56+
realFetch = null;
57+
if (saved != null) {
58+
utils.fetchPost = saved.fetchPost;
59+
utils.fetchGet = saved.fetchGet;
60+
}
61+
saved = null;
62+
});
63+
64+
it('[RQLA] startAccessRequest posts requesterDisplay verbatim and returns the echo of the core', async function () {
65+
// The core echoes its stored form: here the logo host is not allowed, so
66+
// the echo comes back without `logoUri`, which is what the app reads.
67+
const echo = { name: DISPLAY.name, purpose: DISPLAY.purpose };
68+
stubFetch({ key: 'k1', authUrl: 'https://auth/', poll: 'https://poll/k1', poll_rate_ms: 1000, requesterDisplay: echo });
69+
const service = new pryv.Service('https://reg.test.local/service/info');
70+
const env = await service.startAccessRequest(REQUEST);
71+
expect(posted).to.have.lengthOf(1);
72+
expect(posted[0].requesterDisplay).to.deep.equal(DISPLAY);
73+
expect(env.requesterDisplay).to.deep.equal(echo);
74+
});
75+
76+
it('[RQLB] no echo (an older core, or a platform that does not show declared names) leaves the envelope without the key', async function () {
77+
stubFetch({ key: 'k2', authUrl: 'https://auth/', poll: 'https://poll/k2', poll_rate_ms: 1000 });
78+
const service = new pryv.Service('https://reg.test.local/service/info');
79+
const env = await service.startAccessRequest(REQUEST);
80+
expect(posted[0].requesterDisplay).to.deep.equal(DISPLAY);
81+
expect(env.key).to.equal('k2');
82+
expect(env).to.not.have.property('requesterDisplay');
83+
});
84+
85+
it('[RQLC] the sign-in button posts requesterDisplay as given and its NEED_SIGNIN state carries the echo', async function () {
86+
const echo = { name: 'Cycle Intelligence' };
87+
const bodies = [];
88+
saved = { fetchPost: utils.fetchPost, fetchGet: utils.fetchGet };
89+
utils.fetchPost = async (url, body) => {
90+
bodies.push(body);
91+
return {
92+
response: { ok: true, status: 201 },
93+
body: { status: 'NEED_SIGNIN', key: 'rk1', poll: 'https://core.test.local/reg/access/rk1', poll_rate_ms: 5, authUrl: 'https://ui.test.local/auth?key=rk1', requesterDisplay: echo }
94+
};
95+
};
96+
utils.fetchGet = async () => ({ response: { ok: false, status: 403 }, body: { status: 'REFUSED', reasonId: 'REFUSED_BY_USER' } });
97+
const seen = [];
98+
const auth = new AuthController({
99+
onStateChange: (s) => seen.push(s),
100+
authRequest: { requestingAppId: 'cycle-intelligence', requestedPermissions: [], credentialHandoff: 'inline', requesterDisplay: DISPLAY }
101+
}, { infoSync: () => SERVICE_INFO });
102+
auth.serviceInfo = SERVICE_INFO;
103+
auth.state = { status: AuthStates.INITIALIZED, serviceInfo: SERVICE_INFO };
104+
await auth.startAuthRequest();
105+
106+
expect(bodies).to.have.lengthOf(1);
107+
expect(bodies[0].requesterDisplay).to.deep.equal(DISPLAY);
108+
const needSignin = seen.find((s) => s.status === 'NEED_SIGNIN');
109+
expect(needSignin.requesterDisplay).to.deep.equal(echo);
110+
// not carried past the decision
111+
const refused = seen.find((s) => s.status === 'REFUSED');
112+
expect(refused).to.not.have.property('requesterDisplay');
113+
});
114+
});

‎components/pryv/test/typings.test.js‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,4 +55,8 @@ describe('[TYPX] typings', function () {
5555
it('[TYP6] events.delete / streams.delete: mergeEventsWithParent, trashed item or deletion answer, apiOne keys', function () {
5656
expect(compile('deleteMethods.ts')).to.deep.equal([]);
5757
});
58+
59+
it('[TYP7] requesterDisplay: request field, startAccessRequest echo, NEED_SIGNIN state, auth response', function () {
60+
expect(compile('requesterDisplay.ts')).to.deep.equal([]);
61+
});
5862
});
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/**
2+
* @license
3+
* [BSD-3-Clause](https://github.com/pryv/lib-js/blob/master/LICENSE)
4+
*/
5+
// Compiled (never run) by typings.test.js: the declared requester display of
6+
// an auth request, as an app sends it and reads the echo.
7+
import type { Service, AuthSettings, AuthRequesterDisplay, StateChange, AuthRequestResponse } from 'pryv';
8+
9+
const plain: AuthRequesterDisplay = { name: 'Cycle Intelligence' };
10+
const localized: AuthRequesterDisplay = {
11+
name: { en: 'Cycle Intelligence', fr: 'Intelligence du cycle' },
12+
logoUri: 'https://cdn.example.com/logo.png',
13+
purpose: { en: 'Tracks your cycle', fr: 'Suit votre cycle' }
14+
};
15+
// @ts-expect-error the name is required
16+
const nameless: AuthRequesterDisplay = { logoUri: 'https://cdn.example.com/logo.png' };
17+
18+
export const settings: AuthSettings = {
19+
authRequest: {
20+
requestingAppId: 'cycle-intelligence',
21+
requestedPermissions: [],
22+
requesterDisplay: localized
23+
}
24+
};
25+
26+
export async function start (service: Service): Promise<string | undefined> {
27+
const env = await service.startAccessRequest({ ...settings.authRequest, requesterDisplay: plain });
28+
const name = env.requesterDisplay?.name;
29+
return typeof name === 'string' ? name : name?.en;
30+
}
31+
32+
export function onNeedSignin (state: StateChange<'NEED_SIGNIN'>): string | undefined {
33+
return state.requesterDisplay?.logoUri;
34+
}
35+
36+
export function fromResponse (res: AuthRequestResponse): boolean {
37+
return res.requesterDisplay != null && nameless != null;
38+
}

0 commit comments

Comments
 (0)