@capgo/capacitor-social-login 8.1.1 → 8.2.1
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/README.md +215 -35
- package/android/src/main/AndroidManifest.xml +4 -0
- package/android/src/main/java/ee/forgr/capacitor/social/login/OAuth2LoginActivity.java +110 -0
- package/android/src/main/java/ee/forgr/capacitor/social/login/OAuth2Provider.java +848 -0
- package/android/src/main/java/ee/forgr/capacitor/social/login/SocialLoginPlugin.java +27 -1
- package/dist/docs.json +352 -22
- package/dist/esm/definitions.d.ts +167 -3
- package/dist/esm/definitions.js.map +1 -1
- package/dist/esm/oauth2-provider.d.ts +41 -0
- package/dist/esm/oauth2-provider.js +444 -0
- package/dist/esm/oauth2-provider.js.map +1 -0
- package/dist/esm/web.d.ts +3 -1
- package/dist/esm/web.js +32 -0
- package/dist/esm/web.js.map +1 -1
- package/dist/plugin.cjs.js +474 -0
- package/dist/plugin.cjs.js.map +1 -1
- package/dist/plugin.js +474 -0
- package/dist/plugin.js.map +1 -1
- package/ios/Sources/SocialLoginPlugin/OAuth2Provider.swift +575 -0
- package/ios/Sources/SocialLoginPlugin/SocialLoginPlugin.swift +111 -2
- package/package.json +2 -1
package/dist/plugin.js
CHANGED
|
@@ -611,6 +611,449 @@ var capacitorCapacitorUpdater = (function (exports, core) {
|
|
|
611
611
|
}
|
|
612
612
|
}
|
|
613
613
|
|
|
614
|
+
var __rest$1 = (undefined && undefined.__rest) || function (s, e) {
|
|
615
|
+
var t = {};
|
|
616
|
+
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
|
|
617
|
+
t[p] = s[p];
|
|
618
|
+
if (s != null && typeof Object.getOwnPropertySymbols === "function")
|
|
619
|
+
for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
|
|
620
|
+
if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
|
|
621
|
+
t[p[i]] = s[p[i]];
|
|
622
|
+
}
|
|
623
|
+
return t;
|
|
624
|
+
};
|
|
625
|
+
/**
|
|
626
|
+
* OAuth2 Social Login Manager
|
|
627
|
+
* Supports multiple OAuth2 provider configurations
|
|
628
|
+
*/
|
|
629
|
+
class OAuth2SocialLogin extends BaseSocialLogin {
|
|
630
|
+
constructor() {
|
|
631
|
+
super(...arguments);
|
|
632
|
+
this.providers = new Map();
|
|
633
|
+
this.TOKENS_KEY_PREFIX = 'capgo_social_login_oauth2_tokens_';
|
|
634
|
+
this.STATE_PREFIX = 'capgo_social_login_oauth2_state_';
|
|
635
|
+
}
|
|
636
|
+
/**
|
|
637
|
+
* Initialize multiple OAuth2 providers
|
|
638
|
+
*/
|
|
639
|
+
async initializeProviders(configs) {
|
|
640
|
+
var _a, _b, _c, _d;
|
|
641
|
+
for (const [providerId, config] of Object.entries(configs)) {
|
|
642
|
+
if (!config.appId || !config.authorizationBaseUrl || !config.redirectUrl) {
|
|
643
|
+
throw new Error(`OAuth2 provider '${providerId}' requires appId, authorizationBaseUrl, and redirectUrl`);
|
|
644
|
+
}
|
|
645
|
+
const internalConfig = Object.assign(Object.assign({}, config), { responseType: (_a = config.responseType) !== null && _a !== void 0 ? _a : 'code', pkceEnabled: (_b = config.pkceEnabled) !== null && _b !== void 0 ? _b : true, scope: (_c = config.scope) !== null && _c !== void 0 ? _c : '', logsEnabled: (_d = config.logsEnabled) !== null && _d !== void 0 ? _d : false });
|
|
646
|
+
this.providers.set(providerId, internalConfig);
|
|
647
|
+
if (internalConfig.logsEnabled) {
|
|
648
|
+
console.log(`[OAuth2:${providerId}] Initialized with config:`, {
|
|
649
|
+
appId: config.appId,
|
|
650
|
+
authorizationBaseUrl: config.authorizationBaseUrl,
|
|
651
|
+
redirectUrl: config.redirectUrl,
|
|
652
|
+
responseType: internalConfig.responseType,
|
|
653
|
+
pkceEnabled: internalConfig.pkceEnabled,
|
|
654
|
+
});
|
|
655
|
+
}
|
|
656
|
+
}
|
|
657
|
+
}
|
|
658
|
+
getProvider(providerId) {
|
|
659
|
+
const config = this.providers.get(providerId);
|
|
660
|
+
if (!config) {
|
|
661
|
+
throw new Error(`OAuth2 provider '${providerId}' not configured. Call initialize() first.`);
|
|
662
|
+
}
|
|
663
|
+
return config;
|
|
664
|
+
}
|
|
665
|
+
getTokensKey(providerId) {
|
|
666
|
+
return `${this.TOKENS_KEY_PREFIX}${providerId}`;
|
|
667
|
+
}
|
|
668
|
+
async login(options) {
|
|
669
|
+
var _a, _b, _c, _d;
|
|
670
|
+
const { providerId } = options;
|
|
671
|
+
const config = this.getProvider(providerId);
|
|
672
|
+
const redirectUri = (_a = options.redirectUrl) !== null && _a !== void 0 ? _a : config.redirectUrl;
|
|
673
|
+
const scope = (_b = options.scope) !== null && _b !== void 0 ? _b : config.scope;
|
|
674
|
+
const state = (_c = options.state) !== null && _c !== void 0 ? _c : this.generateState();
|
|
675
|
+
const codeVerifier = (_d = options.codeVerifier) !== null && _d !== void 0 ? _d : this.generateCodeVerifier();
|
|
676
|
+
// Build authorization URL
|
|
677
|
+
const params = new URLSearchParams({
|
|
678
|
+
response_type: config.responseType,
|
|
679
|
+
client_id: config.appId,
|
|
680
|
+
redirect_uri: redirectUri,
|
|
681
|
+
state,
|
|
682
|
+
});
|
|
683
|
+
if (scope) {
|
|
684
|
+
params.set('scope', scope);
|
|
685
|
+
}
|
|
686
|
+
// Add PKCE for code flow
|
|
687
|
+
if (config.responseType === 'code' && config.pkceEnabled) {
|
|
688
|
+
const codeChallenge = await this.generateCodeChallenge(codeVerifier);
|
|
689
|
+
params.set('code_challenge', codeChallenge);
|
|
690
|
+
params.set('code_challenge_method', 'S256');
|
|
691
|
+
}
|
|
692
|
+
// Add additional parameters from config
|
|
693
|
+
if (config.additionalParameters) {
|
|
694
|
+
for (const [key, value] of Object.entries(config.additionalParameters)) {
|
|
695
|
+
params.set(key, value);
|
|
696
|
+
}
|
|
697
|
+
}
|
|
698
|
+
// Add additional parameters from login options
|
|
699
|
+
if (options.additionalParameters) {
|
|
700
|
+
for (const [key, value] of Object.entries(options.additionalParameters)) {
|
|
701
|
+
params.set(key, value);
|
|
702
|
+
}
|
|
703
|
+
}
|
|
704
|
+
// Store pending login state
|
|
705
|
+
this.persistPendingLogin(state, {
|
|
706
|
+
providerId,
|
|
707
|
+
codeVerifier,
|
|
708
|
+
redirectUri,
|
|
709
|
+
scope,
|
|
710
|
+
});
|
|
711
|
+
localStorage.setItem(BaseSocialLogin.OAUTH_STATE_KEY, JSON.stringify({ provider: 'oauth2', providerId, state }));
|
|
712
|
+
const authUrl = `${config.authorizationBaseUrl}?${params.toString()}`;
|
|
713
|
+
if (config.logsEnabled) {
|
|
714
|
+
console.log(`[OAuth2:${providerId}] Opening authorization URL:`, authUrl);
|
|
715
|
+
}
|
|
716
|
+
// Open popup window
|
|
717
|
+
const width = 500;
|
|
718
|
+
const height = 650;
|
|
719
|
+
const left = window.screenX + (window.outerWidth - width) / 2;
|
|
720
|
+
const top = window.screenY + (window.outerHeight - height) / 2;
|
|
721
|
+
const popup = window.open(authUrl, 'OAuth2Login', `width=${width},height=${height},left=${left},top=${top},popup=1`);
|
|
722
|
+
return new Promise((resolve, reject) => {
|
|
723
|
+
if (!popup) {
|
|
724
|
+
reject(new Error('Unable to open login window. Please allow popups.'));
|
|
725
|
+
return;
|
|
726
|
+
}
|
|
727
|
+
const cleanup = (messageHandler, timeoutHandle, intervalHandle) => {
|
|
728
|
+
window.removeEventListener('message', messageHandler);
|
|
729
|
+
clearTimeout(timeoutHandle);
|
|
730
|
+
clearInterval(intervalHandle);
|
|
731
|
+
};
|
|
732
|
+
const messageHandler = (event) => {
|
|
733
|
+
var _a, _b, _c, _d, _e;
|
|
734
|
+
if (event.origin !== window.location.origin) {
|
|
735
|
+
return;
|
|
736
|
+
}
|
|
737
|
+
if (((_a = event.data) === null || _a === void 0 ? void 0 : _a.type) === 'oauth-response') {
|
|
738
|
+
if (((_b = event.data) === null || _b === void 0 ? void 0 : _b.provider) && event.data.provider !== 'oauth2') {
|
|
739
|
+
return;
|
|
740
|
+
}
|
|
741
|
+
// Check providerId matches if present
|
|
742
|
+
if (((_c = event.data) === null || _c === void 0 ? void 0 : _c.providerId) && event.data.providerId !== providerId) {
|
|
743
|
+
return;
|
|
744
|
+
}
|
|
745
|
+
cleanup(messageHandler, timeoutHandle, popupClosedInterval);
|
|
746
|
+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
747
|
+
const _f = event.data, { provider: _ignoredProvider, type: _ignoredType } = _f, payload = __rest$1(_f, ["provider", "type"]);
|
|
748
|
+
resolve({
|
|
749
|
+
provider: 'oauth2',
|
|
750
|
+
result: payload,
|
|
751
|
+
});
|
|
752
|
+
}
|
|
753
|
+
else if (((_d = event.data) === null || _d === void 0 ? void 0 : _d.type) === 'oauth-error') {
|
|
754
|
+
if (((_e = event.data) === null || _e === void 0 ? void 0 : _e.provider) && event.data.provider !== 'oauth2') {
|
|
755
|
+
return;
|
|
756
|
+
}
|
|
757
|
+
cleanup(messageHandler, timeoutHandle, popupClosedInterval);
|
|
758
|
+
reject(new Error(event.data.error || 'OAuth2 login was cancelled.'));
|
|
759
|
+
}
|
|
760
|
+
};
|
|
761
|
+
window.addEventListener('message', messageHandler);
|
|
762
|
+
const timeoutHandle = window.setTimeout(() => {
|
|
763
|
+
window.removeEventListener('message', messageHandler);
|
|
764
|
+
popup.close();
|
|
765
|
+
reject(new Error('OAuth2 login timed out.'));
|
|
766
|
+
}, 300000);
|
|
767
|
+
const popupClosedInterval = window.setInterval(() => {
|
|
768
|
+
if (popup.closed) {
|
|
769
|
+
window.removeEventListener('message', messageHandler);
|
|
770
|
+
clearInterval(popupClosedInterval);
|
|
771
|
+
clearTimeout(timeoutHandle);
|
|
772
|
+
reject(new Error('OAuth2 login window was closed.'));
|
|
773
|
+
}
|
|
774
|
+
}, 1000);
|
|
775
|
+
});
|
|
776
|
+
}
|
|
777
|
+
async logout(providerId) {
|
|
778
|
+
const config = this.providers.get(providerId);
|
|
779
|
+
localStorage.removeItem(this.getTokensKey(providerId));
|
|
780
|
+
// If logout URL is configured, redirect to it
|
|
781
|
+
if (config === null || config === void 0 ? void 0 : config.logoutUrl) {
|
|
782
|
+
window.open(config.logoutUrl, '_blank');
|
|
783
|
+
}
|
|
784
|
+
}
|
|
785
|
+
async isLoggedIn(providerId) {
|
|
786
|
+
const tokens = this.getStoredTokens(providerId);
|
|
787
|
+
if (!tokens) {
|
|
788
|
+
return { isLoggedIn: false };
|
|
789
|
+
}
|
|
790
|
+
const isValid = tokens.expiresAt > Date.now();
|
|
791
|
+
if (!isValid) {
|
|
792
|
+
localStorage.removeItem(this.getTokensKey(providerId));
|
|
793
|
+
}
|
|
794
|
+
return { isLoggedIn: isValid };
|
|
795
|
+
}
|
|
796
|
+
async getAuthorizationCode(providerId) {
|
|
797
|
+
const tokens = this.getStoredTokens(providerId);
|
|
798
|
+
if (!tokens) {
|
|
799
|
+
throw new Error(`OAuth2 access token is not available for provider '${providerId}'.`);
|
|
800
|
+
}
|
|
801
|
+
return {
|
|
802
|
+
accessToken: tokens.accessToken,
|
|
803
|
+
jwt: tokens.idToken,
|
|
804
|
+
};
|
|
805
|
+
}
|
|
806
|
+
async refresh(providerId) {
|
|
807
|
+
const tokens = this.getStoredTokens(providerId);
|
|
808
|
+
if (!(tokens === null || tokens === void 0 ? void 0 : tokens.refreshToken)) {
|
|
809
|
+
throw new Error(`No OAuth2 refresh token is available for provider '${providerId}'. Include offline_access scope to receive one.`);
|
|
810
|
+
}
|
|
811
|
+
const config = this.getProvider(providerId);
|
|
812
|
+
if (!config.accessTokenEndpoint) {
|
|
813
|
+
throw new Error(`No accessTokenEndpoint configured for provider '${providerId}'.`);
|
|
814
|
+
}
|
|
815
|
+
await this.refreshWithRefreshToken(providerId, tokens.refreshToken);
|
|
816
|
+
}
|
|
817
|
+
async handleOAuthRedirect(url, expectedState) {
|
|
818
|
+
var _a, _b, _c, _d, _e;
|
|
819
|
+
// Check both query params and hash fragment
|
|
820
|
+
const params = new URLSearchParams(url.search);
|
|
821
|
+
const hashParams = new URLSearchParams(url.hash.slice(1));
|
|
822
|
+
// Merge params, hash takes priority (for implicit flow)
|
|
823
|
+
hashParams.forEach((value, key) => {
|
|
824
|
+
params.set(key, value);
|
|
825
|
+
});
|
|
826
|
+
const stateFromUrl = expectedState !== null && expectedState !== void 0 ? expectedState : params.get('state');
|
|
827
|
+
if (!stateFromUrl) {
|
|
828
|
+
return null;
|
|
829
|
+
}
|
|
830
|
+
const pending = this.consumePendingLogin(stateFromUrl);
|
|
831
|
+
if (!pending) {
|
|
832
|
+
localStorage.removeItem(BaseSocialLogin.OAUTH_STATE_KEY);
|
|
833
|
+
return { error: 'OAuth2 login session expired or state mismatch.' };
|
|
834
|
+
}
|
|
835
|
+
const { providerId } = pending;
|
|
836
|
+
const config = this.providers.get(providerId);
|
|
837
|
+
if (!config) {
|
|
838
|
+
localStorage.removeItem(BaseSocialLogin.OAUTH_STATE_KEY);
|
|
839
|
+
return { error: `OAuth2 provider '${providerId}' configuration not found.` };
|
|
840
|
+
}
|
|
841
|
+
const error = params.get('error');
|
|
842
|
+
if (error) {
|
|
843
|
+
localStorage.removeItem(BaseSocialLogin.OAUTH_STATE_KEY);
|
|
844
|
+
return { error: params.get('error_description') || error };
|
|
845
|
+
}
|
|
846
|
+
try {
|
|
847
|
+
let tokenResponse;
|
|
848
|
+
// Check response type
|
|
849
|
+
if (params.has('code')) {
|
|
850
|
+
// Authorization code flow
|
|
851
|
+
const code = params.get('code');
|
|
852
|
+
if (!code) {
|
|
853
|
+
localStorage.removeItem(BaseSocialLogin.OAUTH_STATE_KEY);
|
|
854
|
+
return { error: 'OAuth2 authorization code missing from redirect.' };
|
|
855
|
+
}
|
|
856
|
+
tokenResponse = await this.exchangeAuthorizationCode(providerId, code, pending);
|
|
857
|
+
}
|
|
858
|
+
else if (params.has('access_token')) {
|
|
859
|
+
// Implicit flow
|
|
860
|
+
tokenResponse = {
|
|
861
|
+
access_token: params.get('access_token'),
|
|
862
|
+
token_type: params.get('token_type') || 'bearer',
|
|
863
|
+
expires_in: params.has('expires_in') ? parseInt(params.get('expires_in'), 10) : undefined,
|
|
864
|
+
scope: params.get('scope') || undefined,
|
|
865
|
+
id_token: params.get('id_token') || undefined,
|
|
866
|
+
};
|
|
867
|
+
}
|
|
868
|
+
else {
|
|
869
|
+
localStorage.removeItem(BaseSocialLogin.OAUTH_STATE_KEY);
|
|
870
|
+
return { error: 'No authorization code or access token in redirect.' };
|
|
871
|
+
}
|
|
872
|
+
const expiresAt = tokenResponse.expires_in ? Date.now() + tokenResponse.expires_in * 1000 : Date.now() + 3600000;
|
|
873
|
+
const scopeArray = (_b = (_a = tokenResponse.scope) === null || _a === void 0 ? void 0 : _a.split(' ').filter(Boolean)) !== null && _b !== void 0 ? _b : [];
|
|
874
|
+
// Fetch resource data if configured
|
|
875
|
+
let resourceData = null;
|
|
876
|
+
if (config.resourceUrl) {
|
|
877
|
+
resourceData = await this.fetchResource(providerId, tokenResponse.access_token);
|
|
878
|
+
}
|
|
879
|
+
this.persistTokens(providerId, {
|
|
880
|
+
accessToken: tokenResponse.access_token,
|
|
881
|
+
refreshToken: tokenResponse.refresh_token,
|
|
882
|
+
idToken: tokenResponse.id_token,
|
|
883
|
+
expiresAt,
|
|
884
|
+
scope: scopeArray,
|
|
885
|
+
tokenType: tokenResponse.token_type,
|
|
886
|
+
});
|
|
887
|
+
return {
|
|
888
|
+
provider: 'oauth2',
|
|
889
|
+
result: {
|
|
890
|
+
providerId,
|
|
891
|
+
accessToken: {
|
|
892
|
+
token: tokenResponse.access_token,
|
|
893
|
+
tokenType: tokenResponse.token_type,
|
|
894
|
+
expires: new Date(expiresAt).toISOString(),
|
|
895
|
+
refreshToken: tokenResponse.refresh_token,
|
|
896
|
+
},
|
|
897
|
+
idToken: (_c = tokenResponse.id_token) !== null && _c !== void 0 ? _c : null,
|
|
898
|
+
refreshToken: (_d = tokenResponse.refresh_token) !== null && _d !== void 0 ? _d : null,
|
|
899
|
+
resourceData,
|
|
900
|
+
scope: scopeArray,
|
|
901
|
+
tokenType: tokenResponse.token_type,
|
|
902
|
+
expiresIn: (_e = tokenResponse.expires_in) !== null && _e !== void 0 ? _e : null,
|
|
903
|
+
},
|
|
904
|
+
};
|
|
905
|
+
}
|
|
906
|
+
catch (err) {
|
|
907
|
+
if (err instanceof Error) {
|
|
908
|
+
return { error: err.message };
|
|
909
|
+
}
|
|
910
|
+
return { error: 'OAuth2 login failed unexpectedly.' };
|
|
911
|
+
}
|
|
912
|
+
finally {
|
|
913
|
+
localStorage.removeItem(BaseSocialLogin.OAUTH_STATE_KEY);
|
|
914
|
+
}
|
|
915
|
+
}
|
|
916
|
+
async exchangeAuthorizationCode(providerId, code, pending) {
|
|
917
|
+
const config = this.getProvider(providerId);
|
|
918
|
+
if (!config.accessTokenEndpoint) {
|
|
919
|
+
throw new Error(`No accessTokenEndpoint configured for provider '${providerId}'.`);
|
|
920
|
+
}
|
|
921
|
+
const params = new URLSearchParams({
|
|
922
|
+
grant_type: 'authorization_code',
|
|
923
|
+
client_id: config.appId,
|
|
924
|
+
code,
|
|
925
|
+
redirect_uri: pending.redirectUri,
|
|
926
|
+
});
|
|
927
|
+
if (config.pkceEnabled) {
|
|
928
|
+
params.set('code_verifier', pending.codeVerifier);
|
|
929
|
+
}
|
|
930
|
+
if (config.logsEnabled) {
|
|
931
|
+
console.log(`[OAuth2:${providerId}] Exchanging code at:`, config.accessTokenEndpoint);
|
|
932
|
+
}
|
|
933
|
+
const response = await fetch(config.accessTokenEndpoint, {
|
|
934
|
+
method: 'POST',
|
|
935
|
+
headers: {
|
|
936
|
+
'Content-Type': 'application/x-www-form-urlencoded',
|
|
937
|
+
},
|
|
938
|
+
body: params.toString(),
|
|
939
|
+
});
|
|
940
|
+
if (!response.ok) {
|
|
941
|
+
const text = await response.text();
|
|
942
|
+
throw new Error(`OAuth2 token exchange failed (${response.status}): ${text}`);
|
|
943
|
+
}
|
|
944
|
+
return (await response.json());
|
|
945
|
+
}
|
|
946
|
+
async refreshWithRefreshToken(providerId, refreshToken) {
|
|
947
|
+
var _a, _b, _c;
|
|
948
|
+
const config = this.getProvider(providerId);
|
|
949
|
+
if (!config.accessTokenEndpoint) {
|
|
950
|
+
throw new Error(`No accessTokenEndpoint configured for provider '${providerId}'.`);
|
|
951
|
+
}
|
|
952
|
+
const params = new URLSearchParams({
|
|
953
|
+
grant_type: 'refresh_token',
|
|
954
|
+
refresh_token: refreshToken,
|
|
955
|
+
client_id: config.appId,
|
|
956
|
+
});
|
|
957
|
+
const response = await fetch(config.accessTokenEndpoint, {
|
|
958
|
+
method: 'POST',
|
|
959
|
+
headers: {
|
|
960
|
+
'Content-Type': 'application/x-www-form-urlencoded',
|
|
961
|
+
},
|
|
962
|
+
body: params.toString(),
|
|
963
|
+
});
|
|
964
|
+
if (!response.ok) {
|
|
965
|
+
const text = await response.text();
|
|
966
|
+
throw new Error(`OAuth2 refresh failed (${response.status}): ${text}`);
|
|
967
|
+
}
|
|
968
|
+
const tokens = (await response.json());
|
|
969
|
+
const expiresAt = tokens.expires_in ? Date.now() + tokens.expires_in * 1000 : Date.now() + 3600000;
|
|
970
|
+
const scopeArray = (_b = (_a = tokens.scope) === null || _a === void 0 ? void 0 : _a.split(' ').filter(Boolean)) !== null && _b !== void 0 ? _b : [];
|
|
971
|
+
this.persistTokens(providerId, {
|
|
972
|
+
accessToken: tokens.access_token,
|
|
973
|
+
refreshToken: (_c = tokens.refresh_token) !== null && _c !== void 0 ? _c : refreshToken,
|
|
974
|
+
idToken: tokens.id_token,
|
|
975
|
+
expiresAt,
|
|
976
|
+
scope: scopeArray,
|
|
977
|
+
tokenType: tokens.token_type,
|
|
978
|
+
});
|
|
979
|
+
}
|
|
980
|
+
async fetchResource(providerId, accessToken) {
|
|
981
|
+
const config = this.getProvider(providerId);
|
|
982
|
+
if (!config.resourceUrl) {
|
|
983
|
+
throw new Error(`No resourceUrl configured for provider '${providerId}'.`);
|
|
984
|
+
}
|
|
985
|
+
const headers = {
|
|
986
|
+
Authorization: `Bearer ${accessToken}`,
|
|
987
|
+
};
|
|
988
|
+
if (config.additionalResourceHeaders) {
|
|
989
|
+
Object.assign(headers, config.additionalResourceHeaders);
|
|
990
|
+
}
|
|
991
|
+
const response = await fetch(config.resourceUrl, {
|
|
992
|
+
headers,
|
|
993
|
+
});
|
|
994
|
+
if (!response.ok) {
|
|
995
|
+
const text = await response.text();
|
|
996
|
+
throw new Error(`Unable to fetch OAuth2 resource (${response.status}): ${text}`);
|
|
997
|
+
}
|
|
998
|
+
return (await response.json());
|
|
999
|
+
}
|
|
1000
|
+
persistTokens(providerId, tokens) {
|
|
1001
|
+
localStorage.setItem(this.getTokensKey(providerId), JSON.stringify(tokens));
|
|
1002
|
+
}
|
|
1003
|
+
getStoredTokens(providerId) {
|
|
1004
|
+
const raw = localStorage.getItem(this.getTokensKey(providerId));
|
|
1005
|
+
if (!raw) {
|
|
1006
|
+
return null;
|
|
1007
|
+
}
|
|
1008
|
+
try {
|
|
1009
|
+
return JSON.parse(raw);
|
|
1010
|
+
}
|
|
1011
|
+
catch (err) {
|
|
1012
|
+
console.warn(`Failed to parse stored OAuth2 tokens for provider '${providerId}'`, err);
|
|
1013
|
+
return null;
|
|
1014
|
+
}
|
|
1015
|
+
}
|
|
1016
|
+
persistPendingLogin(state, payload) {
|
|
1017
|
+
localStorage.setItem(`${this.STATE_PREFIX}${state}`, JSON.stringify(payload));
|
|
1018
|
+
}
|
|
1019
|
+
consumePendingLogin(state) {
|
|
1020
|
+
const key = `${this.STATE_PREFIX}${state}`;
|
|
1021
|
+
const raw = localStorage.getItem(key);
|
|
1022
|
+
localStorage.removeItem(key);
|
|
1023
|
+
if (!raw) {
|
|
1024
|
+
return null;
|
|
1025
|
+
}
|
|
1026
|
+
try {
|
|
1027
|
+
return JSON.parse(raw);
|
|
1028
|
+
}
|
|
1029
|
+
catch (err) {
|
|
1030
|
+
console.warn('Failed to parse pending OAuth2 login payload', err);
|
|
1031
|
+
return null;
|
|
1032
|
+
}
|
|
1033
|
+
}
|
|
1034
|
+
generateState() {
|
|
1035
|
+
return [...crypto.getRandomValues(new Uint8Array(16))].map((b) => b.toString(16).padStart(2, '0')).join('');
|
|
1036
|
+
}
|
|
1037
|
+
generateCodeVerifier() {
|
|
1038
|
+
const array = new Uint8Array(64);
|
|
1039
|
+
crypto.getRandomValues(array);
|
|
1040
|
+
return Array.from(array)
|
|
1041
|
+
.map((b) => 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-._~'[b % 66])
|
|
1042
|
+
.join('');
|
|
1043
|
+
}
|
|
1044
|
+
async generateCodeChallenge(codeVerifier) {
|
|
1045
|
+
const encoder = new TextEncoder();
|
|
1046
|
+
const data = encoder.encode(codeVerifier);
|
|
1047
|
+
const digest = await crypto.subtle.digest('SHA-256', data);
|
|
1048
|
+
return this.base64UrlEncode(new Uint8Array(digest));
|
|
1049
|
+
}
|
|
1050
|
+
base64UrlEncode(buffer) {
|
|
1051
|
+
let binary = '';
|
|
1052
|
+
buffer.forEach((b) => (binary += String.fromCharCode(b)));
|
|
1053
|
+
return btoa(binary).replace(/\+/g, '-').replace(/\//g, '_').replace(/=+$/, '');
|
|
1054
|
+
}
|
|
1055
|
+
}
|
|
1056
|
+
|
|
614
1057
|
var __rest = (undefined && undefined.__rest) || function (s, e) {
|
|
615
1058
|
var t = {};
|
|
616
1059
|
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
|
|
@@ -964,6 +1407,7 @@ var capacitorCapacitorUpdater = (function (exports, core) {
|
|
|
964
1407
|
this.appleProvider = new AppleSocialLogin();
|
|
965
1408
|
this.facebookProvider = new FacebookSocialLogin();
|
|
966
1409
|
this.twitterProvider = new TwitterSocialLogin();
|
|
1410
|
+
this.oauth2Provider = new OAuth2SocialLogin();
|
|
967
1411
|
// Set up listener for OAuth redirects if we have a pending OAuth flow
|
|
968
1412
|
if (localStorage.getItem(SocialLoginWeb.OAUTH_STATE_KEY)) {
|
|
969
1413
|
console.log('OAUTH_STATE_KEY found');
|
|
@@ -994,6 +1438,9 @@ var capacitorCapacitorUpdater = (function (exports, core) {
|
|
|
994
1438
|
case 'twitter':
|
|
995
1439
|
result = await this.twitterProvider.handleOAuthRedirect(url, state);
|
|
996
1440
|
break;
|
|
1441
|
+
case 'oauth2':
|
|
1442
|
+
result = await this.oauth2Provider.handleOAuthRedirect(url, state);
|
|
1443
|
+
break;
|
|
997
1444
|
case 'google':
|
|
998
1445
|
default:
|
|
999
1446
|
result = this.googleProvider.handleOAuthRedirect(url);
|
|
@@ -1030,6 +1477,9 @@ var capacitorCapacitorUpdater = (function (exports, core) {
|
|
|
1030
1477
|
if ((_d = options.twitter) === null || _d === void 0 ? void 0 : _d.clientId) {
|
|
1031
1478
|
initPromises.push(this.twitterProvider.initialize(options.twitter.clientId, options.twitter.redirectUrl, options.twitter.defaultScopes, options.twitter.forceLogin, options.twitter.audience));
|
|
1032
1479
|
}
|
|
1480
|
+
if (options.oauth2 && Object.keys(options.oauth2).length > 0) {
|
|
1481
|
+
initPromises.push(this.oauth2Provider.initializeProviders(options.oauth2));
|
|
1482
|
+
}
|
|
1033
1483
|
await Promise.all(initPromises);
|
|
1034
1484
|
}
|
|
1035
1485
|
async login(options) {
|
|
@@ -1042,6 +1492,8 @@ var capacitorCapacitorUpdater = (function (exports, core) {
|
|
|
1042
1492
|
return this.facebookProvider.login(options.options);
|
|
1043
1493
|
case 'twitter':
|
|
1044
1494
|
return this.twitterProvider.login(options.options);
|
|
1495
|
+
case 'oauth2':
|
|
1496
|
+
return this.oauth2Provider.login(options.options);
|
|
1045
1497
|
default:
|
|
1046
1498
|
throw new Error(`Login for ${options.provider} is not implemented on web`);
|
|
1047
1499
|
}
|
|
@@ -1056,6 +1508,11 @@ var capacitorCapacitorUpdater = (function (exports, core) {
|
|
|
1056
1508
|
return this.facebookProvider.logout();
|
|
1057
1509
|
case 'twitter':
|
|
1058
1510
|
return this.twitterProvider.logout();
|
|
1511
|
+
case 'oauth2':
|
|
1512
|
+
if (!options.providerId) {
|
|
1513
|
+
throw new Error('providerId is required for oauth2 logout');
|
|
1514
|
+
}
|
|
1515
|
+
return this.oauth2Provider.logout(options.providerId);
|
|
1059
1516
|
default:
|
|
1060
1517
|
throw new Error(`Logout for ${options.provider} is not implemented`);
|
|
1061
1518
|
}
|
|
@@ -1070,6 +1527,11 @@ var capacitorCapacitorUpdater = (function (exports, core) {
|
|
|
1070
1527
|
return this.facebookProvider.isLoggedIn();
|
|
1071
1528
|
case 'twitter':
|
|
1072
1529
|
return this.twitterProvider.isLoggedIn();
|
|
1530
|
+
case 'oauth2':
|
|
1531
|
+
if (!options.providerId) {
|
|
1532
|
+
throw new Error('providerId is required for oauth2 isLoggedIn');
|
|
1533
|
+
}
|
|
1534
|
+
return this.oauth2Provider.isLoggedIn(options.providerId);
|
|
1073
1535
|
default:
|
|
1074
1536
|
throw new Error(`isLoggedIn for ${options.provider} is not implemented`);
|
|
1075
1537
|
}
|
|
@@ -1084,6 +1546,11 @@ var capacitorCapacitorUpdater = (function (exports, core) {
|
|
|
1084
1546
|
return this.facebookProvider.getAuthorizationCode();
|
|
1085
1547
|
case 'twitter':
|
|
1086
1548
|
return this.twitterProvider.getAuthorizationCode();
|
|
1549
|
+
case 'oauth2':
|
|
1550
|
+
if (!options.providerId) {
|
|
1551
|
+
throw new Error('providerId is required for oauth2 getAuthorizationCode');
|
|
1552
|
+
}
|
|
1553
|
+
return this.oauth2Provider.getAuthorizationCode(options.providerId);
|
|
1087
1554
|
default:
|
|
1088
1555
|
throw new Error(`getAuthorizationCode for ${options.provider} is not implemented`);
|
|
1089
1556
|
}
|
|
@@ -1098,6 +1565,13 @@ var capacitorCapacitorUpdater = (function (exports, core) {
|
|
|
1098
1565
|
return this.facebookProvider.refresh(options.options);
|
|
1099
1566
|
case 'twitter':
|
|
1100
1567
|
return this.twitterProvider.refresh();
|
|
1568
|
+
case 'oauth2': {
|
|
1569
|
+
const oauth2Options = options.options;
|
|
1570
|
+
if (!(oauth2Options === null || oauth2Options === void 0 ? void 0 : oauth2Options.providerId)) {
|
|
1571
|
+
throw new Error('providerId is required for oauth2 refresh');
|
|
1572
|
+
}
|
|
1573
|
+
return this.oauth2Provider.refresh(oauth2Options.providerId);
|
|
1574
|
+
}
|
|
1101
1575
|
default:
|
|
1102
1576
|
throw new Error(`Refresh for ${options.provider} is not implemented`);
|
|
1103
1577
|
}
|