@sneat/auth-ui 0.27.42 → 0.27.43
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/esm2022/index.js +1 -0
- package/esm2022/index.js.map +1 -1
- package/esm2022/lib/pages/login-page/login-with-telegram.component.js +11 -4
- package/esm2022/lib/pages/login-page/login-with-telegram.component.js.map +1 -1
- package/esm2022/lib/pages/login-page/sneat-auth-with-telegram.service.js +45 -4
- package/esm2022/lib/pages/login-page/sneat-auth-with-telegram.service.js.map +1 -1
- package/index.d.ts +1 -0
- package/lib/pages/login-page/login-with-telegram.component.d.ts +1 -0
- package/lib/pages/login-page/sneat-auth-with-telegram.service.d.ts +6 -0
- package/package.json +1 -1
- package/tsconfig.lib.prod.tsbuildinfo +1 -1
package/esm2022/index.js
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
export * from './lib/pages/sneat-auth-routing.module';
|
|
2
2
|
export * from './lib/pages/login-page/auth-panel.component';
|
|
3
3
|
export * from './lib/pages/login-page/telegram-login-config';
|
|
4
|
+
export { LoginWithTelegramComponent } from './lib/pages/login-page/login-with-telegram.component';
|
|
4
5
|
export * from './lib/components';
|
|
5
6
|
export * from './lib/pipes/person-names.pipe';
|
|
6
7
|
export * from './lib/sso';
|
package/esm2022/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../../../libs/auth/ui/src/index.ts"],"names":[],"mappings":"AAAA,cAAc,uCAAuC,CAAC;AACtD,cAAc,6CAA6C,CAAC;AAE5D,cAAc,8CAA8C,CAAC;AAC7D,cAAc,kBAAkB,CAAC;AACjC,cAAc,+BAA+B,CAAC;AAC9C,cAAc,WAAW,CAAC;AAC1B,cAAc,6CAA6C,CAAC","sourcesContent":["export * from './lib/pages/sneat-auth-routing.module';\nexport * from './lib/pages/login-page/auth-panel.component';\nexport type { AuthPanelInitialMode } from './lib/pages/login-page/email-login-form/email-login-form.component';\nexport * from './lib/pages/login-page/telegram-login-config';\nexport * from './lib/components';\nexport * from './lib/pipes/person-names.pipe';\nexport * from './lib/sso';\nexport * from './lib/pages/login-page/auth-panel-ui-config';\n"]}
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../../../libs/auth/ui/src/index.ts"],"names":[],"mappings":"AAAA,cAAc,uCAAuC,CAAC;AACtD,cAAc,6CAA6C,CAAC;AAE5D,cAAc,8CAA8C,CAAC;AAC7D,OAAO,EAAE,0BAA0B,EAAE,MAAM,sDAAsD,CAAC;AAClG,cAAc,kBAAkB,CAAC;AACjC,cAAc,+BAA+B,CAAC;AAC9C,cAAc,WAAW,CAAC;AAC1B,cAAc,6CAA6C,CAAC","sourcesContent":["export * from './lib/pages/sneat-auth-routing.module';\nexport * from './lib/pages/login-page/auth-panel.component';\nexport type { AuthPanelInitialMode } from './lib/pages/login-page/email-login-form/email-login-form.component';\nexport * from './lib/pages/login-page/telegram-login-config';\nexport { LoginWithTelegramComponent } from './lib/pages/login-page/login-with-telegram.component';\nexport * from './lib/components';\nexport * from './lib/pipes/person-names.pipe';\nexport * from './lib/sso';\nexport * from './lib/pages/login-page/auth-panel-ui-config';\n"]}
|
|
@@ -2,6 +2,7 @@ import { DOCUMENT } from '@angular/common';
|
|
|
2
2
|
import { ChangeDetectionStrategy, Component, DestroyRef, ElementRef, inject, input, signal, } from '@angular/core';
|
|
3
3
|
import { takeUntilDestroyed } from '@angular/core/rxjs-interop';
|
|
4
4
|
import { ErrorLogger } from '@sneat/core';
|
|
5
|
+
import { newRandomId } from '@sneat/random';
|
|
5
6
|
import { SneatAuthWithTelegramService, } from './sneat-auth-with-telegram.service';
|
|
6
7
|
import { TelegramLoginConfig, resolveTelegramBotID, } from './telegram-login-config';
|
|
7
8
|
import { loadTelegramLoginScript, telegramLoginApi, } from './telegram-login-js';
|
|
@@ -23,6 +24,12 @@ export class LoginWithTelegramComponent {
|
|
|
23
24
|
this.telegramLoginConfig = inject(TelegramLoginConfig, {
|
|
24
25
|
optional: true,
|
|
25
26
|
});
|
|
27
|
+
this.legacyCallbackName = `sneatTelegramAuth_${newRandomId({
|
|
28
|
+
len: 20,
|
|
29
|
+
digits: true,
|
|
30
|
+
lowerCaseLetters: true,
|
|
31
|
+
upperCaseLetters: true,
|
|
32
|
+
})}`;
|
|
26
33
|
this.isUserAuthenticated = input(false, /* @ts-ignore */
|
|
27
34
|
...(ngDevMode ? [{ debugName: "isUserAuthenticated" }] : /* istanbul ignore next */ []));
|
|
28
35
|
this.botID = input(resolveTelegramBotID(this.telegramLoginConfig, location.hostname), /* @ts-ignore */
|
|
@@ -40,8 +47,8 @@ export class LoginWithTelegramComponent {
|
|
|
40
47
|
this.destroyRef.onDestroy(() => {
|
|
41
48
|
clearInterval(this.nonceTimer);
|
|
42
49
|
const host = this.document.defaultView;
|
|
43
|
-
if (host && host.
|
|
44
|
-
delete host.
|
|
50
|
+
if (host && host[this.legacyCallbackName] === this.legacyCallback)
|
|
51
|
+
delete host[this.legacyCallbackName];
|
|
45
52
|
});
|
|
46
53
|
}
|
|
47
54
|
ngOnInit() {
|
|
@@ -115,7 +122,7 @@ export class LoginWithTelegramComponent {
|
|
|
115
122
|
const host = this.document.defaultView;
|
|
116
123
|
if (!host)
|
|
117
124
|
return;
|
|
118
|
-
host.
|
|
125
|
+
host[this.legacyCallbackName] = this.legacyCallback;
|
|
119
126
|
const script = this.document.createElement('script');
|
|
120
127
|
script.src = 'https://telegram.org/js/telegram-widget.js?22';
|
|
121
128
|
script.setAttribute('data-telegram-login', botID);
|
|
@@ -124,7 +131,7 @@ export class LoginWithTelegramComponent {
|
|
|
124
131
|
if (!this.userPic()) {
|
|
125
132
|
script.setAttribute('data-userpic', 'false');
|
|
126
133
|
}
|
|
127
|
-
script.setAttribute('data-onauth',
|
|
134
|
+
script.setAttribute('data-onauth', `${this.legacyCallbackName}(user)`);
|
|
128
135
|
this.el.nativeElement.appendChild(script);
|
|
129
136
|
}
|
|
130
137
|
static { this.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "22.1.3", ngImport: i0, type: LoginWithTelegramComponent, deps: [], target: i0.ɵɵFactoryTarget.Component }); }
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"login-with-telegram.component.js","sourceRoot":"","sources":["../../../../../../../../libs/auth/ui/src/lib/pages/login-page/login-with-telegram.component.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,MAAM,iBAAiB,CAAC;AAC3C,OAAO,EACL,uBAAuB,EACvB,SAAS,EACT,UAAU,EACV,UAAU,EAEV,MAAM,EACN,KAAK,EACL,MAAM,GACP,MAAM,eAAe,CAAC;AACvB,OAAO,EAAE,kBAAkB,EAAE,MAAM,4BAA4B,CAAC;AAChE,OAAO,EAAE,WAAW,EAAgB,MAAM,aAAa,CAAC;AACxD,OAAO,EAGL,4BAA4B,GAC7B,MAAM,oCAAoC,CAAC;AAC5C,OAAO,EACL,mBAAmB,EACnB,oBAAoB,GACrB,MAAM,yBAAyB,CAAC;AACjC,OAAO,EAEL,uBAAuB,EACvB,gBAAgB,GACjB,MAAM,qBAAqB,CAAC;;AAE7B,+DAA+D;AAC/D,MAAM,qBAAqB,GAAG,CAAC,GAAG,EAAE,GAAG,IAAI,CAAC;AAE5C;;;;GAIG;AAwCH,MAAM,OAAO,0BAA0B;IAarC;QAZiB,OAAE,GAAG,MAAM,CAAC,UAAU,CAAC,CAAC;QACxB,aAAQ,GAAG,MAAM,CAAW,QAAQ,CAAC,CAAC;QACtC,eAAU,GAAG,MAAM,CAAC,UAAU,CAAC,CAAC;QAChC,gBAAW,GAAG,MAAM,CAAe,WAAW,CAAC,CAAC;QACxD,qBAAgB,GAAG,MAAM,CAAC,4BAA4B,CAAC,CAAC;QAChD,wBAAmB,GAAG,MAAM,CAAC,mBAAmB,EAAE;YACjE,QAAQ,EAAE,IAAI;SACf,CAAC,CAAC;QAiBa,wBAAmB,GAAG,KAAK,CAAC,KAAK;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AAC,YAAY,CAAC,qBAAqB,EAAE,KAAK,CAAC,CAAC;QAClD,MAAM,CAAC,YAAY,CAAC,qBAAqB,EAAE,IAAI,CAAC,aAAa,EAAE,CAAC,CAAC;QACjE,MAAM,CAAC,YAAY,CAAC,WAAW,EAAE,IAAI,CAAC,IAAI,EAAE,CAAC,CAAC;QAC9C,IAAI,CAAC,IAAI,CAAC,OAAO,EAAE,EAAE,CAAC;YACpB,MAAM,CAAC,YAAY,CAAC,cAAc,EAAE,OAAO,CAAC,CAAC;QAC/C,CAAC;QACD,MAAM,CAAC,YAAY,CAAC,aAAa,EAAE,sBAAsB,CAAC,CAAC;QAC3D,IAAI,CAAC,EAAE,CAAC,aAAa,CAAC,WAAW,CAAC,MAAM,CAAC,CAAC;IAC5C,CAAC;8GA7IU,0BAA0B;kGAA1B,0BAA0B,ivBAtC1B,CAAC,4BAA4B,CAAC,0BAG/B;;;;;;;;;;;GAWT;;2FAwBU,0BAA0B;kBAvCtC,SAAS;gCACG,CAAC,4BAA4B,CAAC,YAC/B,2BAA2B,mBACpB,uBAAuB,CAAC,MAAM,YACrC;;;;;;;;;;;GAWT","sourcesContent":["import { DOCUMENT } from '@angular/common';\nimport {\n ChangeDetectionStrategy,\n Component,\n DestroyRef,\n ElementRef,\n OnInit,\n inject,\n input,\n signal,\n} from '@angular/core';\nimport { takeUntilDestroyed } from '@angular/core/rxjs-interop';\nimport { ErrorLogger, IErrorLogger } from '@sneat/core';\nimport {\n ITelegramAuthData,\n ITelegramOIDCNonce,\n SneatAuthWithTelegramService,\n} from './sneat-auth-with-telegram.service';\nimport {\n TelegramLoginConfig,\n resolveTelegramBotID,\n} from './telegram-login-config';\nimport {\n ITelegramLoginResult,\n loadTelegramLoginScript,\n telegramLoginApi,\n} from './telegram-login-js';\n\n/** Server nonces live 10 minutes; refresh well inside that. */\nconst OIDC_NONCE_REFRESH_MS = 8 * 60 * 1000;\n\n/**\n * Telegram login button. Bots with OpenID login configured server-side use\n * Telegram's OpenID popup (telegram-login.js); others keep the legacy Login\n * Widget (telegram-widget.js), e.g. the local dev bot.\n */\n@Component({\n providers: [SneatAuthWithTelegramService],\n selector: 'sneat-login-with-telegram',\n changeDetection: ChangeDetectionStrategy.OnPush,\n template: `\n @if (oidc(); as cfg) {\n <button\n type=\"button\"\n class=\"tg-oidc-login\"\n [attr.data-size]=\"size()\"\n (click)=\"loginWithOIDC(cfg)\"\n >\n Log in with Telegram\n </button>\n }\n `,\n styles: [\n `\n .tg-oidc-login {\n background: #54a9eb;\n color: #fff;\n border: 0;\n border-radius: 20px;\n font-weight: 500;\n cursor: pointer;\n font-size: 16px;\n padding: 10px 21px;\n }\n .tg-oidc-login[data-size='medium'] {\n font-size: 14px;\n padding: 8px 16px;\n }\n .tg-oidc-login[data-size='small'] {\n font-size: 13px;\n padding: 6px 12px;\n }\n `,\n ],\n})\nexport class LoginWithTelegramComponent implements OnInit {\n private readonly el = inject(ElementRef);\n private readonly document = inject<Document>(DOCUMENT);\n private readonly destroyRef = inject(DestroyRef);\n private readonly errorLogger = inject<IErrorLogger>(ErrorLogger);\n readonly authWithTelegram = inject(SneatAuthWithTelegramService);\n private readonly telegramLoginConfig = inject(TelegramLoginConfig, {\n optional: true,\n });\n\n private nonceTimer?: ReturnType<typeof setInterval>;\n private legacyCallback?: (data: ITelegramAuthData) => void;\n\n constructor() {\n // Register while the view is alive, never from a late loader continuation.\n this.destroyRef.onDestroy(() => {\n clearInterval(this.nonceTimer);\n const host = this.document.defaultView as\n | (Window & { onTelegramAuth?: (data: ITelegramAuthData) => void })\n | null;\n if (host && host.onTelegramAuth === this.legacyCallback)\n delete host.onTelegramAuth;\n });\n }\n\n public readonly isUserAuthenticated = input(false);\n\n public readonly botID = input<string>(\n resolveTelegramBotID(this.telegramLoginConfig, location.hostname),\n );\n\n public readonly size = input<'small' | 'medium' | 'large'>('large');\n public readonly requestAccess = input<'write' | 'read'>('write');\n public readonly userPic = input(true);\n\n /** Set once the OpenID popup can be offered: Client ID plus a live nonce. */\n protected readonly oidc = signal<ITelegramOIDCNonce | undefined>(undefined);\n\n ngOnInit() {\n const botID = this.botID();\n if (!botID) {\n return;\n }\n this.authWithTelegram\n .getTelegramOIDCNonce(botID)\n .pipe(takeUntilDestroyed(this.destroyRef))\n .subscribe({\n next: (cfg) => this.startOIDC(botID, cfg),\n // The server answers 400 for a bot without OpenID login configured.\n error: () => this.startLegacyWidget(botID),\n });\n }\n\n protected loginWithOIDC(cfg: ITelegramOIDCNonce): void {\n if (this.destroyRef.destroyed) return;\n const api = telegramLoginApi(this.document.defaultView ?? window);\n if (!api) {\n this.errorLogger.logErrorHandler('Telegram login library not loaded')(\n new Error('window.Telegram.Login is undefined'),\n );\n return;\n }\n const botID = this.botID();\n // Called straight from the click handler: the library opens its popup\n // synchronously, which keeps the browser's popup blocker out of the way.\n api.auth(\n {\n client_id: Number(cfg.clientID),\n nonce: cfg.nonce,\n request_access: this.requestAccess() === 'write' ? ['write'] : [],\n },\n (result: ITelegramLoginResult) => {\n if (result.id_token && !this.destroyRef.destroyed) {\n this.authWithTelegram.loginWithTelegramOIDC(\n botID,\n result.id_token,\n this.isUserAuthenticated(),\n );\n this.refreshNonce(botID);\n }\n // result.error (e.g. the user closed the popup) needs no action:\n // the button stays, and the user can try again.\n },\n );\n }\n\n private startOIDC(botID: string, cfg: ITelegramOIDCNonce): void {\n loadTelegramLoginScript(this.document).then(() => {\n if (this.destroyRef.destroyed) return;\n this.oidc.set(cfg);\n clearInterval(this.nonceTimer);\n this.nonceTimer = setInterval(\n () => this.refreshNonce(botID),\n OIDC_NONCE_REFRESH_MS,\n );\n }, this.errorLogger.logErrorHandler('Failed to load Telegram login library'));\n }\n\n private refreshNonce(botID: string): void {\n this.authWithTelegram\n .getTelegramOIDCNonce(botID)\n .pipe(takeUntilDestroyed(this.destroyRef))\n .subscribe({\n next: (cfg) => this.oidc.set(cfg),\n error: this.errorLogger.logErrorHandler(\n 'Failed to refresh Telegram login nonce',\n ),\n });\n }\n\n private startLegacyWidget(botID: string): void {\n if (this.destroyRef.destroyed) return;\n this.legacyCallback = (tgAuthData: ITelegramAuthData) => {\n if (this.destroyRef.destroyed) return;\n // https://core.telegram.org/widgets/login-legacy\n // After a successful authorization, the widget returns data\n // by calling the callback function data-onauth with the JSON-object containing\n // id, first_name, last_name, username, photo_url, auth_date and hash fields.\n this.authWithTelegram.loginWithTelegram(\n botID,\n tgAuthData,\n this.isUserAuthenticated(),\n );\n };\n\n const host = this.document.defaultView as\n | (Window & { onTelegramAuth?: (data: ITelegramAuthData) => void })\n | null;\n if (!host) return;\n host.onTelegramAuth = this.legacyCallback;\n const script = this.document.createElement('script');\n\n script.src = 'https://telegram.org/js/telegram-widget.js?22';\n script.setAttribute('data-telegram-login', botID);\n script.setAttribute('data-request-access', this.requestAccess());\n script.setAttribute('data-size', this.size());\n if (!this.userPic()) {\n script.setAttribute('data-userpic', 'false');\n }\n script.setAttribute('data-onauth', 'onTelegramAuth(user)');\n this.el.nativeElement.appendChild(script);\n }\n}\n"]}
|
|
1
|
+
{"version":3,"file":"login-with-telegram.component.js","sourceRoot":"","sources":["../../../../../../../../libs/auth/ui/src/lib/pages/login-page/login-with-telegram.component.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,MAAM,iBAAiB,CAAC;AAC3C,OAAO,EACL,uBAAuB,EACvB,SAAS,EACT,UAAU,EACV,UAAU,EAEV,MAAM,EACN,KAAK,EACL,MAAM,GACP,MAAM,eAAe,CAAC;AACvB,OAAO,EAAE,kBAAkB,EAAE,MAAM,4BAA4B,CAAC;AAChE,OAAO,EAAE,WAAW,EAAgB,MAAM,aAAa,CAAC;AACxD,OAAO,EAAE,WAAW,EAAE,MAAM,eAAe,CAAC;AAC5C,OAAO,EAGL,4BAA4B,GAC7B,MAAM,oCAAoC,CAAC;AAC5C,OAAO,EACL,mBAAmB,EACnB,oBAAoB,GACrB,MAAM,yBAAyB,CAAC;AACjC,OAAO,EAEL,uBAAuB,EACvB,gBAAgB,GACjB,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AAC,MAAM,CAAC,CAAC;IAC5C,CAAC;8GA/IU,0BAA0B;kGAA1B,0BAA0B,ivBAtC1B,CAAC,4BAA4B,CAAC,0BAG/B;;;;;;;;;;;GAWT;;2FAwBU,0BAA0B;kBAvCtC,SAAS;gCACG,CAAC,4BAA4B,CAAC,YAC/B,2BAA2B,mBACpB,uBAAuB,CAAC,MAAM,YACrC;;;;;;;;;;;GAWT","sourcesContent":["import { DOCUMENT } from '@angular/common';\nimport {\n ChangeDetectionStrategy,\n Component,\n DestroyRef,\n ElementRef,\n OnInit,\n inject,\n input,\n signal,\n} from '@angular/core';\nimport { takeUntilDestroyed } from '@angular/core/rxjs-interop';\nimport { ErrorLogger, IErrorLogger } from '@sneat/core';\nimport { newRandomId } from '@sneat/random';\nimport {\n ITelegramAuthData,\n ITelegramOIDCNonce,\n SneatAuthWithTelegramService,\n} from './sneat-auth-with-telegram.service';\nimport {\n TelegramLoginConfig,\n resolveTelegramBotID,\n} from './telegram-login-config';\nimport {\n ITelegramLoginResult,\n loadTelegramLoginScript,\n telegramLoginApi,\n} from './telegram-login-js';\n\n/** Server nonces live 10 minutes; refresh well inside that. */\nconst OIDC_NONCE_REFRESH_MS = 8 * 60 * 1000;\n\ntype LegacyTelegramWindow = Window & {\n [name: `sneatTelegramAuth_${string}`]:\n ((data: ITelegramAuthData) => void) | undefined;\n};\n\n/**\n * Telegram login button. Bots with OpenID login configured server-side use\n * Telegram's OpenID popup (telegram-login.js); others keep the legacy Login\n * Widget (telegram-widget.js), e.g. the local dev bot.\n */\n@Component({\n providers: [SneatAuthWithTelegramService],\n selector: 'sneat-login-with-telegram',\n changeDetection: ChangeDetectionStrategy.OnPush,\n template: `\n @if (oidc(); as cfg) {\n <button\n type=\"button\"\n class=\"tg-oidc-login\"\n [attr.data-size]=\"size()\"\n (click)=\"loginWithOIDC(cfg)\"\n >\n Log in with Telegram\n </button>\n }\n `,\n styles: [\n `\n .tg-oidc-login {\n background: #54a9eb;\n color: #fff;\n border: 0;\n border-radius: 20px;\n font-weight: 500;\n cursor: pointer;\n font-size: 16px;\n padding: 10px 21px;\n }\n .tg-oidc-login[data-size='medium'] {\n font-size: 14px;\n padding: 8px 16px;\n }\n .tg-oidc-login[data-size='small'] {\n font-size: 13px;\n padding: 6px 12px;\n }\n `,\n ],\n})\nexport class LoginWithTelegramComponent implements OnInit {\n private readonly el = inject(ElementRef);\n private readonly document = inject<Document>(DOCUMENT);\n private readonly destroyRef = inject(DestroyRef);\n private readonly errorLogger = inject<IErrorLogger>(ErrorLogger);\n readonly authWithTelegram = inject(SneatAuthWithTelegramService);\n private readonly telegramLoginConfig = inject(TelegramLoginConfig, {\n optional: true,\n });\n\n private nonceTimer?: ReturnType<typeof setInterval>;\n private legacyCallback?: (data: ITelegramAuthData) => void;\n private readonly legacyCallbackName = `sneatTelegramAuth_${newRandomId({\n len: 20,\n digits: true,\n lowerCaseLetters: true,\n upperCaseLetters: true,\n })}` as const;\n\n constructor() {\n // Register while the view is alive, never from a late loader continuation.\n this.destroyRef.onDestroy(() => {\n clearInterval(this.nonceTimer);\n const host = this.document.defaultView as LegacyTelegramWindow | null;\n if (host && host[this.legacyCallbackName] === this.legacyCallback)\n delete host[this.legacyCallbackName];\n });\n }\n\n public readonly isUserAuthenticated = input(false);\n\n public readonly botID = input<string>(\n resolveTelegramBotID(this.telegramLoginConfig, location.hostname),\n );\n\n public readonly size = input<'small' | 'medium' | 'large'>('large');\n public readonly requestAccess = input<'write' | 'read'>('write');\n public readonly userPic = input(true);\n\n /** Set once the OpenID popup can be offered: Client ID plus a live nonce. */\n protected readonly oidc = signal<ITelegramOIDCNonce | undefined>(undefined);\n\n ngOnInit() {\n const botID = this.botID();\n if (!botID) {\n return;\n }\n this.authWithTelegram\n .getTelegramOIDCNonce(botID)\n .pipe(takeUntilDestroyed(this.destroyRef))\n .subscribe({\n next: (cfg) => this.startOIDC(botID, cfg),\n // The server answers 400 for a bot without OpenID login configured.\n error: () => this.startLegacyWidget(botID),\n });\n }\n\n protected loginWithOIDC(cfg: ITelegramOIDCNonce): void {\n if (this.destroyRef.destroyed) return;\n const api = telegramLoginApi(this.document.defaultView ?? window);\n if (!api) {\n this.errorLogger.logErrorHandler('Telegram login library not loaded')(\n new Error('window.Telegram.Login is undefined'),\n );\n return;\n }\n const botID = this.botID();\n // Called straight from the click handler: the library opens its popup\n // synchronously, which keeps the browser's popup blocker out of the way.\n api.auth(\n {\n client_id: Number(cfg.clientID),\n nonce: cfg.nonce,\n request_access: this.requestAccess() === 'write' ? ['write'] : [],\n },\n (result: ITelegramLoginResult) => {\n if (result.id_token && !this.destroyRef.destroyed) {\n this.authWithTelegram.loginWithTelegramOIDC(\n botID,\n result.id_token,\n this.isUserAuthenticated(),\n );\n this.refreshNonce(botID);\n }\n // result.error (e.g. the user closed the popup) needs no action:\n // the button stays, and the user can try again.\n },\n );\n }\n\n private startOIDC(botID: string, cfg: ITelegramOIDCNonce): void {\n loadTelegramLoginScript(this.document).then(() => {\n if (this.destroyRef.destroyed) return;\n this.oidc.set(cfg);\n clearInterval(this.nonceTimer);\n this.nonceTimer = setInterval(\n () => this.refreshNonce(botID),\n OIDC_NONCE_REFRESH_MS,\n );\n }, this.errorLogger.logErrorHandler('Failed to load Telegram login library'));\n }\n\n private refreshNonce(botID: string): void {\n this.authWithTelegram\n .getTelegramOIDCNonce(botID)\n .pipe(takeUntilDestroyed(this.destroyRef))\n .subscribe({\n next: (cfg) => this.oidc.set(cfg),\n error: this.errorLogger.logErrorHandler(\n 'Failed to refresh Telegram login nonce',\n ),\n });\n }\n\n private startLegacyWidget(botID: string): void {\n if (this.destroyRef.destroyed) return;\n this.legacyCallback = (tgAuthData: ITelegramAuthData) => {\n if (this.destroyRef.destroyed) return;\n // https://core.telegram.org/widgets/login-legacy\n // After a successful authorization, the widget returns data\n // by calling the callback function data-onauth with the JSON-object containing\n // id, first_name, last_name, username, photo_url, auth_date and hash fields.\n this.authWithTelegram.loginWithTelegram(\n botID,\n tgAuthData,\n this.isUserAuthenticated(),\n );\n };\n\n const host = this.document.defaultView as LegacyTelegramWindow | null;\n if (!host) return;\n host[this.legacyCallbackName] = this.legacyCallback;\n const script = this.document.createElement('script');\n\n script.src = 'https://telegram.org/js/telegram-widget.js?22';\n script.setAttribute('data-telegram-login', botID);\n script.setAttribute('data-request-access', this.requestAccess());\n script.setAttribute('data-size', this.size());\n if (!this.userPic()) {\n script.setAttribute('data-userpic', 'false');\n }\n script.setAttribute('data-onauth', `${this.legacyCallbackName}(user)`);\n this.el.nativeElement.appendChild(script);\n }\n}\n"]}
|
|
@@ -1,13 +1,45 @@
|
|
|
1
|
-
import { Injectable, inject } from '@angular/core';
|
|
1
|
+
import { DestroyRef, Injectable, inject } from '@angular/core';
|
|
2
|
+
import { takeUntilDestroyed } from '@angular/core/rxjs-interop';
|
|
2
3
|
import { SneatApiService } from '@sneat/api';
|
|
3
|
-
import { SneatAuthStateService } from '@sneat/auth-core';
|
|
4
|
+
import { AuthStatuses, SneatAuthStateService, } from '@sneat/auth-core';
|
|
4
5
|
import { ErrorLogger } from '@sneat/core';
|
|
6
|
+
import { filter, take, takeUntil } from 'rxjs';
|
|
5
7
|
import * as i0 from "@angular/core";
|
|
6
8
|
export class SneatAuthWithTelegramService {
|
|
7
9
|
constructor() {
|
|
8
10
|
this.errorLogger = inject(ErrorLogger);
|
|
9
11
|
this.apiService = inject(SneatApiService);
|
|
10
12
|
this.authService = inject(SneatAuthStateService);
|
|
13
|
+
this.destroyRef = inject(DestroyRef);
|
|
14
|
+
this.linkContextInvalidated = false;
|
|
15
|
+
// Auth state replays its current principal synchronously. If it is not
|
|
16
|
+
// ready, authenticated linking fails closed for this control's lifetime.
|
|
17
|
+
let initialState;
|
|
18
|
+
this.authService.authState
|
|
19
|
+
.pipe(take(1), takeUntilDestroyed(this.destroyRef))
|
|
20
|
+
.subscribe((state) => {
|
|
21
|
+
initialState = state;
|
|
22
|
+
});
|
|
23
|
+
this.linkUserID =
|
|
24
|
+
initialState?.status === AuthStatuses.authenticated
|
|
25
|
+
? initialState.user?.uid
|
|
26
|
+
: undefined;
|
|
27
|
+
this.authService.authState
|
|
28
|
+
.pipe(takeUntilDestroyed(this.destroyRef))
|
|
29
|
+
.subscribe((state) => {
|
|
30
|
+
if (!this.matchesLinkUser(state)) {
|
|
31
|
+
// Never revive an old popup after an A -> B -> A account transition.
|
|
32
|
+
this.linkContextInvalidated = true;
|
|
33
|
+
}
|
|
34
|
+
});
|
|
35
|
+
}
|
|
36
|
+
matchesLinkUser(state) {
|
|
37
|
+
return (!!this.linkUserID &&
|
|
38
|
+
state.status === AuthStatuses.authenticated &&
|
|
39
|
+
state.user?.uid === this.linkUserID);
|
|
40
|
+
}
|
|
41
|
+
linkContextIsCurrent() {
|
|
42
|
+
return !!this.linkUserID && !this.linkContextInvalidated;
|
|
11
43
|
}
|
|
12
44
|
/** Legacy Login Widget: posts the widget's signed payload. */
|
|
13
45
|
loginWithTelegram(botID, tgAuthData, isUserAuthenticated) {
|
|
@@ -25,11 +57,20 @@ export class SneatAuthWithTelegramService {
|
|
|
25
57
|
this.signIn('auth/login-from-telegram-oidc?botID=' + botID, { id_token: idToken }, isUserAuthenticated);
|
|
26
58
|
}
|
|
27
59
|
signIn(endpoint, body, isUserAuthenticated) {
|
|
60
|
+
if (isUserAuthenticated && !this.linkContextIsCurrent()) {
|
|
61
|
+
this.errorLogger.logErrorHandler('Telegram account linking stopped')(new Error('Your sign-in changed. Reload this page before authorizing Telegram.'));
|
|
62
|
+
return;
|
|
63
|
+
}
|
|
28
64
|
const request = isUserAuthenticated
|
|
29
65
|
? this.apiService.post(endpoint, body)
|
|
30
66
|
: this.apiService.postAsAnonymous(endpoint, body);
|
|
31
|
-
|
|
67
|
+
const guardedRequest = isUserAuthenticated
|
|
68
|
+
? request.pipe(takeUntil(this.authService.authState.pipe(filter((state) => !this.matchesLinkUser(state)))))
|
|
69
|
+
: request;
|
|
70
|
+
guardedRequest.pipe(takeUntilDestroyed(this.destroyRef)).subscribe({
|
|
32
71
|
next: (response) => {
|
|
72
|
+
if (isUserAuthenticated && !this.linkContextIsCurrent())
|
|
73
|
+
return;
|
|
33
74
|
this.authService
|
|
34
75
|
.signInWithToken(response.token)
|
|
35
76
|
.catch(this.errorLogger.logErrorHandler('Failed to sign-in with custom token'));
|
|
@@ -42,5 +83,5 @@ export class SneatAuthWithTelegramService {
|
|
|
42
83
|
}
|
|
43
84
|
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "22.1.3", ngImport: i0, type: SneatAuthWithTelegramService, decorators: [{
|
|
44
85
|
type: Injectable
|
|
45
|
-
}] });
|
|
86
|
+
}], ctorParameters: () => [] });
|
|
46
87
|
//# sourceMappingURL=sneat-auth-with-telegram.service.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"sneat-auth-with-telegram.service.js","sourceRoot":"","sources":["../../../../../../../../libs/auth/ui/src/lib/pages/login-page/sneat-auth-with-telegram.service.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,MAAM,EAAE,MAAM,eAAe,CAAC;
|
|
1
|
+
{"version":3,"file":"sneat-auth-with-telegram.service.js","sourceRoot":"","sources":["../../../../../../../../libs/auth/ui/src/lib/pages/login-page/sneat-auth-with-telegram.service.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,UAAU,EAAE,MAAM,EAAE,MAAM,eAAe,CAAC;AAC/D,OAAO,EAAE,kBAAkB,EAAE,MAAM,4BAA4B,CAAC;AAChE,OAAO,EAAE,eAAe,EAAE,MAAM,YAAY,CAAC;AAC7C,OAAO,EACL,YAAY,EAEZ,qBAAqB,GACtB,MAAM,kBAAkB,CAAC;AAC1B,OAAO,EAAE,WAAW,EAAgB,MAAM,aAAa,CAAC;AACxD,OAAO,EAAc,MAAM,EAAE,IAAI,EAAE,SAAS,EAAE,MAAM,MAAM,CAAC;;AAuB3D,MAAM,OAAO,4BAA4B;IAQvC;QAPiB,gBAAW,GAAG,MAAM,CAAe,WAAW,C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AO,CAAC;QACZ,cAAc,CAAC,IAAI,CAAC,kBAAkB,CAAC,IAAI,CAAC,UAAU,CAAC,CAAC,CAAC,SAAS,CAAC;YACjE,IAAI,EAAE,CAAC,QAAQ,EAAE,EAAE;gBACjB,IAAI,mBAAmB,IAAI,CAAC,IAAI,CAAC,oBAAoB,EAAE;oBAAE,OAAO;gBAChE,IAAI,CAAC,WAAW;qBACb,eAAe,CAAC,QAAQ,CAAC,KAAK,CAAC;qBAC/B,KAAK,CACJ,IAAI,CAAC,WAAW,CAAC,eAAe,CAC9B,qCAAqC,CACtC,CACF,CAAC;YACN,CAAC;YACD,KAAK,EAAE,IAAI,CAAC,WAAW,CAAC,eAAe,CAAC,6BAA6B,CAAC;SACvE,CAAC,CAAC;IACL,CAAC;8GAtHU,4BAA4B;kHAA5B,4BAA4B;;2FAA5B,4BAA4B;kBADxC,UAAU","sourcesContent":["import { DestroyRef, Injectable, inject } from '@angular/core';\nimport { takeUntilDestroyed } from '@angular/core/rxjs-interop';\nimport { SneatApiService } from '@sneat/api';\nimport {\n AuthStatuses,\n ISneatAuthState,\n SneatAuthStateService,\n} from '@sneat/auth-core';\nimport { ErrorLogger, IErrorLogger } from '@sneat/core';\nimport { Observable, filter, take, takeUntil } from 'rxjs';\n\nexport interface ITelegramAuthData {\n id: number;\n first_name: string;\n last_name: string;\n username?: string;\n photo_url?: string;\n auth_date: number;\n hash: string;\n}\n\n/** What the server hands the page before it opens Telegram's OpenID popup. */\nexport interface ITelegramOIDCNonce {\n clientID: string;\n nonce: string;\n}\n\ninterface IResponse {\n token: string;\n}\n\n@Injectable()\nexport class SneatAuthWithTelegramService {\n private readonly errorLogger = inject<IErrorLogger>(ErrorLogger);\n private readonly apiService = inject(SneatApiService);\n private readonly authService = inject(SneatAuthStateService);\n private readonly destroyRef = inject(DestroyRef);\n private readonly linkUserID?: string;\n private linkContextInvalidated = false;\n\n constructor() {\n // Auth state replays its current principal synchronously. If it is not\n // ready, authenticated linking fails closed for this control's lifetime.\n let initialState: ISneatAuthState | undefined;\n this.authService.authState\n .pipe(take(1), takeUntilDestroyed(this.destroyRef))\n .subscribe((state) => {\n initialState = state;\n });\n this.linkUserID =\n initialState?.status === AuthStatuses.authenticated\n ? initialState.user?.uid\n : undefined;\n this.authService.authState\n .pipe(takeUntilDestroyed(this.destroyRef))\n .subscribe((state) => {\n if (!this.matchesLinkUser(state)) {\n // Never revive an old popup after an A -> B -> A account transition.\n this.linkContextInvalidated = true;\n }\n });\n }\n\n private matchesLinkUser(state: ISneatAuthState): boolean {\n return (\n !!this.linkUserID &&\n state.status === AuthStatuses.authenticated &&\n state.user?.uid === this.linkUserID\n );\n }\n\n private linkContextIsCurrent(): boolean {\n return !!this.linkUserID && !this.linkContextInvalidated;\n }\n\n /** Legacy Login Widget: posts the widget's signed payload. */\n public loginWithTelegram(\n botID: string,\n tgAuthData: ITelegramAuthData,\n isUserAuthenticated: boolean,\n ): void {\n this.signIn(\n 'auth/login-from-telegram-widget?botID=' + botID,\n tgAuthData,\n isUserAuthenticated,\n );\n }\n\n /**\n * Asks the server for the bot's OpenID Client ID and a fresh nonce. Fails\n * with 400 for a bot that has no OpenID login configured server-side.\n */\n public getTelegramOIDCNonce(botID: string): Observable<ITelegramOIDCNonce> {\n return this.apiService.postAsAnonymous<ITelegramOIDCNonce>(\n 'auth/telegram-oidc-nonce?botID=' + botID,\n {},\n );\n }\n\n /** OpenID login: posts the id_token Telegram's popup returned. */\n public loginWithTelegramOIDC(\n botID: string,\n idToken: string,\n isUserAuthenticated: boolean,\n ): void {\n this.signIn(\n 'auth/login-from-telegram-oidc?botID=' + botID,\n { id_token: idToken },\n isUserAuthenticated,\n );\n }\n\n private signIn(\n endpoint: string,\n body: unknown,\n isUserAuthenticated: boolean,\n ): void {\n if (isUserAuthenticated && !this.linkContextIsCurrent()) {\n this.errorLogger.logErrorHandler('Telegram account linking stopped')(\n new Error(\n 'Your sign-in changed. Reload this page before authorizing Telegram.',\n ),\n );\n return;\n }\n const request = isUserAuthenticated\n ? this.apiService.post<IResponse>(endpoint, body)\n : this.apiService.postAsAnonymous<IResponse>(endpoint, body);\n const guardedRequest = isUserAuthenticated\n ? request.pipe(\n takeUntil(\n this.authService.authState.pipe(\n filter((state) => !this.matchesLinkUser(state)),\n ),\n ),\n )\n : request;\n guardedRequest.pipe(takeUntilDestroyed(this.destroyRef)).subscribe({\n next: (response) => {\n if (isUserAuthenticated && !this.linkContextIsCurrent()) return;\n this.authService\n .signInWithToken(response.token)\n .catch(\n this.errorLogger.logErrorHandler(\n 'Failed to sign-in with custom token',\n ),\n );\n },\n error: this.errorLogger.logErrorHandler('signInWithTelegram() error:'),\n });\n }\n}\n"]}
|
package/index.d.ts
CHANGED
|
@@ -2,6 +2,7 @@ export * from './lib/pages/sneat-auth-routing.module';
|
|
|
2
2
|
export * from './lib/pages/login-page/auth-panel.component';
|
|
3
3
|
export type { AuthPanelInitialMode } from './lib/pages/login-page/email-login-form/email-login-form.component';
|
|
4
4
|
export * from './lib/pages/login-page/telegram-login-config';
|
|
5
|
+
export { LoginWithTelegramComponent } from './lib/pages/login-page/login-with-telegram.component';
|
|
5
6
|
export * from './lib/components';
|
|
6
7
|
export * from './lib/pipes/person-names.pipe';
|
|
7
8
|
export * from './lib/sso';
|
|
@@ -15,6 +15,7 @@ export declare class LoginWithTelegramComponent implements OnInit {
|
|
|
15
15
|
private readonly telegramLoginConfig;
|
|
16
16
|
private nonceTimer?;
|
|
17
17
|
private legacyCallback?;
|
|
18
|
+
private readonly legacyCallbackName;
|
|
18
19
|
constructor();
|
|
19
20
|
readonly isUserAuthenticated: import("@angular/core").InputSignal<boolean>;
|
|
20
21
|
readonly botID: import("@angular/core").InputSignal<string>;
|
|
@@ -18,6 +18,12 @@ export declare class SneatAuthWithTelegramService {
|
|
|
18
18
|
private readonly errorLogger;
|
|
19
19
|
private readonly apiService;
|
|
20
20
|
private readonly authService;
|
|
21
|
+
private readonly destroyRef;
|
|
22
|
+
private readonly linkUserID?;
|
|
23
|
+
private linkContextInvalidated;
|
|
24
|
+
constructor();
|
|
25
|
+
private matchesLinkUser;
|
|
26
|
+
private linkContextIsCurrent;
|
|
21
27
|
/** Legacy Login Widget: posts the widget's signed payload. */
|
|
22
28
|
loginWithTelegram(botID: string, tgAuthData: ITelegramAuthData, isUserAuthenticated: boolean): void;
|
|
23
29
|
/**
|