@capgo/capacitor-social-login 8.5.2 → 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 +73 -15
- 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 +145 -7
- package/dist/esm/definitions.d.ts +85 -2
- package/dist/esm/definitions.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 +17 -1
- package/dist/esm/web.js.map +1 -1
- package/dist/plugin.cjs.js +325 -8
- package/dist/plugin.cjs.js.map +1 -1
- package/dist/plugin.js +325 -8
- 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.
|
|
@@ -558,6 +559,32 @@ Initialize method to create a script tag with Google lib. We cannot know when it
|
|
|
558
559
|
|
|
559
560
|
On Web, Google `refresh()` is not implemented, even when using `mode: 'online'`. Call `SocialLogin.login({ provider: 'google', ... })` again to obtain a fresh token.
|
|
560
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
|
+
|
|
561
588
|
## OAuth2 (Generic)
|
|
562
589
|
|
|
563
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.
|
|
@@ -878,14 +905,14 @@ Initialize the plugin
|
|
|
878
905
|
### login(...)
|
|
879
906
|
|
|
880
907
|
```typescript
|
|
881
|
-
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]; }>
|
|
882
909
|
```
|
|
883
910
|
|
|
884
911
|
Login with the selected provider
|
|
885
912
|
|
|
886
|
-
| Param | Type
|
|
887
|
-
| ------------- |
|
|
888
|
-
| **`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> |
|
|
889
916
|
|
|
890
917
|
**Returns:** <code>Promise<{ provider: T; result: ProviderResponseMap[T]; }></code>
|
|
891
918
|
|
|
@@ -895,14 +922,14 @@ Login with the selected provider
|
|
|
895
922
|
### logout(...)
|
|
896
923
|
|
|
897
924
|
```typescript
|
|
898
|
-
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>
|
|
899
926
|
```
|
|
900
927
|
|
|
901
928
|
Logout
|
|
902
929
|
|
|
903
|
-
| Param | Type
|
|
904
|
-
| ------------- |
|
|
905
|
-
| **`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> |
|
|
906
933
|
|
|
907
934
|
--------------------
|
|
908
935
|
|
|
@@ -1201,6 +1228,7 @@ And in the AndroidManifest.xml file:
|
|
|
1201
1228
|
| Prop | Type | Description |
|
|
1202
1229
|
| -------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------ |
|
|
1203
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. |
|
|
1204
1232
|
| **`twitter`** | <code>{ clientId: string; redirectUrl: string; defaultScopes?: string[]; forceLogin?: boolean; audience?: string; }</code> | |
|
|
1205
1233
|
| **`facebook`** | <code>{ appId: string; clientToken?: string; locale?: string; }</code> | |
|
|
1206
1234
|
| **`google`** | <code>{ iOSClientId?: string; iOSServerClientId?: string; webClientId?: string; mode?: 'online' \| 'offline'; hostedDomain?: string; redirectUrl?: string; }</code> | |
|
|
@@ -1320,6 +1348,27 @@ Configuration for a single OAuth2 provider instance
|
|
|
1320
1348
|
| **`email`** | <code>string \| null</code> |
|
|
1321
1349
|
|
|
1322
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
|
+
|
|
1323
1372
|
#### OAuth2LoginResponse
|
|
1324
1373
|
|
|
1325
1374
|
| Prop | Type | Description |
|
|
@@ -1378,6 +1427,15 @@ Configuration for a single OAuth2 provider instance
|
|
|
1378
1427
|
| **`forceLogin`** | <code>boolean</code> | Force the consent screen on every attempt, maps to `force_login=true`. |
|
|
1379
1428
|
|
|
1380
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
|
+
|
|
1381
1439
|
#### OAuth2LoginOptions
|
|
1382
1440
|
|
|
1383
1441
|
| Prop | Type | Description | Default |
|
|
@@ -1396,10 +1454,10 @@ Configuration for a single OAuth2 provider instance
|
|
|
1396
1454
|
|
|
1397
1455
|
#### isLoggedInOptions
|
|
1398
1456
|
|
|
1399
|
-
| Prop | Type
|
|
1400
|
-
| ---------------- |
|
|
1401
|
-
| **`provider`** | <code>'apple' \| 'google' \| 'facebook' \| 'twitter' \| 'oauth2'</code> | Provider |
|
|
1402
|
-
| **`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') |
|
|
1403
1461
|
|
|
1404
1462
|
|
|
1405
1463
|
#### AuthorizationCode
|
|
@@ -1524,7 +1582,7 @@ Construct a type with a set of properties K of type T
|
|
|
1524
1582
|
|
|
1525
1583
|
#### ProviderResponseMap
|
|
1526
1584
|
|
|
1527
|
-
<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>
|
|
1528
1586
|
|
|
1529
1587
|
|
|
1530
1588
|
#### GoogleLoginResponse
|
|
@@ -1534,7 +1592,7 @@ Construct a type with a set of properties K of type T
|
|
|
1534
1592
|
|
|
1535
1593
|
#### LoginOptions
|
|
1536
1594
|
|
|
1537
|
-
<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>
|
|
1538
1596
|
|
|
1539
1597
|
|
|
1540
1598
|
#### Extract
|
|
@@ -1546,7 +1604,7 @@ Construct a type with a set of properties K of type T
|
|
|
1546
1604
|
|
|
1547
1605
|
#### LoginResult
|
|
1548
1606
|
|
|
1549
|
-
<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>
|
|
1550
1608
|
|
|
1551
1609
|
|
|
1552
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
|
+
}
|
|
@@ -20,6 +20,7 @@ public class DependencyAvailabilityChecker {
|
|
|
20
20
|
private static Boolean facebookDependenciesAvailable = null;
|
|
21
21
|
private static Boolean appleDependenciesAvailable = null;
|
|
22
22
|
private static Boolean twitterDependenciesAvailable = null;
|
|
23
|
+
private static Boolean telegramDependenciesAvailable = null;
|
|
23
24
|
|
|
24
25
|
/**
|
|
25
26
|
* Set the plugin instance for config access
|
|
@@ -66,7 +67,7 @@ public class DependencyAvailabilityChecker {
|
|
|
66
67
|
|
|
67
68
|
/**
|
|
68
69
|
* Check if a provider's dependencies are available.
|
|
69
|
-
* @param providerName Provider name: "google", "facebook", "apple", or "
|
|
70
|
+
* @param providerName Provider name: "google", "facebook", "apple", "twitter", or "telegram"
|
|
70
71
|
* @return true if dependencies are available, false otherwise
|
|
71
72
|
*/
|
|
72
73
|
public static boolean isProviderAvailable(String providerName) {
|
|
@@ -83,6 +84,8 @@ public class DependencyAvailabilityChecker {
|
|
|
83
84
|
return isAppleAvailable();
|
|
84
85
|
case "twitter":
|
|
85
86
|
return isTwitterAvailable();
|
|
87
|
+
case "telegram":
|
|
88
|
+
return isTelegramAvailable();
|
|
86
89
|
default:
|
|
87
90
|
Log.w(LOG_TAG, "Unknown provider: " + providerName);
|
|
88
91
|
return false;
|
|
@@ -260,6 +263,22 @@ public class DependencyAvailabilityChecker {
|
|
|
260
263
|
return allAvailable;
|
|
261
264
|
}
|
|
262
265
|
|
|
266
|
+
/**
|
|
267
|
+
* Telegram Login uses the system WebView (OAuth2LoginActivity); allow config to disable it.
|
|
268
|
+
*/
|
|
269
|
+
private static boolean isTelegramAvailable() {
|
|
270
|
+
if (telegramDependenciesAvailable != null) {
|
|
271
|
+
return telegramDependenciesAvailable;
|
|
272
|
+
}
|
|
273
|
+
if (!isProviderEnabledInConfig("telegram")) {
|
|
274
|
+
telegramDependenciesAvailable = false;
|
|
275
|
+
Log.d(LOG_TAG, "Telegram provider is disabled via config");
|
|
276
|
+
return false;
|
|
277
|
+
}
|
|
278
|
+
telegramDependenciesAvailable = true;
|
|
279
|
+
return true;
|
|
280
|
+
}
|
|
281
|
+
|
|
263
282
|
/**
|
|
264
283
|
* Check if a specific class is available using reflection.
|
|
265
284
|
*/
|
|
@@ -283,5 +302,6 @@ public class DependencyAvailabilityChecker {
|
|
|
283
302
|
facebookDependenciesAvailable = null;
|
|
284
303
|
appleDependenciesAvailable = null;
|
|
285
304
|
twitterDependenciesAvailable = null;
|
|
305
|
+
telegramDependenciesAvailable = null;
|
|
286
306
|
}
|
|
287
307
|
}
|