fix qr auth
This commit is contained in:
@@ -51,8 +51,9 @@ export class AdvancedAuthController {
|
|||||||
@ApiOperation({ summary: 'Проверить QR-сессию', description: 'Возвращает текущий статус QR-сессии: PENDING/APPROVED/EXPIRED.' })
|
@ApiOperation({ summary: 'Проверить QR-сессию', description: 'Возвращает текущий статус QR-сессии: PENDING/APPROVED/EXPIRED.' })
|
||||||
@ApiParam({ name: 'sessionId', description: 'ID QR-сессии' })
|
@ApiParam({ name: 'sessionId', description: 'ID QR-сессии' })
|
||||||
@ApiResponse({ status: 200, description: 'Статус QR-сессии получен' })
|
@ApiResponse({ status: 200, description: 'Статус QR-сессии получен' })
|
||||||
pollQr(@Param('sessionId') sessionId: string) {
|
async pollQr(@Param('sessionId') sessionId: string) {
|
||||||
return this.core.advancedAuth.PollQrSession({ sessionId });
|
const response = await firstValueFrom(this.core.advancedAuth.PollQrSession({ sessionId }));
|
||||||
|
return this.normalizeQrResponse(response);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Post('qr/session/:sessionId/claim')
|
@Post('qr/session/:sessionId/claim')
|
||||||
@@ -86,6 +87,54 @@ export class AdvancedAuthController {
|
|||||||
@ApiResponse({ status: 201, description: 'QR-сессия подтверждена' })
|
@ApiResponse({ status: 201, description: 'QR-сессия подтверждена' })
|
||||||
async approveQr(@Param('sessionId') sessionId: string, @Headers('authorization') authorization?: string) {
|
async approveQr(@Param('sessionId') sessionId: string, @Headers('authorization') authorization?: string) {
|
||||||
const payload = await resolveAuthorizedPayload(this.jwt, this.core, authorization);
|
const payload = await resolveAuthorizedPayload(this.jwt, this.core, authorization);
|
||||||
return this.core.advancedAuth.ApproveQrSession({ sessionId, userId: payload.sub });
|
const response = await firstValueFrom(this.core.advancedAuth.ApproveQrSession({ sessionId, userId: payload.sub }));
|
||||||
|
return this.normalizeQrResponse(response);
|
||||||
|
}
|
||||||
|
|
||||||
|
private normalizeQrResponse(response: unknown) {
|
||||||
|
const value = response as {
|
||||||
|
sessionId?: string;
|
||||||
|
status?: string;
|
||||||
|
expiresAt?: string;
|
||||||
|
qrPayload?: string;
|
||||||
|
claimed?: boolean;
|
||||||
|
claimedDeviceName?: string;
|
||||||
|
accessToken?: string;
|
||||||
|
refreshToken?: string;
|
||||||
|
authSessionId?: string;
|
||||||
|
pinVerified?: boolean;
|
||||||
|
auth?: {
|
||||||
|
accessToken?: string;
|
||||||
|
refreshToken?: string;
|
||||||
|
sessionId?: string;
|
||||||
|
pinVerified?: boolean;
|
||||||
|
expiresAt?: string;
|
||||||
|
user?: unknown;
|
||||||
|
};
|
||||||
|
};
|
||||||
|
const auth = value.auth;
|
||||||
|
|
||||||
|
return {
|
||||||
|
sessionId: value.sessionId,
|
||||||
|
status: value.status,
|
||||||
|
expiresAt: value.expiresAt,
|
||||||
|
qrPayload: value.qrPayload,
|
||||||
|
claimed: value.claimed,
|
||||||
|
claimedDeviceName: value.claimedDeviceName,
|
||||||
|
accessToken: value.accessToken ?? auth?.accessToken,
|
||||||
|
refreshToken: value.refreshToken ?? auth?.refreshToken,
|
||||||
|
authSessionId: value.authSessionId ?? auth?.sessionId,
|
||||||
|
pinVerified: value.pinVerified ?? auth?.pinVerified,
|
||||||
|
auth: auth
|
||||||
|
? {
|
||||||
|
accessToken: auth.accessToken,
|
||||||
|
refreshToken: auth.refreshToken,
|
||||||
|
sessionId: auth.sessionId,
|
||||||
|
pinVerified: auth.pinVerified,
|
||||||
|
expiresAt: auth.expiresAt,
|
||||||
|
user: auth.user
|
||||||
|
}
|
||||||
|
: undefined
|
||||||
|
};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -33,7 +33,7 @@ import { PhoneInput, phoneCountries, toE164 } from '@/components/ui/phone-input'
|
|||||||
import { Tabs, TabsContent, TabsList, TabsTrigger } from '@/components/ui/tabs';
|
import { Tabs, TabsContent, TabsList, TabsTrigger } from '@/components/ui/tabs';
|
||||||
|
|
||||||
import type { AuthTokens, LoginMethod, OtpChannel, OtpSendResponse } from '@/lib/api';
|
import type { AuthTokens, LoginMethod, OtpChannel, OtpSendResponse } from '@/lib/api';
|
||||||
import { AUTH_TOKEN_KEY, claimQrLoginSession, createQrLoginSession, fetchCurrentUser, fetchFedcmSessionStatus, pollQrLoginSession } from '@/lib/api';
|
import { AUTH_TOKEN_KEY, claimQrLoginSession, createQrLoginSession, fetchFedcmSessionStatus, pollQrLoginSession } from '@/lib/api';
|
||||||
|
|
||||||
import { isFedcmLoginPopup, finalizeFedcmLogin } from '@/lib/fedcm-login-bridge';
|
import { isFedcmLoginPopup, finalizeFedcmLogin } from '@/lib/fedcm-login-bridge';
|
||||||
|
|
||||||
@@ -224,8 +224,7 @@ function LoginPageContent() {
|
|||||||
function finishQrAuth(auth: Omit<AuthTokens, 'user'> & { user?: AuthTokens['user'] }) {
|
function finishQrAuth(auth: Omit<AuthTokens, 'user'> & { user?: AuthTokens['user'] }) {
|
||||||
void (async () => {
|
void (async () => {
|
||||||
try {
|
try {
|
||||||
const user = auth.user ?? await fetchCurrentUser(auth.accessToken);
|
const resolved = await applyLoginAuth(auth);
|
||||||
const resolved = await applyLoginAuth({ ...auth, user });
|
|
||||||
finishAuth(resolved.pinVerified, resolved.sessionId);
|
finishAuth(resolved.pinVerified, resolved.sessionId);
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
qrAuthStartedRef.current = false;
|
qrAuthStartedRef.current = false;
|
||||||
@@ -410,22 +409,25 @@ function LoginPageContent() {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (session.status === 'APPROVED' && session.auth && !qrAuthStartedRef.current) {
|
const qrAuth = session.auth;
|
||||||
|
const accessToken = session.accessToken ?? qrAuth?.accessToken;
|
||||||
|
|
||||||
|
if (session.status === 'APPROVED' && accessToken && !qrAuthStartedRef.current) {
|
||||||
qrAuthStartedRef.current = true;
|
qrAuthStartedRef.current = true;
|
||||||
|
|
||||||
finishQrAuth({
|
finishQrAuth({
|
||||||
|
|
||||||
accessToken: session.auth.accessToken,
|
accessToken,
|
||||||
|
|
||||||
refreshToken: session.auth.refreshToken,
|
refreshToken: session.refreshToken ?? qrAuth?.refreshToken ?? '',
|
||||||
|
|
||||||
sessionId: session.auth.sessionId,
|
sessionId: session.authSessionId ?? qrAuth?.sessionId ?? session.sessionId,
|
||||||
|
|
||||||
pinVerified: session.auth.pinVerified,
|
pinVerified: session.pinVerified ?? qrAuth?.pinVerified ?? false,
|
||||||
|
|
||||||
expiresAt: session.auth.expiresAt,
|
expiresAt: qrAuth?.expiresAt ?? session.expiresAt,
|
||||||
|
|
||||||
user: session.auth.user
|
user: qrAuth?.user
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
@@ -65,7 +65,7 @@ interface AuthContextValue {
|
|||||||
beginTotpLogin: (recipient: string) => Promise<string>;
|
beginTotpLogin: (recipient: string) => Promise<string>;
|
||||||
verifyTotpLogin: (totpChallengeToken: string, code: string) => Promise<AuthTokens>;
|
verifyTotpLogin: (totpChallengeToken: string, code: string) => Promise<AuthTokens>;
|
||||||
completePin: (sessionId: string, pin: string) => Promise<void>;
|
completePin: (sessionId: string, pin: string) => Promise<void>;
|
||||||
applyLoginAuth: (auth: AuthTokens) => Promise<AuthTokens>;
|
applyLoginAuth: (auth: Omit<AuthTokens, 'user'> & { user?: PublicUser }) => Promise<Omit<AuthTokens, 'user'> & { user?: PublicUser }>;
|
||||||
applyUserPatch: (patch: Partial<PublicUser>) => void;
|
applyUserPatch: (patch: Partial<PublicUser>) => void;
|
||||||
register: (data: { displayName: string; login: string; password: string }) => Promise<void>;
|
register: (data: { displayName: string; login: string; password: string }) => Promise<void>;
|
||||||
refreshProfile: () => Promise<void>;
|
refreshProfile: () => Promise<void>;
|
||||||
@@ -88,10 +88,12 @@ function readCachedUserProfile(): PublicUser | null {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function persistPartialAuth(auth: AuthTokens) {
|
function persistPartialAuth(auth: Omit<AuthTokens, 'user'> & { user?: PublicUser }) {
|
||||||
window.localStorage.setItem(AUTH_REFRESH_KEY, auth.refreshToken);
|
window.localStorage.setItem(AUTH_REFRESH_KEY, auth.refreshToken);
|
||||||
window.localStorage.setItem(AUTH_SESSION_KEY, auth.sessionId);
|
window.localStorage.setItem(AUTH_SESSION_KEY, auth.sessionId);
|
||||||
cacheUserProfile(auth.user);
|
if (auth.user) {
|
||||||
|
cacheUserProfile(auth.user);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function applySessionState(
|
function applySessionState(
|
||||||
@@ -625,13 +627,20 @@ export function AuthProvider({ children }: { children: React.ReactNode }) {
|
|||||||
);
|
);
|
||||||
|
|
||||||
const applyLoginAuth = React.useCallback(
|
const applyLoginAuth = React.useCallback(
|
||||||
async (auth: AuthTokens) => {
|
async (auth: Omit<AuthTokens, 'user'> & { user?: PublicUser }) => {
|
||||||
if (auth.pinVerified) {
|
if (auth.pinVerified) {
|
||||||
await establishSession(auth);
|
if (!auth.user) {
|
||||||
|
const user = await fetchAuthSession(auth.accessToken).then((session) => session.user);
|
||||||
|
await establishSession({ ...auth, user });
|
||||||
|
} else {
|
||||||
|
await establishSession(auth);
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
persistPartialAuth(auth);
|
persistPartialAuth(auth);
|
||||||
|
if (auth.user) {
|
||||||
|
setUser(auth.user);
|
||||||
|
}
|
||||||
setHasStoredSession(true);
|
setHasStoredSession(true);
|
||||||
activatePinLock(auth.sessionId);
|
|
||||||
}
|
}
|
||||||
return auth;
|
return auth;
|
||||||
},
|
},
|
||||||
|
|||||||
@@ -238,6 +238,10 @@ export interface QrLoginSession {
|
|||||||
qrPayload?: string;
|
qrPayload?: string;
|
||||||
claimed?: boolean;
|
claimed?: boolean;
|
||||||
claimedDeviceName?: string;
|
claimedDeviceName?: string;
|
||||||
|
accessToken?: string;
|
||||||
|
refreshToken?: string;
|
||||||
|
authSessionId?: string;
|
||||||
|
pinVerified?: boolean;
|
||||||
auth?: Omit<AuthTokens, 'user'> & { user?: PublicUser };
|
auth?: Omit<AuthTokens, 'user'> & { user?: PublicUser };
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -201,6 +201,8 @@ export class AdvancedAuthService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private buildQrSessionResponse(sessionId: string, data: StoredQrSession, qrPayload?: string) {
|
private buildQrSessionResponse(sessionId: string, data: StoredQrSession, qrPayload?: string) {
|
||||||
|
const auth = data.auth
|
||||||
|
|
||||||
return {
|
return {
|
||||||
sessionId,
|
sessionId,
|
||||||
status: data.status,
|
status: data.status,
|
||||||
@@ -208,16 +210,20 @@ export class AdvancedAuthService {
|
|||||||
qrPayload,
|
qrPayload,
|
||||||
claimed: Boolean(data.claimed && data.fingerprint),
|
claimed: Boolean(data.claimed && data.fingerprint),
|
||||||
claimedDeviceName: data.claimed && data.deviceName ? data.deviceName : undefined,
|
claimedDeviceName: data.claimed && data.deviceName ? data.deviceName : undefined,
|
||||||
auth: data.auth
|
accessToken: auth?.accessToken,
|
||||||
|
refreshToken: auth?.refreshToken,
|
||||||
|
authSessionId: auth?.sessionId,
|
||||||
|
pinVerified: auth?.pinVerified,
|
||||||
|
auth: auth
|
||||||
? {
|
? {
|
||||||
accessToken: data.auth.accessToken,
|
accessToken: auth.accessToken,
|
||||||
refreshToken: data.auth.refreshToken,
|
refreshToken: auth.refreshToken,
|
||||||
sessionId: data.auth.sessionId,
|
sessionId: auth.sessionId,
|
||||||
pinVerified: data.auth.pinVerified,
|
pinVerified: auth.pinVerified,
|
||||||
expiresAt: data.auth.expiresAt,
|
expiresAt: auth.expiresAt,
|
||||||
requiresTotp: data.auth.requiresTotp,
|
requiresTotp: auth.requiresTotp,
|
||||||
totpChallengeToken: data.auth.totpChallengeToken,
|
totpChallengeToken: auth.totpChallengeToken,
|
||||||
user: data.auth.user
|
user: auth.user
|
||||||
}
|
}
|
||||||
: undefined
|
: undefined
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -249,6 +249,10 @@ message QrSessionResponse {
|
|||||||
optional QrAuthPayload auth = 5;
|
optional QrAuthPayload auth = 5;
|
||||||
optional bool claimed = 6;
|
optional bool claimed = 6;
|
||||||
optional string claimedDeviceName = 7;
|
optional string claimedDeviceName = 7;
|
||||||
|
optional string accessToken = 8;
|
||||||
|
optional string refreshToken = 9;
|
||||||
|
optional string authSessionId = 10;
|
||||||
|
optional bool pinVerified = 11;
|
||||||
}
|
}
|
||||||
|
|
||||||
message CreateDeviceLinkSessionRequest {
|
message CreateDeviceLinkSessionRequest {
|
||||||
|
|||||||
Reference in New Issue
Block a user