@capgo/capacitor-social-login 8.5.2 → 8.5.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (33) hide show
  1. package/README.md +176 -24
  2. package/android/src/main/java/ee/forgr/capacitor/social/login/OAuth2LoginActivity.java +7 -0
  3. package/android/src/main/java/ee/forgr/capacitor/social/login/SocialLoginPlugin.java +31 -1
  4. package/android/src/main/java/ee/forgr/capacitor/social/login/TelegramProvider.java +352 -0
  5. package/android/src/main/java/ee/forgr/capacitor/social/login/helpers/DependencyAvailabilityChecker.java +21 -1
  6. package/dist/docs.json +299 -13
  7. package/dist/esm/auth-connect.js +2 -2
  8. package/dist/esm/auth-connect.js.map +1 -1
  9. package/dist/esm/definitions.d.ts +154 -10
  10. package/dist/esm/definitions.js.map +1 -1
  11. package/dist/esm/linkedin-provider.d.ts +17 -0
  12. package/dist/esm/linkedin-provider.js +54 -0
  13. package/dist/esm/linkedin-provider.js.map +1 -0
  14. package/dist/esm/oauth-popup-bridge.js +17 -7
  15. package/dist/esm/oauth-popup-bridge.js.map +1 -1
  16. package/dist/esm/oauth-popup-redirect.js +5 -0
  17. package/dist/esm/oauth-popup-redirect.js.map +1 -1
  18. package/dist/esm/social-login.d.ts +4 -8
  19. package/dist/esm/social-login.js +50 -3
  20. package/dist/esm/social-login.js.map +1 -1
  21. package/dist/esm/telegram-provider.d.ts +37 -0
  22. package/dist/esm/telegram-provider.js +292 -0
  23. package/dist/esm/telegram-provider.js.map +1 -0
  24. package/dist/esm/web.d.ts +4 -8
  25. package/dist/esm/web.js +52 -4
  26. package/dist/esm/web.js.map +1 -1
  27. package/dist/plugin.cjs.js +498 -51
  28. package/dist/plugin.cjs.js.map +1 -1
  29. package/dist/plugin.js +3181 -2734
  30. package/dist/plugin.js.map +1 -1
  31. package/ios/Sources/SocialLoginPlugin/SocialLoginPlugin.swift +67 -1
  32. package/ios/Sources/SocialLoginPlugin/TelegramProvider.swift +260 -0
  33. package/package.json +1 -1
@@ -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 "twitter"
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
  }