@schibsted/account-sdk-browser 6.0.0-alpha.1 → 6.0.0-alpha.3

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.
Files changed (89) hide show
  1. package/README.md +39 -64
  2. package/dist/account-sdk.d.ts +4 -0
  3. package/dist/account.d.ts +29 -0
  4. package/dist/cache.d.ts +65 -0
  5. package/dist/config.d.ts +86 -0
  6. package/dist/get-account-sdk.d.ts +3 -0
  7. package/dist/global-registry.d.ts +23 -0
  8. package/dist/globals.d.ts +15 -0
  9. package/dist/has-access-response.d.ts +2 -0
  10. package/dist/identity.d.ts +323 -0
  11. package/dist/index.d.ts +8 -0
  12. package/dist/index.js +859 -0
  13. package/dist/index.js.map +1 -0
  14. package/dist/monetization.d.ts +58 -0
  15. package/{src → dist}/object.d.ts +4 -9
  16. package/dist/popup.d.ts +9 -0
  17. package/dist/resolve-browser-window.d.ts +1 -0
  18. package/dist/rest-client.d.ts +102 -0
  19. package/dist/rest-clients.d.ts +10 -0
  20. package/dist/sdk-error.d.ts +27 -0
  21. package/dist/session-response.d.ts +30 -0
  22. package/{src/spidTalk.d.ts → dist/spid-talk.d.ts} +4 -6
  23. package/dist/types/account.d.ts +13 -0
  24. package/dist/types/common.d.ts +22 -0
  25. package/dist/types/identity.d.ts +41 -0
  26. package/dist/types/login.d.ts +120 -0
  27. package/dist/types/monetization-guards.d.ts +2 -0
  28. package/dist/types/monetization.d.ts +21 -0
  29. package/dist/types/session-guards.d.ts +36 -0
  30. package/dist/types/session.d.ts +124 -0
  31. package/dist/url.d.ts +8 -0
  32. package/dist/validate.d.ts +50 -0
  33. package/dist/version.d.ts +2 -0
  34. package/package.json +30 -22
  35. package/src/account-sdk.ts +74 -0
  36. package/src/account.ts +149 -0
  37. package/src/{cache.js → cache.ts} +53 -38
  38. package/src/{config.js → config.ts} +7 -32
  39. package/src/get-account-sdk.ts +94 -0
  40. package/src/global-registry.ts +39 -0
  41. package/src/globals.ts +12 -0
  42. package/src/has-access-response.ts +35 -0
  43. package/src/identity.ts +1102 -0
  44. package/src/index.ts +32 -0
  45. package/src/{monetization.js → monetization.ts} +65 -73
  46. package/src/{object.js → object.ts} +9 -16
  47. package/src/popup.ts +74 -0
  48. package/src/resolve-browser-window.ts +11 -0
  49. package/src/rest-client.ts +253 -0
  50. package/src/rest-clients.ts +30 -0
  51. package/src/sdk-error.ts +59 -0
  52. package/src/session-response.ts +85 -0
  53. package/src/{spidTalk.js → spid-talk.ts} +10 -12
  54. package/src/types/account.ts +27 -0
  55. package/src/types/common.ts +26 -0
  56. package/src/types/identity.ts +55 -0
  57. package/src/types/login.ts +145 -0
  58. package/src/types/monetization-guards.ts +35 -0
  59. package/src/types/monetization.ts +24 -0
  60. package/src/types/session-guards.ts +89 -0
  61. package/src/types/session.ts +147 -0
  62. package/src/{url.js → url.ts} +6 -10
  63. package/src/{validate.js → validate.ts} +27 -43
  64. package/src/{version.js → version.ts} +1 -2
  65. package/identity.d.ts +0 -1
  66. package/identity.js +0 -5
  67. package/index.d.ts +0 -4
  68. package/index.js +0 -9
  69. package/monetization.d.ts +0 -1
  70. package/monetization.js +0 -5
  71. package/payment.d.ts +0 -1
  72. package/payment.js +0 -5
  73. package/src/RESTClient.d.ts +0 -89
  74. package/src/RESTClient.js +0 -193
  75. package/src/SDKError.d.ts +0 -16
  76. package/src/SDKError.js +0 -55
  77. package/src/cache.d.ts +0 -64
  78. package/src/config.d.ts +0 -34
  79. package/src/global-registry.js +0 -20
  80. package/src/identity.d.ts +0 -679
  81. package/src/identity.js +0 -1154
  82. package/src/monetization.d.ts +0 -80
  83. package/src/payment.d.ts +0 -115
  84. package/src/payment.js +0 -211
  85. package/src/popup.d.ts +0 -10
  86. package/src/popup.js +0 -59
  87. package/src/url.d.ts +0 -10
  88. package/src/validate.d.ts +0 -64
  89. package/src/version.d.ts +0 -2
@@ -0,0 +1,323 @@
1
+ import { TinyEmitter } from 'tiny-emitter';
2
+ import { default as Cache } from './cache.js';
3
+ import { default as RESTClient } from './rest-client.js';
4
+ import { LogFunction } from './types/common.js';
5
+ import { IdentityBeforeRedirectCallback, IdentityOptions, VarnishCookieOptions } from './types/identity.js';
6
+ import { LoginOptions, OpenSimplifiedLoginWidget, SimplifiedLoginData, SimplifiedLoginWidgetLoginOptions, SimplifiedLoginWidgetOptions } from './types/login.js';
7
+ import { ConnectedSessionResponse, SessionObjectResponse, SessionResponse } from './types/session.js';
8
+ export { isConnectedSessionResponse, isSessionSuccessResponse } from './session-response.js';
9
+ export type { ConnectedSessionResponse, SessionFailureResponse, SessionObjectResponse, SessionResponse, SessionSuccessResponse, SessionUserId, } from './types/session.js';
10
+ declare global {
11
+ interface Window {
12
+ openSimplifiedLoginWidget?: OpenSimplifiedLoginWidget;
13
+ }
14
+ }
15
+ /**
16
+ * Provides Identity functionalty to a web page
17
+ */
18
+ export declare class Identity extends TinyEmitter {
19
+ _sessionInitiatedSent: boolean;
20
+ window: Window;
21
+ clientId: string;
22
+ sessionStorageCache: Cache;
23
+ localStorageCache: Cache;
24
+ redirectUri: string;
25
+ env: string;
26
+ log?: LogFunction;
27
+ callbackBeforeRedirect: IdentityBeforeRedirectCallback;
28
+ _sessionDomain: string;
29
+ _enableSessionCaching: boolean;
30
+ /**
31
+ * @internal Starts as `{}` before the first `hasSession()` call, then stores the latest
32
+ * object response from hasSession().
33
+ */
34
+ _session: SessionObjectResponse;
35
+ _spid: RESTClient;
36
+ _oauthService: RESTClient;
37
+ _bffService: RESTClient;
38
+ _sessionService: RESTClient;
39
+ _globalSessionService: RESTClient;
40
+ _usedSessionServiceGetSessionEndpoint: 'v2/session' | 'session';
41
+ _hasSessionInProgress?: false | Promise<SessionResponse>;
42
+ popup?: Window | null;
43
+ setVarnishCookie?: boolean;
44
+ varnishExpiresIn?: number;
45
+ varnishCookieDomain?: string;
46
+ /**
47
+ * @param options
48
+ * @param options.clientId - Example: "1234567890abcdef12345678"
49
+ * @param options.sessionDomain - Example: "https://id.site.com"
50
+ * @param options.redirectUri - Example: "https://site.com"
51
+ * @param options.env - Schibsted account environment: `PRE` (default), `PRO`, `PRO_NO`, `PRO_FI` or `PRO_DK`
52
+ * @param options.log - A function that receives debug log information. If not set,
53
+ * no logging will be done
54
+ * @param options.window - window object
55
+ * @param options.callbackBeforeRedirect - callback triggered before session refresh redirect happen
56
+ * @throws {SDKError} - If any of options are invalid
57
+ */
58
+ constructor({ clientId, redirectUri, sessionDomain, env, log, window, callbackBeforeRedirect, }: IdentityOptions);
59
+ /**
60
+ * Read tabId from session storage
61
+ * @private
62
+ */
63
+ _getTabId(): number | undefined;
64
+ /**
65
+ * Checks if getting session is blocked
66
+ * @private
67
+ *
68
+ */
69
+ _isSessionCallBlocked(): boolean;
70
+ /**
71
+ * Block calls to get session
72
+ * @private
73
+ *
74
+ */
75
+ _blockSessionCall(): void;
76
+ /**
77
+ * Unblocks calls to get session
78
+ * @private
79
+ *
80
+ */
81
+ _unblockSessionCall(): void;
82
+ /**
83
+ * Emits the relevant events based on the previous and new reply from hasSession
84
+ * @private
85
+ * @param previous
86
+ * @param current
87
+ */
88
+ _emitSessionEvent(previous: object, current: object): void;
89
+ /**
90
+ * Close this.popup if it exists and is open
91
+ * @private
92
+ */
93
+ _closePopup(): void;
94
+ /**
95
+ * Set the Varnish cookie (`SP_ID`) when hasSession() is called. Note that most browsers require
96
+ * that you are on a "real domain" for this to work — so, **not** `localhost`
97
+ * @param options
98
+ * @param options.expiresIn Override this to set number of seconds before the varnish
99
+ * cookie expires. The default is to use the same time that hasSession responses are cached for
100
+ * @param options.domain Override cookie domain. E.g. «vg.no» instead of «www.vg.no»
101
+ */
102
+ enableVarnishCookie(options: VarnishCookieOptions): void;
103
+ /**
104
+ * Set the Varnish cookie if configured
105
+ * @private
106
+ * @param sessionData
107
+ */
108
+ _maybeSetVarnishCookie(sessionData: object): void;
109
+ /**
110
+ * Clear the Varnish cookie if configured
111
+ * @private
112
+ */
113
+ _maybeClearVarnishCookie(): void;
114
+ /**
115
+ * Clear the Varnish cookie
116
+ * @private
117
+ */
118
+ _clearVarnishCookie(): void;
119
+ /**
120
+ * Log used settings and version
121
+ * @throws {SDKError} - If log method is not provided
122
+ */
123
+ logSettings(): void;
124
+ /**
125
+ * @summary Queries the hassession endpoint and returns information about the status of the user
126
+ * @remarks When we send a request to this endpoint, cookies sent along with the request
127
+ * determines the status of the user.
128
+ * @throws {SDKError} - If the call to the hasSession service fails in any way (this will happen
129
+ * if, say, the user is not logged in)
130
+ * @fires Identity#login
131
+ * @fires Identity#logout
132
+ * @fires Identity#userChange
133
+ * @fires Identity#sessionChange
134
+ * @fires Identity#notLoggedin
135
+ * @fires Identity#sessionInit
136
+ * @fires Identity#statusChange
137
+ * @fires Identity#error
138
+ */
139
+ hasSession(): Promise<SessionResponse>;
140
+ /**
141
+ * @summary Allows the client app to check if the user is logged in to Schibsted account
142
+ * @remarks This function calls {@link Identity#hasSession} internally and thus has the side
143
+ * effect that it might perform an auto-login on the user
144
+ */
145
+ isLoggedIn(): Promise<boolean>;
146
+ /**
147
+ * Removes the cached user session.
148
+ */
149
+ clearCachedUserSession(): void;
150
+ /**
151
+ * @summary Allows the caller to check if the current user is connected to the client_id in
152
+ * Schibsted account. Being connected means that the user has agreed for their account to be
153
+ * used by your web app and have accepted the required terms
154
+ * @remarks This function calls {@link Identity#hasSession} internally and thus has the side
155
+ * effect that it might perform an auto-login on the user
156
+ * @summary Check if the user is connected to the client_id
157
+ */
158
+ isConnected(): Promise<boolean>;
159
+ /**
160
+ * @summary Returns information about the user
161
+ * @remarks This function calls {@link Identity#hasSession} internally and thus has the side
162
+ * effect that it might perform an auto-login on the user
163
+ * @throws {SDKError} If the user isn't connected to the merchant
164
+ * @throws {SDKError} If we couldn't get the user
165
+ */
166
+ getUser(): Promise<ConnectedSessionResponse>;
167
+ /**
168
+ * @summary
169
+ * In Schibsted account, there are multiple ways of identifying a user; the `userId`,
170
+ * `uuid` and `externalId` used for identifying a user-merchant pair (see {@link Identity#getExternalId}).
171
+ * There are reasons for them all to exist. The `userId` is a numeric identifier, but
172
+ * since Schibsted account is deployed separately in Norway and Sweden, there are a lot of
173
+ * duplicates. The `userId` was introduced early, so many sites still need to use them for
174
+ * legacy reasons. The `uuid` is universally unique, and so — if we could disregard a lot of
175
+ * Schibsted components depending on the numeric `userId` — it would be a good identifier to use
176
+ * @remarks This function calls {@link Identity#hasSession} internally and thus has the side
177
+ * effect that it might perform an auto-login on the user
178
+ * @throws {SDKError} If the user isn't connected to the merchant
179
+ * @return The `userId` field (not to be confused with the `uuid`)
180
+ */
181
+ getUserId(): Promise<string>;
182
+ /**
183
+ * @function
184
+ * @summary
185
+ * Retrieves the external identifier (`externalId`) for the authenticated user.
186
+ *
187
+ * In Schibsted Account there are multiple ways of identifying users, however for integrations with
188
+ * third-parties it's recommended to use `externalId` as it does not disclose
189
+ * any critical data whilst allowing for user identification.
190
+ *
191
+ * `externalId` is merchant-scoped using a pairwise identifier (`pairId`),
192
+ * meaning the same user's ID will differ between merchants.
193
+ * Additionally, this identifier is bound to the external party provided as argument.
194
+ *
195
+ * @param externalParty
196
+ * @param optionalSuffix
197
+ * @remarks This function calls {@link Identity#hasSession} internally and thus has the side
198
+ * effect that it might perform an auto-login on the user
199
+ * @throws {SDKError} If the `pairId` is missing in user session.
200
+ * @throws {SDKError} If the `externalParty` is not defined
201
+ * @return The merchant- and 3rd-party-specific `externalId`
202
+ */
203
+ getExternalId(externalParty: string, optionalSuffix?: string): Promise<string>;
204
+ /**
205
+ * @summary Enables brands to programmatically get the current the SDRN based on the user's session.
206
+ * @remarks This function calls {@link Identity#hasSession} internally and thus has the side
207
+ * effect that it might perform an auto-login on the user
208
+ * @throws {SDKError} If the SDRN is missing in user session object.
209
+ */
210
+ getUserSDRN(): Promise<string>;
211
+ /**
212
+ * @summary In Schibsted account, there are two ways of identifying a user; the `userId` and the
213
+ * `uuid`. There are reasons for them both existing. The `userId` is a numeric identifier, but
214
+ * since Schibsted account is deployed separately in Norway and Sweden, there are a lot of
215
+ * duplicates. The `userId` was introduced early, so many sites still need to use them for
216
+ * legacy reasons. The `uuid` is universally unique, and so — if we could disregard a lot of
217
+ * Schibsted components depending on the numeric `userId` — it would be a good identifier to use
218
+ * @remarks This function calls {@link Identity#hasSession} internally and thus has the side
219
+ * effect that it might perform an auto-login on the user
220
+ * @throws {SDKError} If the user isn't connected to the merchant
221
+ * @return The `uuid` field (not to be confused with the `userId`)
222
+ */
223
+ getUserUuid(): Promise<string>;
224
+ /**
225
+ * @summary Get basic information about any user currently logged-in to their Schibsted account
226
+ * in this browser. Can be used to provide context in a continue-as prompt.
227
+ * @remarks This function relies on the global Schibsted account user session cookie, which
228
+ * is a third-party cookie and hence might be blocked by the browser (for example due to ITP in
229
+ * Safari). So there's no guarantee any data is returned, even though a user is logged-in in
230
+ * the current browser.
231
+ */
232
+ getUserContextData(): Promise<SimplifiedLoginData | null>;
233
+ /**
234
+ * If a popup is desired, this function needs to be called in response to a user event (like
235
+ * click or tap) in order to work correctly. Otherwise the popup will be blocked by the
236
+ * browser's popup blockers and has to be explicitly authorized to be shown.
237
+ * @summary Perform a login, either using a full-page redirect or a popup
238
+ * @see https://tools.ietf.org/html/rfc6749#section-4.1.1
239
+ *
240
+ * @param options
241
+ * @param options.state
242
+ * @param options.acrValues
243
+ * @param options.scope
244
+ * @param options.redirectUri
245
+ * @param options.preferPopup
246
+ * @param options.loginHint
247
+ * @param options.tag
248
+ * @param options.teaser
249
+ * @param options.maxAge
250
+ * @param options.locale
251
+ * @param options.oneStepLogin
252
+ * @param options.prompt
253
+ * @param options.xDomainId
254
+ * @param options.xEnvironmentId
255
+ * @param options.originCampaign
256
+ * @return - Reference to popup window if created (or `null` otherwise)
257
+ */
258
+ login({ state, acrValues, scope, redirectUri, preferPopup, loginHint, tag, teaser, maxAge, locale, oneStepLogin, prompt, xDomainId, xEnvironmentId, originCampaign, }: LoginOptions): Window | null;
259
+ /**
260
+ * @summary Retrieve the sp_id (Varnish ID)
261
+ * @remarks This function calls {@link Identity#hasSession} internally and thus has the side
262
+ * effect that it might perform an auto-login on the user
263
+ * @return - The sp_id string or null (if the server didn't return it)
264
+ */
265
+ getSpId(): Promise<string | null>;
266
+ /**
267
+ * @summary Logs the user out from the Identity platform
268
+ * @param redirectUri - Where to redirect the browser after logging out of Schibsted
269
+ * account
270
+ */
271
+ logout(redirectUri?: string): void;
272
+ /**
273
+ * Generates the link to the new login page that'll be used in the popup or redirect flow
274
+ * @param options
275
+ * @param options.state
276
+ * @param options.acrValues
277
+ * @param options.scope
278
+ * @param options.redirectUri
279
+ * @param options.loginHint
280
+ * @param options.tag
281
+ * @param options.teaser
282
+ * @param options.maxAge
283
+ * @param options.locale
284
+ * @param options.oneStepLogin
285
+ * @param options.prompt
286
+ * @param options.xDomainId
287
+ * @param options.xEnvironmentId
288
+ * @param options.originCampaign
289
+ * @return - The url
290
+ */
291
+ loginUrl({ state, acrValues, scope, redirectUri, loginHint, tag, teaser, maxAge, locale, oneStepLogin, prompt, xDomainId, xEnvironmentId, originCampaign, }: LoginOptions): string;
292
+ /**
293
+ * The url for logging the user out
294
+ * @param redirectUri
295
+ * @return url
296
+ */
297
+ logoutUrl(redirectUri?: string): string;
298
+ /**
299
+ * The account summary page url
300
+ * @param redirectUri
301
+ */
302
+ accountUrl(redirectUri?: string): string;
303
+ /**
304
+ * The phone editing page url
305
+ * @param redirectUri
306
+ */
307
+ phonesUrl(redirectUri?: string): string;
308
+ /**
309
+ * Function responsible for loading and displaying simplified login widget. How often
310
+ * widget will be display is up to you. Preferred way would be to show it once per user,
311
+ * and store that info in localStorage. Widget will be display only if user is logged in to SSO.
312
+ *
313
+ * @param loginParams - the same as `options` param for login function. Login will be called on user
314
+ * continue action. `state` might be string or async function.
315
+ * @param options - additional configuration of Simplified Login Widget
316
+ * @fires Identity#simplifiedLoginOpened
317
+ * @fires Identity#simplifiedLoginCancelled
318
+ * @throws {SDKError} - If user context data is missing or the widget script cannot be loaded
319
+ * @return - will resolve to true if widget will be displayed
320
+ */
321
+ showSimplifiedLoginWidget(loginParams: SimplifiedLoginWidgetLoginOptions, options?: SimplifiedLoginWidgetOptions): Promise<boolean>;
322
+ }
323
+ export default Identity;
@@ -0,0 +1,8 @@
1
+ export { Account } from './account.js';
2
+ export { createAccountSdk, getAccountSdk } from './account-sdk.js';
3
+ export { default as SDKError } from './sdk-error.js';
4
+ export type { AccountConfig, AccountEventHandler, AccountEventName, AccountSessionEventName, } from './types/account.js';
5
+ export type { AccountEnvironment, LogFunction } from './types/common.js';
6
+ export type { LoginOptions, SimplifiedLoginData, SimplifiedLoginWidgetLoginOptions, SimplifiedLoginWidgetOptions, } from './types/login.js';
7
+ export type { HasAccessResult } from './types/monetization.js';
8
+ export type { ConnectedSessionResponse, ConnectedSessionWithUserIdResponse, RedirectSessionResponse, SessionFailureResponse, SessionResponse, SessionSuccessResponse, SessionUserId, } from './types/session.js';