@capgo/capacitor-social-login 8.5.1 → 8.5.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.
- package/README.md +78 -18
- package/android/src/main/java/ee/forgr/capacitor/social/login/OAuth2LoginActivity.java +7 -0
- package/android/src/main/java/ee/forgr/capacitor/social/login/SocialLoginPlugin.java +31 -1
- package/android/src/main/java/ee/forgr/capacitor/social/login/TelegramProvider.java +352 -0
- package/android/src/main/java/ee/forgr/capacitor/social/login/helpers/DependencyAvailabilityChecker.java +21 -1
- package/dist/docs.json +150 -8
- package/dist/esm/definitions.d.ts +87 -3
- package/dist/esm/definitions.js.map +1 -1
- package/dist/esm/facebook-provider.d.ts +3 -1
- package/dist/esm/facebook-provider.js +33 -0
- package/dist/esm/facebook-provider.js.map +1 -1
- package/dist/esm/oauth-popup-bridge.js +17 -7
- package/dist/esm/oauth-popup-bridge.js.map +1 -1
- package/dist/esm/oauth-popup-redirect.js +5 -0
- package/dist/esm/oauth-popup-redirect.js.map +1 -1
- package/dist/esm/social-login.d.ts +1 -1
- package/dist/esm/social-login.js.map +1 -1
- package/dist/esm/telegram-provider.d.ts +37 -0
- package/dist/esm/telegram-provider.js +292 -0
- package/dist/esm/telegram-provider.js.map +1 -0
- package/dist/esm/web.d.ts +2 -1
- package/dist/esm/web.js +23 -3
- package/dist/esm/web.js.map +1 -1
- package/dist/plugin.cjs.js +364 -10
- package/dist/plugin.cjs.js.map +1 -1
- package/dist/plugin.js +364 -10
- package/dist/plugin.js.map +1 -1
- package/ios/Sources/SocialLoginPlugin/SocialLoginPlugin.swift +67 -1
- package/ios/Sources/SocialLoginPlugin/TelegramProvider.swift +260 -0
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -19,6 +19,7 @@ This plugin implements social auth for:
|
|
|
19
19
|
- Apple (with OAuth on android)
|
|
20
20
|
- Facebook (with latest SDK)
|
|
21
21
|
- Twitter/X (OAuth 2.0)
|
|
22
|
+
- Telegram (Login Widget)
|
|
22
23
|
- Generic OAuth2 (supports multiple providers: GitHub, Azure AD, Auth0, Okta, and any OAuth2-compliant server)
|
|
23
24
|
|
|
24
25
|
This plugin is the all-in-one solution for social authentication on Web, iOS, and Android.
|
|
@@ -190,6 +191,8 @@ Docs: [How to setup facebook login](https://capgo.app/docs/plugins/social-login/
|
|
|
190
191
|
|
|
191
192
|
This plugin fully supports Facebook Business Login for accessing business-related features and permissions. Business accounts can request additional permissions beyond standard consumer login, including Instagram and Pages management.
|
|
192
193
|
|
|
194
|
+
Works on **iOS**, **Android**, and **Web**. After login, use `providerSpecificCall` to fetch business fields (e.g., `instagram_business_account`, Pages) on every platform.
|
|
195
|
+
|
|
193
196
|
**Supported Business Permissions:**
|
|
194
197
|
- `instagram_basic` - Access to Instagram Basic Display API
|
|
195
198
|
- `instagram_manage_insights` - Access to Instagram Insights
|
|
@@ -556,6 +559,32 @@ Initialize method to create a script tag with Google lib. We cannot know when it
|
|
|
556
559
|
|
|
557
560
|
On Web, Google `refresh()` is not implemented, even when using `mode: 'online'`. Call `SocialLogin.login({ provider: 'google', ... })` again to obtain a fresh token.
|
|
558
561
|
|
|
562
|
+
## Telegram
|
|
563
|
+
|
|
564
|
+
Telegram uses the [Login Widget](https://core.telegram.org/widgets/login) (`oauth.telegram.org`), not standard OAuth2. Create a bot with [@BotFather](https://t.me/BotFather), set the domain for the widget, and pass the **bot id** (not the bot token):
|
|
565
|
+
|
|
566
|
+
```typescript
|
|
567
|
+
await SocialLogin.initialize({
|
|
568
|
+
telegram: {
|
|
569
|
+
botId: '123456789',
|
|
570
|
+
redirectUrl: 'https://your-app.example/auth/telegram', // or myapp://telegram-auth on native
|
|
571
|
+
// Required on native when redirectUrl is a custom scheme
|
|
572
|
+
origin: 'https://your-app.example',
|
|
573
|
+
requestAccess: 'write',
|
|
574
|
+
},
|
|
575
|
+
});
|
|
576
|
+
|
|
577
|
+
const res = await SocialLogin.login({
|
|
578
|
+
provider: 'telegram',
|
|
579
|
+
options: {},
|
|
580
|
+
});
|
|
581
|
+
|
|
582
|
+
// Verify `res.result.hash` on your backend with the bot token.
|
|
583
|
+
// Reject stale or future `res.result.authDate` values before creating a session.
|
|
584
|
+
// Never ship the bot token in the app.
|
|
585
|
+
console.log(res.result.profile.id, res.result.hash);
|
|
586
|
+
```
|
|
587
|
+
|
|
559
588
|
## OAuth2 (Generic)
|
|
560
589
|
|
|
561
590
|
The plugin supports generic OAuth2 authentication, allowing you to integrate with any OAuth2-compliant provider (GitHub, Azure AD, Auth0, Okta, Keycloak, custom servers, etc.). You can configure multiple OAuth2 providers simultaneously.
|
|
@@ -876,14 +905,14 @@ Initialize the plugin
|
|
|
876
905
|
### login(...)
|
|
877
906
|
|
|
878
907
|
```typescript
|
|
879
|
-
login<T extends "apple" | "google" | "facebook" | "twitter" | "oauth2">(options: Extract<LoginOptions, { provider: T; }>) => Promise<{ provider: T; result: ProviderResponseMap[T]; }>
|
|
908
|
+
login<T extends "apple" | "google" | "facebook" | "twitter" | "oauth2" | "telegram">(options: Extract<LoginOptions, { provider: T; }>) => Promise<{ provider: T; result: ProviderResponseMap[T]; }>
|
|
880
909
|
```
|
|
881
910
|
|
|
882
911
|
Login with the selected provider
|
|
883
912
|
|
|
884
|
-
| Param | Type
|
|
885
|
-
| ------------- |
|
|
886
|
-
| **`options`** | <code><a href="#extract">Extract</a><{ provider: 'facebook'; options: <a href="#facebookloginoptions">FacebookLoginOptions</a>; }, { provider: T; }> \| <a href="#extract">Extract</a><{ provider: 'google'; options: <a href="#googleloginoptions">GoogleLoginOptions</a>; }, { provider: T; }> \| <a href="#extract">Extract</a><{ provider: 'apple'; options: <a href="#appleprovideroptions">AppleProviderOptions</a>; }, { provider: T; }> \| <a href="#extract">Extract</a><{ provider: 'twitter'; options: <a href="#twitterloginoptions">TwitterLoginOptions</a>; }, { provider: T; }> \| <a href="#extract">Extract</a><{ provider: 'oauth2'; options: <a href="#oauth2loginoptions">OAuth2LoginOptions</a>; }, { provider: T; }></code> |
|
|
913
|
+
| Param | Type |
|
|
914
|
+
| ------------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
|
|
915
|
+
| **`options`** | <code><a href="#extract">Extract</a><{ provider: 'facebook'; options: <a href="#facebookloginoptions">FacebookLoginOptions</a>; }, { provider: T; }> \| <a href="#extract">Extract</a><{ provider: 'google'; options: <a href="#googleloginoptions">GoogleLoginOptions</a>; }, { provider: T; }> \| <a href="#extract">Extract</a><{ provider: 'apple'; options: <a href="#appleprovideroptions">AppleProviderOptions</a>; }, { provider: T; }> \| <a href="#extract">Extract</a><{ provider: 'twitter'; options: <a href="#twitterloginoptions">TwitterLoginOptions</a>; }, { provider: T; }> \| <a href="#extract">Extract</a><{ provider: 'telegram'; options: <a href="#telegramloginoptions">TelegramLoginOptions</a>; }, { provider: T; }> \| <a href="#extract">Extract</a><{ provider: 'oauth2'; options: <a href="#oauth2loginoptions">OAuth2LoginOptions</a>; }, { provider: T; }></code> |
|
|
887
916
|
|
|
888
917
|
**Returns:** <code>Promise<{ provider: T; result: ProviderResponseMap[T]; }></code>
|
|
889
918
|
|
|
@@ -893,14 +922,14 @@ Login with the selected provider
|
|
|
893
922
|
### logout(...)
|
|
894
923
|
|
|
895
924
|
```typescript
|
|
896
|
-
logout(options: { provider: 'apple' | 'google' | 'facebook' | 'twitter' | 'oauth2'; providerId?: string; }) => Promise<void>
|
|
925
|
+
logout(options: { provider: 'apple' | 'google' | 'facebook' | 'twitter' | 'telegram' | 'oauth2'; providerId?: string; }) => Promise<void>
|
|
897
926
|
```
|
|
898
927
|
|
|
899
928
|
Logout
|
|
900
929
|
|
|
901
|
-
| Param | Type
|
|
902
|
-
| ------------- |
|
|
903
|
-
| **`options`** | <code>{ provider: 'apple' \| 'google' \| 'facebook' \| 'twitter' \| 'oauth2'; providerId?: string; }</code> |
|
|
930
|
+
| Param | Type |
|
|
931
|
+
| ------------- | ------------------------------------------------------------------------------------------------------------------------- |
|
|
932
|
+
| **`options`** | <code>{ provider: 'apple' \| 'google' \| 'facebook' \| 'twitter' \| 'oauth2' \| 'telegram'; providerId?: string; }</code> |
|
|
904
933
|
|
|
905
934
|
--------------------
|
|
906
935
|
|
|
@@ -1199,6 +1228,7 @@ And in the AndroidManifest.xml file:
|
|
|
1199
1228
|
| Prop | Type | Description |
|
|
1200
1229
|
| -------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------ |
|
|
1201
1230
|
| **`oauth2`** | <code><a href="#record">Record</a><string, <a href="#oauth2providerconfig">OAuth2ProviderConfig</a>></code> | OAuth2 provider configurations. Supports multiple providers by using a <a href="#record">Record</a> with provider IDs as keys. |
|
|
1231
|
+
| **`telegram`** | <code>{ botId: string; redirectUrl?: string; origin?: string; requestAccess?: 'read' \| 'write'; languageCode?: string; }</code> | Telegram Login Widget configuration. Uses Telegram's OAuth widget (`oauth.telegram.org`), not standard OAuth2. |
|
|
1202
1232
|
| **`twitter`** | <code>{ clientId: string; redirectUrl: string; defaultScopes?: string[]; forceLogin?: boolean; audience?: string; }</code> | |
|
|
1203
1233
|
| **`facebook`** | <code>{ appId: string; clientToken?: string; locale?: string; }</code> | |
|
|
1204
1234
|
| **`google`** | <code>{ iOSClientId?: string; iOSServerClientId?: string; webClientId?: string; mode?: 'online' \| 'offline'; hostedDomain?: string; redirectUrl?: string; }</code> | |
|
|
@@ -1318,6 +1348,27 @@ Configuration for a single OAuth2 provider instance
|
|
|
1318
1348
|
| **`email`** | <code>string \| null</code> |
|
|
1319
1349
|
|
|
1320
1350
|
|
|
1351
|
+
#### TelegramLoginResponse
|
|
1352
|
+
|
|
1353
|
+
| Prop | Type | Description |
|
|
1354
|
+
| ------------------- | ----------------------------------------------------------- | ------------------------------------------------------------ |
|
|
1355
|
+
| **`profile`** | <code><a href="#telegramprofile">TelegramProfile</a></code> | |
|
|
1356
|
+
| **`authDate`** | <code>number</code> | Unix timestamp (seconds) when the user authorized the login. |
|
|
1357
|
+
| **`hash`** | <code>string</code> | Telegram-provided hash for server-side verification. |
|
|
1358
|
+
| **`requestAccess`** | <code>'read' \| 'write'</code> | Requested access level that was used for this login. |
|
|
1359
|
+
|
|
1360
|
+
|
|
1361
|
+
#### TelegramProfile
|
|
1362
|
+
|
|
1363
|
+
| Prop | Type |
|
|
1364
|
+
| --------------- | --------------------------- |
|
|
1365
|
+
| **`id`** | <code>string</code> |
|
|
1366
|
+
| **`firstName`** | <code>string</code> |
|
|
1367
|
+
| **`lastName`** | <code>string \| null</code> |
|
|
1368
|
+
| **`username`** | <code>string \| null</code> |
|
|
1369
|
+
| **`photoUrl`** | <code>string \| null</code> |
|
|
1370
|
+
|
|
1371
|
+
|
|
1321
1372
|
#### OAuth2LoginResponse
|
|
1322
1373
|
|
|
1323
1374
|
| Prop | Type | Description |
|
|
@@ -1376,6 +1427,15 @@ Configuration for a single OAuth2 provider instance
|
|
|
1376
1427
|
| **`forceLogin`** | <code>boolean</code> | Force the consent screen on every attempt, maps to `force_login=true`. |
|
|
1377
1428
|
|
|
1378
1429
|
|
|
1430
|
+
#### TelegramLoginOptions
|
|
1431
|
+
|
|
1432
|
+
| Prop | Type | Description | Default |
|
|
1433
|
+
| ------------------- | ------------------------------ | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | -------------------- |
|
|
1434
|
+
| **`redirectUrl`** | <code>string</code> | Override the redirect URL for this login attempt. Defaults to the redirect configured during initialize(). Required on iOS/Android when initialize() did not set `redirectUrl`. | |
|
|
1435
|
+
| **`state`** | <code>string</code> | Optional state parameter for CSRF protection. If omitted, a secure random value is generated automatically. | |
|
|
1436
|
+
| **`requestAccess`** | <code>'read' \| 'write'</code> | Override requested access level for this login. Defaults to the value configured during initialize(). | <code>'write'</code> |
|
|
1437
|
+
|
|
1438
|
+
|
|
1379
1439
|
#### OAuth2LoginOptions
|
|
1380
1440
|
|
|
1381
1441
|
| Prop | Type | Description | Default |
|
|
@@ -1394,10 +1454,10 @@ Configuration for a single OAuth2 provider instance
|
|
|
1394
1454
|
|
|
1395
1455
|
#### isLoggedInOptions
|
|
1396
1456
|
|
|
1397
|
-
| Prop | Type
|
|
1398
|
-
| ---------------- |
|
|
1399
|
-
| **`provider`** | <code>'apple' \| 'google' \| 'facebook' \| 'twitter' \| 'oauth2'</code> | Provider |
|
|
1400
|
-
| **`providerId`** | <code>string</code>
|
|
1457
|
+
| Prop | Type | Description |
|
|
1458
|
+
| ---------------- | ------------------------------------------------------------------------------------- | --------------------------------------------------------------------- |
|
|
1459
|
+
| **`provider`** | <code>'apple' \| 'google' \| 'facebook' \| 'twitter' \| 'oauth2' \| 'telegram'</code> | Provider |
|
|
1460
|
+
| **`providerId`** | <code>string</code> | Provider ID for OAuth2 providers (required when provider is 'oauth2') |
|
|
1401
1461
|
|
|
1402
1462
|
|
|
1403
1463
|
#### AuthorizationCode
|
|
@@ -1463,9 +1523,9 @@ Response from `google#clearRestoreCredential`.
|
|
|
1463
1523
|
|
|
1464
1524
|
#### FacebookGetProfileOptions
|
|
1465
1525
|
|
|
1466
|
-
| Prop | Type | Description
|
|
1467
|
-
| ------------ | --------------------- |
|
|
1468
|
-
| **`fields`** | <code>string[]</code> | Fields to retrieve from Facebook profile |
|
|
1526
|
+
| Prop | Type | Description | Default |
|
|
1527
|
+
| ------------ | --------------------- | ----------------------------------------- | ----------------------------------------------- |
|
|
1528
|
+
| **`fields`** | <code>string[]</code> | Fields to retrieve from Facebook profile. | <code>['id', 'name', 'email', 'picture']</code> |
|
|
1469
1529
|
|
|
1470
1530
|
|
|
1471
1531
|
#### GoogleCreateRestoreCredentialOptions
|
|
@@ -1522,7 +1582,7 @@ Construct a type with a set of properties K of type T
|
|
|
1522
1582
|
|
|
1523
1583
|
#### ProviderResponseMap
|
|
1524
1584
|
|
|
1525
|
-
<code>{ facebook: <a href="#facebookloginresponse">FacebookLoginResponse</a>; google: <a href="#googleloginresponse">GoogleLoginResponse</a>; apple: <a href="#appleproviderresponse">AppleProviderResponse</a>; twitter: <a href="#twitterloginresponse">TwitterLoginResponse</a>; oauth2: <a href="#oauth2loginresponse">OAuth2LoginResponse</a>; }</code>
|
|
1585
|
+
<code>{ facebook: <a href="#facebookloginresponse">FacebookLoginResponse</a>; google: <a href="#googleloginresponse">GoogleLoginResponse</a>; apple: <a href="#appleproviderresponse">AppleProviderResponse</a>; twitter: <a href="#twitterloginresponse">TwitterLoginResponse</a>; telegram: <a href="#telegramloginresponse">TelegramLoginResponse</a>; oauth2: <a href="#oauth2loginresponse">OAuth2LoginResponse</a>; }</code>
|
|
1526
1586
|
|
|
1527
1587
|
|
|
1528
1588
|
#### GoogleLoginResponse
|
|
@@ -1532,7 +1592,7 @@ Construct a type with a set of properties K of type T
|
|
|
1532
1592
|
|
|
1533
1593
|
#### LoginOptions
|
|
1534
1594
|
|
|
1535
|
-
<code>{ provider: 'facebook'; options: <a href="#facebookloginoptions">FacebookLoginOptions</a>; } | { provider: 'google'; options: <a href="#googleloginoptions">GoogleLoginOptions</a>; } | { provider: 'apple'; options: <a href="#appleprovideroptions">AppleProviderOptions</a>; } | { provider: 'twitter'; options: <a href="#twitterloginoptions">TwitterLoginOptions</a>; } | { provider: 'oauth2'; options: <a href="#oauth2loginoptions">OAuth2LoginOptions</a>; }</code>
|
|
1595
|
+
<code>{ provider: 'facebook'; options: <a href="#facebookloginoptions">FacebookLoginOptions</a>; } | { provider: 'google'; options: <a href="#googleloginoptions">GoogleLoginOptions</a>; } | { provider: 'apple'; options: <a href="#appleprovideroptions">AppleProviderOptions</a>; } | { provider: 'twitter'; options: <a href="#twitterloginoptions">TwitterLoginOptions</a>; } | { provider: 'telegram'; options: <a href="#telegramloginoptions">TelegramLoginOptions</a>; } | { provider: 'oauth2'; options: <a href="#oauth2loginoptions">OAuth2LoginOptions</a>; }</code>
|
|
1536
1596
|
|
|
1537
1597
|
|
|
1538
1598
|
#### Extract
|
|
@@ -1544,7 +1604,7 @@ Construct a type with a set of properties K of type T
|
|
|
1544
1604
|
|
|
1545
1605
|
#### LoginResult
|
|
1546
1606
|
|
|
1547
|
-
<code>{ provider: 'facebook'; result: <a href="#facebookloginresponse">FacebookLoginResponse</a>; } | { provider: 'google'; result: <a href="#googleloginresponse">GoogleLoginResponse</a>; } | { provider: 'apple'; result: <a href="#appleproviderresponse">AppleProviderResponse</a>; } | { provider: 'twitter'; result: <a href="#twitterloginresponse">TwitterLoginResponse</a>; } | { provider: 'oauth2'; result: <a href="#oauth2loginresponse">OAuth2LoginResponse</a>; }</code>
|
|
1607
|
+
<code>{ provider: 'facebook'; result: <a href="#facebookloginresponse">FacebookLoginResponse</a>; } | { provider: 'google'; result: <a href="#googleloginresponse">GoogleLoginResponse</a>; } | { provider: 'apple'; result: <a href="#appleproviderresponse">AppleProviderResponse</a>; } | { provider: 'twitter'; result: <a href="#twitterloginresponse">TwitterLoginResponse</a>; } | { provider: 'telegram'; result: <a href="#telegramloginresponse">TelegramLoginResponse</a>; } | { provider: 'oauth2'; result: <a href="#oauth2loginresponse">OAuth2LoginResponse</a>; }</code>
|
|
1548
1608
|
|
|
1549
1609
|
|
|
1550
1610
|
#### ProviderSpecificCallResponseMap
|
|
@@ -76,6 +76,13 @@ public class OAuth2LoginActivity extends Activity {
|
|
|
76
76
|
data.putExtra("error", uri.getQueryParameter("error"));
|
|
77
77
|
data.putExtra("error_description", uri.getQueryParameter("error_description"));
|
|
78
78
|
|
|
79
|
+
for (String name : uri.getQueryParameterNames()) {
|
|
80
|
+
if (data.hasExtra(name)) {
|
|
81
|
+
continue;
|
|
82
|
+
}
|
|
83
|
+
data.putExtra(name, uri.getQueryParameter(name));
|
|
84
|
+
}
|
|
85
|
+
|
|
79
86
|
// Handle implicit flow (fragment parameters)
|
|
80
87
|
String fragment = uri.getFragment();
|
|
81
88
|
if (fragment != null && !fragment.isEmpty()) {
|
|
@@ -24,7 +24,7 @@ import org.json.JSONObject;
|
|
|
24
24
|
@CapacitorPlugin(name = "SocialLogin")
|
|
25
25
|
public class SocialLoginPlugin extends Plugin {
|
|
26
26
|
|
|
27
|
-
private final String pluginVersion = "8.5.
|
|
27
|
+
private final String pluginVersion = "8.5.3";
|
|
28
28
|
|
|
29
29
|
public static String LOG_TAG = "CapgoSocialLogin";
|
|
30
30
|
public HashMap<String, SocialProvider> socialProviderHashMap = new HashMap<>();
|
|
@@ -164,6 +164,27 @@ public class SocialLoginPlugin extends Plugin {
|
|
|
164
164
|
}
|
|
165
165
|
}
|
|
166
166
|
|
|
167
|
+
JSObject telegram = call.getObject("telegram");
|
|
168
|
+
if (telegram != null) {
|
|
169
|
+
if (!DependencyAvailabilityChecker.isProviderAvailable("telegram")) {
|
|
170
|
+
call.reject("Telegram provider is disabled in capacitor.config");
|
|
171
|
+
return;
|
|
172
|
+
}
|
|
173
|
+
String botId = telegram.getString("botId");
|
|
174
|
+
if (botId == null || botId.isEmpty()) {
|
|
175
|
+
call.reject("telegram.botId is null or empty");
|
|
176
|
+
return;
|
|
177
|
+
}
|
|
178
|
+
TelegramProvider telegramProvider = new TelegramProvider(this.getActivity(), this.getContext());
|
|
179
|
+
try {
|
|
180
|
+
telegramProvider.initialize(telegram);
|
|
181
|
+
this.socialProviderHashMap.put("telegram", telegramProvider);
|
|
182
|
+
} catch (JSONException e) {
|
|
183
|
+
call.reject("Failed to initialize Telegram provider: " + e.getMessage());
|
|
184
|
+
return;
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
|
|
167
188
|
JSObject oauth2 = call.getObject("oauth2");
|
|
168
189
|
if (oauth2 != null && oauth2.length() > 0) {
|
|
169
190
|
// oauth2 is now a map of providerId -> config: { "github": {...}, "azure": {...} }
|
|
@@ -483,6 +504,15 @@ public class SocialLoginPlugin extends Plugin {
|
|
|
483
504
|
}
|
|
484
505
|
}
|
|
485
506
|
|
|
507
|
+
SocialProvider telegramProvider = socialProviderHashMap.get("telegram");
|
|
508
|
+
if (telegramProvider instanceof TelegramProvider) {
|
|
509
|
+
boolean handled = ((TelegramProvider) telegramProvider).handleActivityResult(requestCode, resultCode, data);
|
|
510
|
+
if (handled) {
|
|
511
|
+
Log.d(LOG_TAG, "Telegram activity result handled");
|
|
512
|
+
return;
|
|
513
|
+
}
|
|
514
|
+
}
|
|
515
|
+
|
|
486
516
|
SocialProvider oauth2Provider = socialProviderHashMap.get("oauth2");
|
|
487
517
|
if (oauth2Provider instanceof OAuth2Provider) {
|
|
488
518
|
boolean handled = ((OAuth2Provider) oauth2Provider).handleActivityResult(requestCode, resultCode, data);
|
|
@@ -0,0 +1,352 @@
|
|
|
1
|
+
package ee.forgr.capacitor.social.login;
|
|
2
|
+
|
|
3
|
+
import android.app.Activity;
|
|
4
|
+
import android.content.Context;
|
|
5
|
+
import android.content.Intent;
|
|
6
|
+
import android.content.SharedPreferences;
|
|
7
|
+
import android.net.Uri;
|
|
8
|
+
import android.text.TextUtils;
|
|
9
|
+
import com.getcapacitor.JSObject;
|
|
10
|
+
import com.getcapacitor.PluginCall;
|
|
11
|
+
import ee.forgr.capacitor.social.login.helpers.SocialProvider;
|
|
12
|
+
import java.util.UUID;
|
|
13
|
+
import org.json.JSONException;
|
|
14
|
+
import org.json.JSONObject;
|
|
15
|
+
|
|
16
|
+
public class TelegramProvider implements SocialProvider {
|
|
17
|
+
|
|
18
|
+
public static final int REQUEST_CODE = 9403;
|
|
19
|
+
private static final String USER_CANCELLED_CODE = "USER_CANCELLED";
|
|
20
|
+
private static final String PREFS_NAME = "CapgoTelegramProviderPrefs";
|
|
21
|
+
private static final String PREFS_KEY = "TelegramSession";
|
|
22
|
+
private static final long SESSION_TTL_MS = 24 * 60 * 60 * 1000L;
|
|
23
|
+
|
|
24
|
+
private final Activity activity;
|
|
25
|
+
private final Context context;
|
|
26
|
+
|
|
27
|
+
private String botId;
|
|
28
|
+
private String redirectUri;
|
|
29
|
+
private String origin;
|
|
30
|
+
private String requestAccess = "write";
|
|
31
|
+
private String languageCode;
|
|
32
|
+
|
|
33
|
+
private PluginCall pendingCall;
|
|
34
|
+
private String pendingState;
|
|
35
|
+
private String pendingRequestAccess;
|
|
36
|
+
|
|
37
|
+
public TelegramProvider(Activity activity, Context context) {
|
|
38
|
+
this.activity = activity;
|
|
39
|
+
this.context = context;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
public void initialize(JSONObject config) throws JSONException {
|
|
43
|
+
this.botId = config.getString("botId");
|
|
44
|
+
this.redirectUri = config.optString("redirectUrl", null);
|
|
45
|
+
this.origin = config.optString("origin", null);
|
|
46
|
+
this.requestAccess = config.optString("requestAccess", "write");
|
|
47
|
+
this.languageCode = config.optString("languageCode", null);
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
@Override
|
|
51
|
+
public void login(PluginCall call, JSONObject config) {
|
|
52
|
+
if (botId == null) {
|
|
53
|
+
call.reject("Telegram provider is not initialized. Call initialize() first.");
|
|
54
|
+
return;
|
|
55
|
+
}
|
|
56
|
+
if (pendingCall != null) {
|
|
57
|
+
call.reject("Another Telegram login is already running.");
|
|
58
|
+
return;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
String redirect = redirectUri;
|
|
62
|
+
if (config != null && config.has("redirectUrl")) {
|
|
63
|
+
redirect = config.optString("redirectUrl", redirectUri);
|
|
64
|
+
}
|
|
65
|
+
if (redirect == null || redirect.isEmpty()) {
|
|
66
|
+
call.reject("telegram.redirectUrl is required");
|
|
67
|
+
return;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
String resolvedRequestAccess = requestAccess;
|
|
71
|
+
if (config != null && config.has("requestAccess")) {
|
|
72
|
+
String override = config.optString("requestAccess", requestAccess);
|
|
73
|
+
if (!override.isEmpty()) {
|
|
74
|
+
resolvedRequestAccess = override;
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
String state = config != null && config.has("state")
|
|
79
|
+
? config.optString("state", UUID.randomUUID().toString())
|
|
80
|
+
: UUID.randomUUID().toString();
|
|
81
|
+
String resolvedOrigin = origin != null && !origin.isEmpty() ? origin : deriveOrigin(redirect);
|
|
82
|
+
if (!isHttpOrigin(resolvedOrigin)) {
|
|
83
|
+
call.reject("telegram.origin must be an http(s) domain registered with the bot when redirectUrl is not http(s).");
|
|
84
|
+
return;
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
pendingState = state;
|
|
88
|
+
pendingRequestAccess = resolvedRequestAccess;
|
|
89
|
+
pendingCall = call;
|
|
90
|
+
|
|
91
|
+
String returnTo = appendStateToRedirect(redirect, state);
|
|
92
|
+
|
|
93
|
+
Uri.Builder builder = Uri.parse("https://oauth.telegram.org/auth")
|
|
94
|
+
.buildUpon()
|
|
95
|
+
.appendQueryParameter("bot_id", botId)
|
|
96
|
+
.appendQueryParameter("origin", resolvedOrigin)
|
|
97
|
+
.appendQueryParameter("request_access", resolvedRequestAccess)
|
|
98
|
+
.appendQueryParameter("return_to", returnTo)
|
|
99
|
+
.appendQueryParameter("embed", "1")
|
|
100
|
+
.appendQueryParameter("mobile", "1");
|
|
101
|
+
|
|
102
|
+
if (languageCode != null && !languageCode.isEmpty()) {
|
|
103
|
+
builder.appendQueryParameter("lang", languageCode);
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
Intent intent = new Intent(activity, OAuth2LoginActivity.class);
|
|
107
|
+
intent.putExtra(OAuth2LoginActivity.EXTRA_AUTH_URL, builder.build().toString());
|
|
108
|
+
intent.putExtra(OAuth2LoginActivity.EXTRA_REDIRECT_URL, redirect);
|
|
109
|
+
activity.startActivityForResult(intent, REQUEST_CODE);
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
@Override
|
|
113
|
+
public void logout(PluginCall call) {
|
|
114
|
+
context.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE).edit().remove(PREFS_KEY).apply();
|
|
115
|
+
call.resolve();
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
@Override
|
|
119
|
+
public void getAuthorizationCode(PluginCall call) {
|
|
120
|
+
call.reject("getAuthorizationCode is not available for Telegram.");
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
@Override
|
|
124
|
+
public void isLoggedIn(PluginCall call) {
|
|
125
|
+
TelegramSession session = loadSession();
|
|
126
|
+
boolean isValid = session != null && session.expiresAtMs > System.currentTimeMillis();
|
|
127
|
+
call.resolve(new JSObject().put("isLoggedIn", isValid));
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
@Override
|
|
131
|
+
public void refresh(PluginCall call) {
|
|
132
|
+
call.reject("Telegram refresh is not supported. Call login() again.");
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
public boolean handleActivityResult(int requestCode, int resultCode, Intent data) {
|
|
136
|
+
if (requestCode != REQUEST_CODE) {
|
|
137
|
+
return false;
|
|
138
|
+
}
|
|
139
|
+
if (pendingCall == null) {
|
|
140
|
+
return true;
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
if (resultCode != Activity.RESULT_OK) {
|
|
144
|
+
boolean userCancelled = data != null && data.getBooleanExtra(OAuth2LoginActivity.EXTRA_USER_CANCELLED, false);
|
|
145
|
+
String error = data != null ? data.getStringExtra("error") : "User cancelled";
|
|
146
|
+
String message = error != null ? error : "User cancelled";
|
|
147
|
+
if (userCancelled) {
|
|
148
|
+
pendingCall.reject(message, USER_CANCELLED_CODE);
|
|
149
|
+
} else {
|
|
150
|
+
pendingCall.reject(message);
|
|
151
|
+
}
|
|
152
|
+
cleanupPending();
|
|
153
|
+
return true;
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
String returnedState = data != null ? data.getStringExtra("state") : null;
|
|
157
|
+
if (returnedState == null || !returnedState.equals(pendingState)) {
|
|
158
|
+
pendingCall.reject("State mismatch during Telegram login");
|
|
159
|
+
cleanupPending();
|
|
160
|
+
return true;
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
if (data != null) {
|
|
164
|
+
String error = data.getStringExtra("error");
|
|
165
|
+
if (error != null) {
|
|
166
|
+
String description = data.getStringExtra("error_description");
|
|
167
|
+
pendingCall.reject(description != null ? description : error);
|
|
168
|
+
cleanupPending();
|
|
169
|
+
return true;
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
String id = data != null ? data.getStringExtra("id") : null;
|
|
174
|
+
String authDateRaw = data != null ? data.getStringExtra("auth_date") : null;
|
|
175
|
+
String hash = data != null ? data.getStringExtra("hash") : null;
|
|
176
|
+
|
|
177
|
+
if (TextUtils.isEmpty(id) || TextUtils.isEmpty(authDateRaw) || TextUtils.isEmpty(hash)) {
|
|
178
|
+
pendingCall.reject("Telegram login payload is incomplete.");
|
|
179
|
+
cleanupPending();
|
|
180
|
+
return true;
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
long authDateSeconds;
|
|
184
|
+
try {
|
|
185
|
+
authDateSeconds = Long.parseLong(authDateRaw);
|
|
186
|
+
} catch (NumberFormatException e) {
|
|
187
|
+
pendingCall.reject("Telegram auth_date is invalid.");
|
|
188
|
+
cleanupPending();
|
|
189
|
+
return true;
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
String firstName = data.getStringExtra("first_name");
|
|
193
|
+
String lastName = data.getStringExtra("last_name");
|
|
194
|
+
String username = data.getStringExtra("username");
|
|
195
|
+
String photoUrl = data.getStringExtra("photo_url");
|
|
196
|
+
|
|
197
|
+
persistSession(
|
|
198
|
+
new TelegramSession(
|
|
199
|
+
id,
|
|
200
|
+
firstName,
|
|
201
|
+
lastName,
|
|
202
|
+
username,
|
|
203
|
+
photoUrl,
|
|
204
|
+
pendingRequestAccess != null ? pendingRequestAccess : requestAccess,
|
|
205
|
+
authDateSeconds,
|
|
206
|
+
authDateSeconds * 1000L + SESSION_TTL_MS
|
|
207
|
+
)
|
|
208
|
+
);
|
|
209
|
+
|
|
210
|
+
JSObject profile = new JSObject();
|
|
211
|
+
profile.put("id", id);
|
|
212
|
+
profile.put("firstName", firstName != null ? firstName : "");
|
|
213
|
+
profile.put("lastName", lastName != null ? lastName : JSONObject.NULL);
|
|
214
|
+
profile.put("username", username != null ? username : JSONObject.NULL);
|
|
215
|
+
profile.put("photoUrl", photoUrl != null ? photoUrl : JSONObject.NULL);
|
|
216
|
+
|
|
217
|
+
JSObject result = new JSObject();
|
|
218
|
+
result.put("profile", profile);
|
|
219
|
+
result.put("authDate", authDateSeconds);
|
|
220
|
+
result.put("hash", hash);
|
|
221
|
+
result.put("requestAccess", pendingRequestAccess != null ? pendingRequestAccess : requestAccess);
|
|
222
|
+
|
|
223
|
+
JSObject response = new JSObject();
|
|
224
|
+
response.put("provider", "telegram");
|
|
225
|
+
response.put("result", result);
|
|
226
|
+
pendingCall.resolve(response);
|
|
227
|
+
cleanupPending();
|
|
228
|
+
return true;
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
private void persistSession(TelegramSession session) {
|
|
232
|
+
try {
|
|
233
|
+
JSONObject stored = new JSONObject();
|
|
234
|
+
stored.put("id", session.id);
|
|
235
|
+
stored.put("firstName", session.firstName);
|
|
236
|
+
stored.put("lastName", session.lastName);
|
|
237
|
+
stored.put("username", session.username);
|
|
238
|
+
stored.put("photoUrl", session.photoUrl);
|
|
239
|
+
stored.put("requestAccess", session.requestAccess);
|
|
240
|
+
stored.put("authDate", session.authDateSeconds);
|
|
241
|
+
stored.put("expiresAt", session.expiresAtMs);
|
|
242
|
+
SharedPreferences prefs = context.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE);
|
|
243
|
+
prefs.edit().putString(PREFS_KEY, stored.toString()).apply();
|
|
244
|
+
} catch (JSONException e) {
|
|
245
|
+
// Ignore persistence errors
|
|
246
|
+
}
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
private TelegramSession loadSession() {
|
|
250
|
+
SharedPreferences prefs = context.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE);
|
|
251
|
+
String raw = prefs.getString(PREFS_KEY, null);
|
|
252
|
+
if (raw == null || raw.isEmpty()) {
|
|
253
|
+
return null;
|
|
254
|
+
}
|
|
255
|
+
try {
|
|
256
|
+
JSONObject obj = new JSONObject(raw);
|
|
257
|
+
return new TelegramSession(
|
|
258
|
+
obj.optString("id", null),
|
|
259
|
+
obj.optString("firstName", null),
|
|
260
|
+
obj.optString("lastName", null),
|
|
261
|
+
obj.optString("username", null),
|
|
262
|
+
obj.optString("photoUrl", null),
|
|
263
|
+
obj.optString("requestAccess", "write"),
|
|
264
|
+
obj.optLong("authDate", 0L),
|
|
265
|
+
obj.optLong("expiresAt", 0L)
|
|
266
|
+
);
|
|
267
|
+
} catch (JSONException e) {
|
|
268
|
+
return null;
|
|
269
|
+
}
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
private void cleanupPending() {
|
|
273
|
+
pendingCall = null;
|
|
274
|
+
pendingState = null;
|
|
275
|
+
pendingRequestAccess = null;
|
|
276
|
+
}
|
|
277
|
+
|
|
278
|
+
private static String appendStateToRedirect(String redirect, String state) {
|
|
279
|
+
try {
|
|
280
|
+
Uri uri = Uri.parse(redirect);
|
|
281
|
+
Uri.Builder builder = uri.buildUpon().clearQuery();
|
|
282
|
+
for (String name : uri.getQueryParameterNames()) {
|
|
283
|
+
if ("state".equals(name)) {
|
|
284
|
+
continue;
|
|
285
|
+
}
|
|
286
|
+
for (String value : uri.getQueryParameters(name)) {
|
|
287
|
+
builder.appendQueryParameter(name, value);
|
|
288
|
+
}
|
|
289
|
+
}
|
|
290
|
+
builder.appendQueryParameter("state", state);
|
|
291
|
+
return builder.build().toString();
|
|
292
|
+
} catch (Exception e) {
|
|
293
|
+
return redirect;
|
|
294
|
+
}
|
|
295
|
+
}
|
|
296
|
+
|
|
297
|
+
private static String deriveOrigin(String redirect) {
|
|
298
|
+
try {
|
|
299
|
+
Uri uri = Uri.parse(redirect);
|
|
300
|
+
String scheme = uri.getScheme();
|
|
301
|
+
String host = uri.getHost();
|
|
302
|
+
if (scheme != null && host != null) {
|
|
303
|
+
int port = uri.getPort();
|
|
304
|
+
if (port != -1) {
|
|
305
|
+
return scheme + "://" + host + ":" + port;
|
|
306
|
+
}
|
|
307
|
+
return scheme + "://" + host;
|
|
308
|
+
}
|
|
309
|
+
} catch (Exception ignored) {}
|
|
310
|
+
return redirect;
|
|
311
|
+
}
|
|
312
|
+
|
|
313
|
+
private static boolean isHttpOrigin(String value) {
|
|
314
|
+
if (value == null || value.isEmpty()) {
|
|
315
|
+
return false;
|
|
316
|
+
}
|
|
317
|
+
String scheme = Uri.parse(value).getScheme();
|
|
318
|
+
return "https".equalsIgnoreCase(scheme) || "http".equalsIgnoreCase(scheme);
|
|
319
|
+
}
|
|
320
|
+
|
|
321
|
+
private static class TelegramSession {
|
|
322
|
+
|
|
323
|
+
final String id;
|
|
324
|
+
final String firstName;
|
|
325
|
+
final String lastName;
|
|
326
|
+
final String username;
|
|
327
|
+
final String photoUrl;
|
|
328
|
+
final String requestAccess;
|
|
329
|
+
final long authDateSeconds;
|
|
330
|
+
final long expiresAtMs;
|
|
331
|
+
|
|
332
|
+
TelegramSession(
|
|
333
|
+
String id,
|
|
334
|
+
String firstName,
|
|
335
|
+
String lastName,
|
|
336
|
+
String username,
|
|
337
|
+
String photoUrl,
|
|
338
|
+
String requestAccess,
|
|
339
|
+
long authDateSeconds,
|
|
340
|
+
long expiresAtMs
|
|
341
|
+
) {
|
|
342
|
+
this.id = id;
|
|
343
|
+
this.firstName = firstName;
|
|
344
|
+
this.lastName = lastName;
|
|
345
|
+
this.username = username;
|
|
346
|
+
this.photoUrl = photoUrl;
|
|
347
|
+
this.requestAccess = requestAccess;
|
|
348
|
+
this.authDateSeconds = authDateSeconds;
|
|
349
|
+
this.expiresAtMs = expiresAtMs;
|
|
350
|
+
}
|
|
351
|
+
}
|
|
352
|
+
}
|