@capgo/capacitor-social-login 8.5.3 → 8.5.4

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.
@@ -2,6 +2,40 @@
2
2
 
3
3
  var core = require('@capacitor/core');
4
4
 
5
+ /******************************************************************************
6
+ Copyright (c) Microsoft Corporation.
7
+
8
+ Permission to use, copy, modify, and/or distribute this software for any
9
+ purpose with or without fee is hereby granted.
10
+
11
+ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
12
+ REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
13
+ AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
14
+ INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
15
+ LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
16
+ OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
17
+ PERFORMANCE OF THIS SOFTWARE.
18
+ ***************************************************************************** */
19
+ /* global Reflect, Promise, SuppressedError, Symbol, Iterator */
20
+
21
+
22
+ function __rest(s, e) {
23
+ var t = {};
24
+ for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
25
+ t[p] = s[p];
26
+ if (s != null && typeof Object.getOwnPropertySymbols === "function")
27
+ for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
28
+ if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
29
+ t[p[i]] = s[p[i]];
30
+ }
31
+ return t;
32
+ }
33
+
34
+ typeof SuppressedError === "function" ? SuppressedError : function (error, suppressed, message) {
35
+ var e = new Error(message);
36
+ return e.name = "SuppressedError", e.error = error, e.suppressed = suppressed, e;
37
+ };
38
+
5
39
  const USER_CANCELLED_CODE = 'USER_CANCELLED';
6
40
  const CANCELLED_PATTERNS = [
7
41
  'access_denied',
@@ -680,40 +714,6 @@ class GoogleSocialLogin extends BaseSocialLogin {
680
714
  }
681
715
  }
682
716
 
683
- /******************************************************************************
684
- Copyright (c) Microsoft Corporation.
685
-
686
- Permission to use, copy, modify, and/or distribute this software for any
687
- purpose with or without fee is hereby granted.
688
-
689
- THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
690
- REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
691
- AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
692
- INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
693
- LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
694
- OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
695
- PERFORMANCE OF THIS SOFTWARE.
696
- ***************************************************************************** */
697
- /* global Reflect, Promise, SuppressedError, Symbol, Iterator */
698
-
699
-
700
- function __rest(s, e) {
701
- var t = {};
702
- for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
703
- t[p] = s[p];
704
- if (s != null && typeof Object.getOwnPropertySymbols === "function")
705
- for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
706
- if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
707
- t[p[i]] = s[p[i]];
708
- }
709
- return t;
710
- }
711
-
712
- typeof SuppressedError === "function" ? SuppressedError : function (error, suppressed, message) {
713
- var e = new Error(message);
714
- return e.name = "SuppressedError", e.error = error, e.suppressed = suppressed, e;
715
- };
716
-
717
717
  /**
718
718
  * OAuth2 Social Login Manager
719
719
  * Supports multiple OAuth2 provider configurations
@@ -2180,39 +2180,138 @@ function initOAuthPopupRedirectHandler() {
2180
2180
  // Auto-run on import so popup windows finish OAuth even before plugin lazy-load
2181
2181
  initOAuthPopupRedirectHandler();
2182
2182
 
2183
+ const LINKEDIN_PROVIDER_ID = 'linkedin';
2184
+ const LINKEDIN_REDIRECT_PENDING_KEY = 'capgo_social_login_linkedin_redirect';
2185
+ const LINKEDIN_AUTH_URL = 'https://www.linkedin.com/oauth/v2/authorization';
2186
+ const LINKEDIN_TOKEN_URL = 'https://www.linkedin.com/oauth/v2/accessToken';
2187
+ const LINKEDIN_RESOURCE_URL = 'https://api.linkedin.com/v2/userinfo';
2188
+ const LINKEDIN_DEFAULT_SCOPE = 'openid profile email';
2189
+ const normalizeScope = (scope) => {
2190
+ if (!scope)
2191
+ return LINKEDIN_DEFAULT_SCOPE;
2192
+ if (Array.isArray(scope)) {
2193
+ return scope.filter(Boolean).join(' ');
2194
+ }
2195
+ return scope;
2196
+ };
2197
+ const buildLinkedInOAuthConfig = (config) => {
2198
+ var _a, _b, _c, _d, _e, _f, _g, _h, _j;
2199
+ const scope = normalizeScope((_a = config.scope) !== null && _a !== void 0 ? _a : config.scopes);
2200
+ return Object.assign(Object.assign({}, config), { appId: (_b = config.appId) !== null && _b !== void 0 ? _b : config.clientId, clientSecret: config.clientSecret, authorizationBaseUrl: (_d = (_c = config.authorizationBaseUrl) !== null && _c !== void 0 ? _c : config.authorizationEndpoint) !== null && _d !== void 0 ? _d : LINKEDIN_AUTH_URL, accessTokenEndpoint: (_f = (_e = config.accessTokenEndpoint) !== null && _e !== void 0 ? _e : config.tokenEndpoint) !== null && _f !== void 0 ? _f : LINKEDIN_TOKEN_URL, resourceUrl: (_g = config.resourceUrl) !== null && _g !== void 0 ? _g : LINKEDIN_RESOURCE_URL, responseType: (_h = config.responseType) !== null && _h !== void 0 ? _h : 'code', pkceEnabled: (_j = config.pkceEnabled) !== null && _j !== void 0 ? _j : true, scope });
2201
+ };
2202
+ const createOAuthState = () => [...crypto.getRandomValues(new Uint8Array(16))].map((b) => b.toString(16).padStart(2, '0')).join('');
2203
+ const buildLinkedInLoginOptions = (options = {}) => {
2204
+ var _a;
2205
+ const scope = (_a = options.scope) !== null && _a !== void 0 ? _a : options.scopes;
2206
+ const loginOptions = Object.assign(Object.assign(Object.assign({}, options), { providerId: LINKEDIN_PROVIDER_ID }), (scope === undefined ? {} : { scope }));
2207
+ // Bind convenience-redirect remapping to this OAuth state, not a generic sentinel.
2208
+ if (loginOptions.flow === 'redirect' && !loginOptions.state) {
2209
+ loginOptions.state = createOAuthState();
2210
+ }
2211
+ return loginOptions;
2212
+ };
2213
+ const isLinkedInOAuthResult = (result) => result.provider === 'oauth2' && result.result.providerId === LINKEDIN_PROVIDER_ID;
2214
+ const asLinkedInLoginResult = (result) => ({
2215
+ provider: 'linkedin',
2216
+ result: result.result,
2217
+ });
2218
+ const markLinkedInRedirectPending = (state) => {
2219
+ try {
2220
+ sessionStorage.setItem(LINKEDIN_REDIRECT_PENDING_KEY, state);
2221
+ }
2222
+ catch (_a) {
2223
+ // sessionStorage may be unavailable
2224
+ }
2225
+ };
2226
+ const consumeLinkedInRedirectPending = (state) => {
2227
+ try {
2228
+ const pending = sessionStorage.getItem(LINKEDIN_REDIRECT_PENDING_KEY);
2229
+ sessionStorage.removeItem(LINKEDIN_REDIRECT_PENDING_KEY);
2230
+ return Boolean(pending && state && pending === state);
2231
+ }
2232
+ catch (_a) {
2233
+ return false;
2234
+ }
2235
+ };
2236
+
2183
2237
  const GOOGLE_OFFLINE_REFRESH_MESSAGE = "Google refresh() is not available when using offline mode. Offline mode only returns serverAuthCode for backend token exchange. Send serverAuthCode to your backend and refresh tokens there, or switch google.mode to 'online' for client-side refresh.";
2184
2238
  const rawSocialLogin = core.registerPlugin('SocialLogin', {
2185
2239
  web: () => Promise.resolve().then(function () { return web; }).then((m) => new m.SocialLoginWeb()),
2186
2240
  });
2187
2241
  class SocialLoginClient {
2242
+ normalizeInitializeOptions(options) {
2243
+ var _a;
2244
+ if (!options.linkedin) {
2245
+ return options;
2246
+ }
2247
+ const { linkedin, oauth2 } = options, rest = __rest(options, ["linkedin", "oauth2"]);
2248
+ return Object.assign(Object.assign({}, rest), { linkedin, oauth2: Object.assign(Object.assign({}, (oauth2 !== null && oauth2 !== void 0 ? oauth2 : {})), { [LINKEDIN_PROVIDER_ID]: (_a = oauth2 === null || oauth2 === void 0 ? void 0 : oauth2[LINKEDIN_PROVIDER_ID]) !== null && _a !== void 0 ? _a : buildLinkedInOAuthConfig(linkedin) }) });
2249
+ }
2188
2250
  constructor() {
2189
2251
  this.initialize = this.initialize.bind(this);
2190
2252
  this.refresh = this.refresh.bind(this);
2191
2253
  }
2192
2254
  async initialize(options) {
2193
- await rawSocialLogin.initialize(options);
2194
- this.initializeOptions = options;
2255
+ const normalized = this.normalizeInitializeOptions(options);
2256
+ await rawSocialLogin.initialize(normalized);
2257
+ this.initializeOptions = normalized;
2195
2258
  }
2196
2259
  async login(options) {
2260
+ var _a;
2261
+ if (options.provider === 'linkedin') {
2262
+ const linkedInOptions = (_a = options.options) !== null && _a !== void 0 ? _a : {};
2263
+ const oauthOptions = buildLinkedInLoginOptions(linkedInOptions);
2264
+ if (oauthOptions.flow === 'redirect' && oauthOptions.state) {
2265
+ markLinkedInRedirectPending(oauthOptions.state);
2266
+ }
2267
+ const response = await rawSocialLogin.login({
2268
+ provider: 'oauth2',
2269
+ options: oauthOptions,
2270
+ });
2271
+ return asLinkedInLoginResult(response);
2272
+ }
2197
2273
  return rawSocialLogin.login(options);
2198
2274
  }
2199
2275
  async logout(options) {
2276
+ if (options.provider === 'linkedin') {
2277
+ return rawSocialLogin.logout({ provider: 'oauth2', providerId: LINKEDIN_PROVIDER_ID });
2278
+ }
2200
2279
  return rawSocialLogin.logout(options);
2201
2280
  }
2202
2281
  async isLoggedIn(options) {
2282
+ if (options.provider === 'linkedin') {
2283
+ return rawSocialLogin.isLoggedIn({ provider: 'oauth2', providerId: LINKEDIN_PROVIDER_ID });
2284
+ }
2203
2285
  return rawSocialLogin.isLoggedIn(options);
2204
2286
  }
2205
2287
  async getAuthorizationCode(options) {
2288
+ if (options.provider === 'linkedin') {
2289
+ return rawSocialLogin.getAuthorizationCode({ provider: 'oauth2', providerId: LINKEDIN_PROVIDER_ID });
2290
+ }
2206
2291
  return rawSocialLogin.getAuthorizationCode(options);
2207
2292
  }
2208
2293
  async refresh(options) {
2209
- var _a, _b;
2294
+ var _a, _b, _c;
2210
2295
  if (options.provider === 'google' && ((_b = (_a = this.initializeOptions) === null || _a === void 0 ? void 0 : _a.google) === null || _b === void 0 ? void 0 : _b.mode) === 'offline') {
2211
2296
  console.warn(`[SocialLogin] ${GOOGLE_OFFLINE_REFRESH_MESSAGE}`);
2212
2297
  }
2298
+ if (options.provider === 'linkedin') {
2299
+ return rawSocialLogin.refresh({
2300
+ provider: 'oauth2',
2301
+ options: buildLinkedInLoginOptions((_c = options.options) !== null && _c !== void 0 ? _c : {}),
2302
+ });
2303
+ }
2213
2304
  return rawSocialLogin.refresh(options);
2214
2305
  }
2215
2306
  async refreshToken(options) {
2307
+ if (options.provider === 'linkedin') {
2308
+ return rawSocialLogin.refreshToken({
2309
+ provider: 'oauth2',
2310
+ providerId: LINKEDIN_PROVIDER_ID,
2311
+ refreshToken: options.refreshToken,
2312
+ additionalParameters: options.additionalParameters,
2313
+ });
2314
+ }
2216
2315
  return rawSocialLogin.refreshToken(options);
2217
2316
  }
2218
2317
  async handleRedirectCallback() {
@@ -2449,7 +2548,7 @@ const createAuthConnectClient = (client) => ({
2449
2548
  providerSpecificCall: async (options) => client.providerSpecificCall(options),
2450
2549
  getPluginVersion: async () => client.getPluginVersion(),
2451
2550
  });
2452
- const SocialLoginAuthConnect = createAuthConnectClient(SocialLoginBase);
2551
+ const SocialLoginAuthConnect = createAuthConnectClient(SocialLogin);
2453
2552
 
2454
2553
  class AppleSocialLogin extends BaseSocialLogin {
2455
2554
  constructor() {
@@ -2834,7 +2933,7 @@ class SocialLoginWeb extends core.WebPlugin {
2834
2933
  }
2835
2934
  }
2836
2935
  async initialize(options) {
2837
- var _a, _b, _c, _d, _e;
2936
+ var _a, _b, _c, _d, _e, _f;
2838
2937
  const initPromises = [];
2839
2938
  if ((_a = options.google) === null || _a === void 0 ? void 0 : _a.webClientId) {
2840
2939
  initPromises.push(this.googleProvider.initialize(options.google.webClientId, options.google.mode, options.google.hostedDomain, options.google.redirectUrl));
@@ -2851,12 +2950,17 @@ class SocialLoginWeb extends core.WebPlugin {
2851
2950
  if ((_e = options.telegram) === null || _e === void 0 ? void 0 : _e.botId) {
2852
2951
  this.telegramProvider.initialize(options.telegram.botId, options.telegram.requestAccess, options.telegram.redirectUrl, options.telegram.origin, options.telegram.languageCode);
2853
2952
  }
2854
- if (options.oauth2 && Object.keys(options.oauth2).length > 0) {
2855
- initPromises.push(this.oauth2Provider.initializeProviders(options.oauth2));
2953
+ const oauth2Configs = Object.assign({}, ((_f = options.oauth2) !== null && _f !== void 0 ? _f : {}));
2954
+ if (options.linkedin && !oauth2Configs[LINKEDIN_PROVIDER_ID]) {
2955
+ oauth2Configs[LINKEDIN_PROVIDER_ID] = buildLinkedInOAuthConfig(options.linkedin);
2956
+ }
2957
+ if (Object.keys(oauth2Configs).length > 0) {
2958
+ initPromises.push(this.oauth2Provider.initializeProviders(oauth2Configs));
2856
2959
  }
2857
2960
  await Promise.all(initPromises);
2858
2961
  }
2859
2962
  async login(options) {
2963
+ var _a;
2860
2964
  switch (options.provider) {
2861
2965
  case 'google':
2862
2966
  return this.googleProvider.login(options.options);
@@ -2868,6 +2972,15 @@ class SocialLoginWeb extends core.WebPlugin {
2868
2972
  return this.twitterProvider.login(options.options);
2869
2973
  case 'telegram':
2870
2974
  return this.telegramProvider.login(options.options);
2975
+ case 'linkedin': {
2976
+ const linkedInOptions = (_a = options.options) !== null && _a !== void 0 ? _a : {};
2977
+ const oauthOptions = buildLinkedInLoginOptions(linkedInOptions);
2978
+ if (oauthOptions.flow === 'redirect' && oauthOptions.state) {
2979
+ markLinkedInRedirectPending(oauthOptions.state);
2980
+ }
2981
+ const response = await this.oauth2Provider.login(oauthOptions);
2982
+ return asLinkedInLoginResult(response);
2983
+ }
2871
2984
  case 'oauth2':
2872
2985
  return this.oauth2Provider.login(options.options);
2873
2986
  default:
@@ -2886,6 +2999,8 @@ class SocialLoginWeb extends core.WebPlugin {
2886
2999
  return this.twitterProvider.logout();
2887
3000
  case 'telegram':
2888
3001
  return this.telegramProvider.logout();
3002
+ case 'linkedin':
3003
+ return this.oauth2Provider.logout(LINKEDIN_PROVIDER_ID);
2889
3004
  case 'oauth2':
2890
3005
  if (!options.providerId) {
2891
3006
  throw new Error('providerId is required for oauth2 logout');
@@ -2907,6 +3022,8 @@ class SocialLoginWeb extends core.WebPlugin {
2907
3022
  return this.twitterProvider.isLoggedIn();
2908
3023
  case 'telegram':
2909
3024
  return this.telegramProvider.isLoggedIn();
3025
+ case 'linkedin':
3026
+ return this.oauth2Provider.isLoggedIn(LINKEDIN_PROVIDER_ID);
2910
3027
  case 'oauth2':
2911
3028
  if (!options.providerId) {
2912
3029
  throw new Error('providerId is required for oauth2 isLoggedIn');
@@ -2926,6 +3043,8 @@ class SocialLoginWeb extends core.WebPlugin {
2926
3043
  return this.facebookProvider.getAuthorizationCode();
2927
3044
  case 'twitter':
2928
3045
  return this.twitterProvider.getAuthorizationCode();
3046
+ case 'linkedin':
3047
+ return this.oauth2Provider.getAuthorizationCode(LINKEDIN_PROVIDER_ID);
2929
3048
  case 'oauth2':
2930
3049
  if (!options.providerId) {
2931
3050
  throw new Error('providerId is required for oauth2 getAuthorizationCode');
@@ -2947,6 +3066,8 @@ class SocialLoginWeb extends core.WebPlugin {
2947
3066
  return this.twitterProvider.refresh();
2948
3067
  case 'telegram':
2949
3068
  return this.telegramProvider.refresh();
3069
+ case 'linkedin':
3070
+ return this.oauth2Provider.refresh(LINKEDIN_PROVIDER_ID);
2950
3071
  case 'oauth2': {
2951
3072
  const oauth2Options = options.options;
2952
3073
  if (!(oauth2Options === null || oauth2Options === void 0 ? void 0 : oauth2Options.providerId)) {
@@ -2976,19 +3097,28 @@ class SocialLoginWeb extends core.WebPlugin {
2976
3097
  }
2977
3098
  }
2978
3099
  async refreshToken(options) {
3100
+ if (options.provider === 'linkedin') {
3101
+ return this.oauth2Provider.refreshToken(LINKEDIN_PROVIDER_ID, options.refreshToken, options.additionalParameters);
3102
+ }
2979
3103
  if (options.provider !== 'oauth2') {
2980
- throw new Error('refreshToken is only implemented for oauth2 on web');
3104
+ throw new Error('refreshToken is only implemented for oauth2 and linkedin on web');
2981
3105
  }
2982
3106
  return this.oauth2Provider.refreshToken(options.providerId, options.refreshToken, options.additionalParameters);
2983
3107
  }
2984
3108
  async handleRedirectCallback() {
3109
+ var _a;
2985
3110
  const parsed = await this.parseRedirectResult();
3111
+ const callbackState = (_a = parsed.state) !== null && _a !== void 0 ? _a : new URL(window.location.href).searchParams.get('state');
3112
+ const pendingLinkedInRedirect = consumeLinkedInRedirectPending(callbackState);
2986
3113
  const result = parsed.result;
2987
3114
  if (!result)
2988
3115
  return null;
2989
3116
  if ('error' in result) {
2990
3117
  throw inferUserCancelledError(result.error);
2991
3118
  }
3119
+ if (pendingLinkedInRedirect && isLinkedInOAuthResult(result)) {
3120
+ return asLinkedInLoginResult(result);
3121
+ }
2992
3122
  return result;
2993
3123
  }
2994
3124
  async decodeIdToken(options) {
@@ -3062,8 +3192,8 @@ class SocialLoginWeb extends core.WebPlugin {
3062
3192
  SocialLoginWeb.OAUTH_STATE_KEY = OAUTH_STATE_KEY;
3063
3193
 
3064
3194
  var web = /*#__PURE__*/Object.freeze({
3065
- __proto__: null,
3066
- SocialLoginWeb: SocialLoginWeb
3195
+ __proto__: null,
3196
+ SocialLoginWeb: SocialLoginWeb
3067
3197
  });
3068
3198
 
3069
3199
  exports.SocialLogin = SocialLogin;