import React, { useEffect, useState } from 'react'; import { useLocation, useNavigate } from 'react-router-dom'; import { Card, Button, Space, Typography, Alert, Row, Col, Tag, Spin, Result, Input, } from 'antd'; import { CheckCircleOutlined, ExclamationCircleOutlined, CopyOutlined, ArrowLeftOutlined, } from '@ant-design/icons'; import { apiService } from '../services/apiService'; const { Title, Text } = Typography; const { TextArea } = Input; interface CallbackData { token?: string; state?: string; error?: string; error_description?: string; } interface VerificationResult { valid: boolean; permitted: boolean; user_id?: string; permissions: string[]; permission_results?: Record; expires_at?: string; max_valid_at?: string; token_type: string; claims?: Record; error?: string; } const TokenTesterCallback: React.FC = () => { const location = useLocation(); const navigate = useNavigate(); const [loading, setLoading] = useState(true); const [callbackData, setCallbackData] = useState({}); const [verificationResult, setVerificationResult] = useState(null); const [verificationError, setVerificationError] = useState(null); useEffect(() => { parseCallbackData(); }, [location]); const parseCallbackData = async () => { try { setLoading(true); // Parse URL parameters const urlParams = new URLSearchParams(location.search); let token = urlParams.get('token') || undefined; // If no token in URL, try to extract from auth_token cookie if (!token) { token = getCookie('auth_token') || undefined; } const data: CallbackData = { token: token, state: urlParams.get('state') || undefined, error: urlParams.get('error') || undefined, error_description: urlParams.get('error_description') || undefined, }; setCallbackData(data); // If we have a token, try to verify it if (data.token && !data.error) { await verifyToken(data.token); } } catch (error) { console.error('Error parsing callback data:', error); setVerificationError('Failed to parse callback data'); } finally { setLoading(false); } }; // Utility function to get cookie value by name const getCookie = (name: string): string | null => { const value = `; ${document.cookie}`; const parts = value.split(`; ${name}=`); if (parts.length === 2) { const cookieValue = parts.pop()?.split(';').shift(); return cookieValue || null; } return null; }; const verifyToken = async (token: string) => { try { // We need to extract app_id from the state or make a best guess // For now, we'll try to verify without specifying app_id // In a real implementation, the app_id should be included in the state parameter // Try to get app_id from localStorage if it was stored during the test const testData = localStorage.getItem('token_tester_data'); let appId = ''; if (testData) { try { const parsed = JSON.parse(testData); appId = parsed.app_id || ''; } catch (e) { console.warn('Could not parse stored test data'); } } if (!appId) { // If we don't have app_id, we can't verify the token properly setVerificationError('Cannot verify token: Application ID not found in callback state'); return; } const verifyRequest = { app_id: appId, token: token, permissions: [], // We'll verify without specific permissions }; const result = await apiService.verifyToken(verifyRequest); setVerificationResult(result); } catch (error: any) { console.error('Token verification failed:', error); setVerificationError( error.response?.data?.message || error.message || 'Token verification failed' ); } }; const copyToClipboard = (text: string) => { navigator.clipboard.writeText(text); }; const goBackToTester = () => { navigate('/token-tester'); }; if (loading) { return (
Processing callback...
); } return (
Token Tester - Callback Callback page for testing the login flow
{/* Callback Status */} {callbackData.error ? ( ) : callbackData.token ? ( ) : ( )} {/* Token Information */} {callbackData.token && (
Token: