mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-12-01 08:53:13 +00:00
Give the admin OAuth-session panel back to the oauth domain
The last of AdminService's borrowed SQL. oauth_tokens belongs to oauth/, which already had a user-facing view of the same table; AdminService carried a second one only because the panel route is under /api/admin. Both survive, under names that say which is which. They are not duplicates: the user-facing listOAuthSessions is scoped to one user, the admin one spans all of them and joins the owner username, and the admin revoke kills the client session for whoever owned the token. OauthModule gains its first export. Its header said nothing inside the container injects the domain, which stops being true here.
This commit is contained in:
@@ -4,6 +4,7 @@ import { readEnv } from '../../app-config';
|
||||
import { AdminService } from './admin.service';
|
||||
import { TokenService } from '../tokens/token.service';
|
||||
import { RegistrationInvitesService } from '../auth/registration-invites.service';
|
||||
import { OauthService } from '../oauth/oauth.service';
|
||||
import {
|
||||
AdminUserCreateDto,
|
||||
AdminUserUpdateDto,
|
||||
@@ -65,6 +66,7 @@ export class AdminController {
|
||||
private readonly notifications: NotificationsService,
|
||||
private readonly tokens: TokenService,
|
||||
private readonly invites: RegistrationInvitesService,
|
||||
private readonly oauth: OauthService,
|
||||
) {}
|
||||
|
||||
// ── Users ──
|
||||
@@ -257,11 +259,11 @@ export class AdminController {
|
||||
}
|
||||
|
||||
@Get('oauth-sessions')
|
||||
listOAuthSessions() { return { sessions: this.admin.listOAuthSessions() }; }
|
||||
listOAuthSessions() { return { sessions: this.oauth.listAllOAuthSessions() }; }
|
||||
|
||||
@Delete('oauth-sessions/:id')
|
||||
revokeOAuthSession(@CurrentUser() user: User, @Param('id') id: string, @Req() req: Request) {
|
||||
ok(this.admin.revokeOAuthSession(id));
|
||||
ok(this.oauth.adminRevokeOAuthSession(id));
|
||||
this.audit.writeAudit({ userId: user.id, action: 'admin.oauth_session_revoke', resource: String(id), ip: getClientIp(req) });
|
||||
return { success: true };
|
||||
}
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
import { Module } from '@nestjs/common';
|
||||
import { TokensModule } from '../tokens/tokens.module';
|
||||
import { OauthModule } from '../oauth/oauth.module';
|
||||
import { AdminController } from './admin.controller';
|
||||
import { AdminService } from './admin.service';
|
||||
import { PluginsRuntimeModule } from '../plugins/plugins-runtime.module';
|
||||
@@ -19,7 +20,7 @@ import { PackingModule } from '../packing/packing.module';
|
||||
import { PermissionsModule } from '../permissions/permissions.module';
|
||||
|
||||
@Module({
|
||||
imports: [PluginsRuntimeModule, SettingsModule, AuditModule, AddonsModule, AuthModule, NotificationsModule, PackingModule, PermissionsModule, TokensModule],
|
||||
imports: [PluginsRuntimeModule, SettingsModule, AuditModule, AddonsModule, AuthModule, NotificationsModule, PackingModule, PermissionsModule, TokensModule, OauthModule],
|
||||
controllers: [AdminController],
|
||||
providers: [AdminService],
|
||||
})
|
||||
|
||||
@@ -602,41 +602,6 @@ export class AdminService {
|
||||
};
|
||||
}
|
||||
|
||||
// ── OAuth Sessions ─────────────────────────────────────────────────────────
|
||||
|
||||
listOAuthSessions() {
|
||||
const rows = this.db.all<Record<string, unknown> & { scopes: string }>(`
|
||||
SELECT ot.id, ot.client_id, oc.name AS client_name, ot.user_id, u.username,
|
||||
ot.scopes, ot.access_token_expires_at, ot.refresh_token_expires_at, ot.created_at
|
||||
FROM oauth_tokens ot
|
||||
JOIN oauth_clients oc ON ot.client_id = oc.client_id
|
||||
JOIN users u ON u.id = ot.user_id
|
||||
WHERE ot.revoked_at IS NULL
|
||||
AND ot.refresh_token_expires_at > CURRENT_TIMESTAMP
|
||||
ORDER BY ot.created_at DESC
|
||||
`);
|
||||
// One malformed row must not 500 the whole admin OAuth-sessions panel.
|
||||
return rows.map((r) => {
|
||||
let scopes: unknown;
|
||||
try {
|
||||
scopes = JSON.parse(r.scopes);
|
||||
} catch {
|
||||
scopes = null;
|
||||
}
|
||||
return { ...r, scopes };
|
||||
});
|
||||
}
|
||||
|
||||
revokeOAuthSession(id: string) {
|
||||
const row = this.db.get<{ id: number; user_id: number; client_id: string }>(
|
||||
'SELECT id, user_id, client_id FROM oauth_tokens WHERE id = ?', id,
|
||||
);
|
||||
if (!row) return { error: 'Session not found', status: 404 };
|
||||
this.db.run('UPDATE oauth_tokens SET revoked_at = CURRENT_TIMESTAMP WHERE id = ?', id);
|
||||
revokeUserSessionsForClient(row.user_id, row.client_id);
|
||||
return {};
|
||||
}
|
||||
|
||||
// ── JWT Rotation ───────────────────────────────────────────────────────────
|
||||
|
||||
rotateJwtSecret(): { error?: string; status?: number } {
|
||||
|
||||
Reference in New Issue
Block a user