fix qr auth
This commit is contained in:
@@ -51,8 +51,9 @@ export class AdvancedAuthController {
|
||||
@ApiOperation({ summary: 'Проверить QR-сессию', description: 'Возвращает текущий статус QR-сессии: PENDING/APPROVED/EXPIRED.' })
|
||||
@ApiParam({ name: 'sessionId', description: 'ID QR-сессии' })
|
||||
@ApiResponse({ status: 200, description: 'Статус QR-сессии получен' })
|
||||
pollQr(@Param('sessionId') sessionId: string) {
|
||||
return this.core.advancedAuth.PollQrSession({ sessionId });
|
||||
async pollQr(@Param('sessionId') sessionId: string) {
|
||||
const response = await firstValueFrom(this.core.advancedAuth.PollQrSession({ sessionId }));
|
||||
return this.normalizeQrResponse(response);
|
||||
}
|
||||
|
||||
@Post('qr/session/:sessionId/claim')
|
||||
@@ -86,6 +87,54 @@ export class AdvancedAuthController {
|
||||
@ApiResponse({ status: 201, description: 'QR-сессия подтверждена' })
|
||||
async approveQr(@Param('sessionId') sessionId: string, @Headers('authorization') authorization?: string) {
|
||||
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 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';
|
||||
|
||||
@@ -224,8 +224,7 @@ function LoginPageContent() {
|
||||
function finishQrAuth(auth: Omit<AuthTokens, 'user'> & { user?: AuthTokens['user'] }) {
|
||||
void (async () => {
|
||||
try {
|
||||
const user = auth.user ?? await fetchCurrentUser(auth.accessToken);
|
||||
const resolved = await applyLoginAuth({ ...auth, user });
|
||||
const resolved = await applyLoginAuth(auth);
|
||||
finishAuth(resolved.pinVerified, resolved.sessionId);
|
||||
} catch (error) {
|
||||
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;
|
||||
|
||||
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>;
|
||||
verifyTotpLogin: (totpChallengeToken: string, code: string) => Promise<AuthTokens>;
|
||||
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;
|
||||
register: (data: { displayName: string; login: string; password: string }) => 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_SESSION_KEY, auth.sessionId);
|
||||
if (auth.user) {
|
||||
cacheUserProfile(auth.user);
|
||||
}
|
||||
}
|
||||
|
||||
function applySessionState(
|
||||
@@ -625,13 +627,20 @@ export function AuthProvider({ children }: { children: React.ReactNode }) {
|
||||
);
|
||||
|
||||
const applyLoginAuth = React.useCallback(
|
||||
async (auth: AuthTokens) => {
|
||||
async (auth: Omit<AuthTokens, 'user'> & { user?: PublicUser }) => {
|
||||
if (auth.pinVerified) {
|
||||
if (!auth.user) {
|
||||
const user = await fetchAuthSession(auth.accessToken).then((session) => session.user);
|
||||
await establishSession({ ...auth, user });
|
||||
} else {
|
||||
await establishSession(auth);
|
||||
}
|
||||
} else {
|
||||
persistPartialAuth(auth);
|
||||
if (auth.user) {
|
||||
setUser(auth.user);
|
||||
}
|
||||
setHasStoredSession(true);
|
||||
activatePinLock(auth.sessionId);
|
||||
}
|
||||
return auth;
|
||||
},
|
||||
|
||||
@@ -238,6 +238,10 @@ export interface QrLoginSession {
|
||||
qrPayload?: string;
|
||||
claimed?: boolean;
|
||||
claimedDeviceName?: string;
|
||||
accessToken?: string;
|
||||
refreshToken?: string;
|
||||
authSessionId?: string;
|
||||
pinVerified?: boolean;
|
||||
auth?: Omit<AuthTokens, 'user'> & { user?: PublicUser };
|
||||
}
|
||||
|
||||
|
||||
@@ -201,6 +201,8 @@ export class AdvancedAuthService {
|
||||
}
|
||||
|
||||
private buildQrSessionResponse(sessionId: string, data: StoredQrSession, qrPayload?: string) {
|
||||
const auth = data.auth
|
||||
|
||||
return {
|
||||
sessionId,
|
||||
status: data.status,
|
||||
@@ -208,16 +210,20 @@ export class AdvancedAuthService {
|
||||
qrPayload,
|
||||
claimed: Boolean(data.claimed && data.fingerprint),
|
||||
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,
|
||||
refreshToken: data.auth.refreshToken,
|
||||
sessionId: data.auth.sessionId,
|
||||
pinVerified: data.auth.pinVerified,
|
||||
expiresAt: data.auth.expiresAt,
|
||||
requiresTotp: data.auth.requiresTotp,
|
||||
totpChallengeToken: data.auth.totpChallengeToken,
|
||||
user: data.auth.user
|
||||
accessToken: auth.accessToken,
|
||||
refreshToken: auth.refreshToken,
|
||||
sessionId: auth.sessionId,
|
||||
pinVerified: auth.pinVerified,
|
||||
expiresAt: auth.expiresAt,
|
||||
requiresTotp: auth.requiresTotp,
|
||||
totpChallengeToken: auth.totpChallengeToken,
|
||||
user: auth.user
|
||||
}
|
||||
: undefined
|
||||
};
|
||||
|
||||
@@ -249,6 +249,10 @@ message QrSessionResponse {
|
||||
optional QrAuthPayload auth = 5;
|
||||
optional bool claimed = 6;
|
||||
optional string claimedDeviceName = 7;
|
||||
optional string accessToken = 8;
|
||||
optional string refreshToken = 9;
|
||||
optional string authSessionId = 10;
|
||||
optional bool pinVerified = 11;
|
||||
}
|
||||
|
||||
message CreateDeviceLinkSessionRequest {
|
||||
|
||||
Reference in New Issue
Block a user