-
Notifications
You must be signed in to change notification settings - Fork 5
oko_api, oko_attached, sdk: migrate telegram auth from legacy widget to OIDC #396
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
lidarbtc
wants to merge
10
commits into
develop
Choose a base branch
from
OKO-856
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 4 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
a5e0521
oko_api, key_share_node: add telegram OIDC backend support
lidarbtc c4735cc
oko_attached: replace telegram widget with OIDC popup flow
lidarbtc fbfa854
sdk: migrate telegram sign-in from widget modal to OIDC popup
lidarbtc c8f4e80
docs_web, sandbox_react_native: add telegram sign-in support
lidarbtc 628ba51
project: make telegram OIDC env vars optional for zero-downtime rollout
lidarbtc 01fbd52
Merge branch 'develop' into OKO-856
lidarbtc 961202e
project: fix telegram OIDC IPv6 connectivity and pairwise identity ma…
lidarbtc 43675dd
Merge remote-tracking branch 'origin/develop' into OKO-856
lidarbtc 2382f56
project: fix undici Agent connect type assertion for IPv4 enforcement
lidarbtc 80c47bd
project: use constant TELEGRAM_CLIENT_ID and reject tokens without id…
lidarbtc File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
87 changes: 87 additions & 0 deletions
87
backend/oko_api/server/src/middleware/auth/telegram_auth/validate_jwt.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,87 @@ | ||
| import type { Result } from "@oko-wallet/stdlib-js"; | ||
| import jwt, { type JwtHeader, type JwtPayload } from "jsonwebtoken"; | ||
|
|
||
| import type { TelegramUserInfo } from "./validate"; | ||
| import { | ||
| createJwksCache, | ||
| jwkToPem, | ||
| } from "@oko-wallet-api/middleware/auth/jwks_cache"; | ||
|
|
||
| const TELEGRAM_OIDC_ISSUER = "https://oauth.telegram.org"; | ||
| const TELEGRAM_JWKS_URL = "https://oauth.telegram.org/.well-known/jwks.json"; | ||
|
|
||
| const telegramJwksCache = createJwksCache(TELEGRAM_JWKS_URL, "Telegram"); | ||
|
|
||
| interface TelegramIdTokenPayload extends JwtPayload { | ||
| id?: number; | ||
| preferred_username?: string; | ||
| name?: string; | ||
| picture?: string; | ||
| } | ||
|
|
||
| export async function validateTelegramJwt( | ||
| idToken: string, | ||
| telegramClientId: string, | ||
| ): Promise<Result<TelegramUserInfo, string>> { | ||
| try { | ||
| const decoded = jwt.decode(idToken, { complete: true }); | ||
|
|
||
| if (!decoded || typeof decoded === "string") { | ||
| return { | ||
| success: false, | ||
| err: "Invalid token format", | ||
| }; | ||
| } | ||
|
|
||
| const header = decoded.header as JwtHeader; | ||
|
|
||
| if (!header.kid) { | ||
| return { | ||
| success: false, | ||
| err: "Missing key id in token header", | ||
| }; | ||
| } | ||
|
|
||
| const jwk = await telegramJwksCache.getSigningKey(header.kid); | ||
|
|
||
| if (!jwk) { | ||
| return { | ||
| success: false, | ||
| err: "Unable to find signing key for token", | ||
| }; | ||
| } | ||
|
|
||
| const pem = jwkToPem(jwk); | ||
|
|
||
| const payload = jwt.verify(idToken, pem, { | ||
| algorithms: ["RS256"], | ||
| issuer: TELEGRAM_OIDC_ISSUER, | ||
| audience: telegramClientId, | ||
| }) as TelegramIdTokenPayload; | ||
|
|
||
| const userId = | ||
| payload.sub ?? (payload.id != null ? String(payload.id) : undefined); | ||
|
|
||
| if (!userId) { | ||
| return { | ||
| success: false, | ||
| err: "Missing user ID (sub) in token", | ||
| }; | ||
| } | ||
|
|
||
| return { | ||
| success: true, | ||
| data: { | ||
| id: userId, | ||
| username: payload.preferred_username, | ||
| }, | ||
| }; | ||
| } catch (error) { | ||
| const message = | ||
| error instanceof Error ? error.message : "JWT validation failed"; | ||
| return { | ||
| success: false, | ||
| err: message, | ||
| }; | ||
| } | ||
| } |
143 changes: 143 additions & 0 deletions
143
backend/oko_api/server/src/routes/social_login_v1/get_telegram_token.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,143 @@ | ||
| import { registry } from "@oko-wallet/oko-api-openapi"; | ||
| import { ErrorResponseSchema } from "@oko-wallet/oko-api-openapi/common"; | ||
| import { | ||
| SocialLoginTelegramRequestSchema, | ||
| SocialLoginTelegramSuccessResponseSchema, | ||
| } from "@oko-wallet/oko-api-openapi/social_login"; | ||
| import type { OkoApiResponse } from "@oko-wallet/oko-types/api_response"; | ||
| import type { | ||
| SocialLoginTelegramBody, | ||
| SocialLoginTelegramResponse, | ||
| } from "@oko-wallet/oko-types/social_login"; | ||
| import type { Request, Response } from "express"; | ||
|
|
||
| const TELEGRAM_OIDC_TOKEN_URL = "https://oauth.telegram.org/token"; | ||
|
|
||
| registry.registerPath({ | ||
| method: "post", | ||
| path: "/social-login/v1/telegram/get-token", | ||
| tags: ["Social Login"], | ||
| summary: "Get Telegram OIDC ID token", | ||
| description: | ||
| "Exchange authorization code for a Telegram OIDC ID token using PKCE", | ||
| request: { | ||
| body: { | ||
| required: true, | ||
| content: { | ||
| "application/json": { | ||
| schema: SocialLoginTelegramRequestSchema, | ||
| }, | ||
| }, | ||
| }, | ||
| }, | ||
| responses: { | ||
| 200: { | ||
| description: "Successfully retrieved ID token", | ||
| content: { | ||
| "application/json": { | ||
| schema: SocialLoginTelegramSuccessResponseSchema, | ||
| }, | ||
| }, | ||
| }, | ||
| 400: { | ||
| description: "Invalid request", | ||
| content: { | ||
| "application/json": { | ||
| schema: ErrorResponseSchema, | ||
| }, | ||
| }, | ||
| }, | ||
| 500: { | ||
| description: "Server error", | ||
| content: { | ||
| "application/json": { | ||
| schema: ErrorResponseSchema, | ||
| }, | ||
| }, | ||
| }, | ||
| }, | ||
| }); | ||
|
|
||
| export async function getTelegramToken( | ||
| req: Request<any, any, SocialLoginTelegramBody>, | ||
| res: Response<OkoApiResponse<SocialLoginTelegramResponse>>, | ||
| ) { | ||
| const body = req.body; | ||
|
|
||
| if (!body.code || !body.code_verifier || !body.redirect_uri) { | ||
| res.status(400).json({ | ||
| success: false, | ||
| code: "INVALID_REQUEST", | ||
| msg: "Code, code_verifier, or redirect_uri is not set", | ||
| }); | ||
| return; | ||
| } | ||
|
|
||
| try { | ||
| const reqBody = new URLSearchParams({ | ||
| code: body.code, | ||
| grant_type: "authorization_code", | ||
| client_id: req.app.locals.telegram_client_id, | ||
| client_secret: req.app.locals.telegram_client_secret, | ||
| redirect_uri: body.redirect_uri, | ||
| code_verifier: body.code_verifier, | ||
| }); | ||
|
|
||
| const response = await fetch(TELEGRAM_OIDC_TOKEN_URL, { | ||
| method: "POST", | ||
| headers: { | ||
| "Content-Type": "application/x-www-form-urlencoded", | ||
| Accept: "application/json", | ||
| }, | ||
|
lidarbtc marked this conversation as resolved.
|
||
| body: reqBody, | ||
| }); | ||
|
|
||
| if (response.status === 200) { | ||
| const data: { | ||
| id_token?: string; | ||
| error?: string; | ||
| error_description?: string; | ||
| } = await response.json(); | ||
|
|
||
| if (data.error) { | ||
| res.status(400).json({ | ||
| success: false, | ||
| code: "UNKNOWN_ERROR", | ||
| msg: `${data.error}: ${data.error_description}`, | ||
| }); | ||
| return; | ||
| } | ||
|
|
||
| if (!data.id_token) { | ||
| res.status(400).json({ | ||
| success: false, | ||
| code: "UNKNOWN_ERROR", | ||
| msg: "Telegram OIDC response missing id_token", | ||
| }); | ||
| return; | ||
| } | ||
|
|
||
| res.status(200).json({ | ||
| success: true, | ||
| data: { | ||
| id_token: data.id_token, | ||
| }, | ||
| }); | ||
| return; | ||
| } | ||
|
|
||
| res.status(response.status).json({ | ||
| success: false, | ||
| code: "UNKNOWN_ERROR", | ||
| msg: await response.text(), | ||
| }); | ||
| } catch (err: unknown) { | ||
| const message = | ||
| err instanceof Error ? err.message : "Failed to exchange Telegram token"; | ||
| res.status(500).json({ | ||
| success: false, | ||
| code: "UNKNOWN_ERROR", | ||
| msg: message, | ||
| }); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.