Umstellung auf nodeclassic

This commit is contained in:
2024-09-04 14:56:24 +02:00
parent c5577969c8
commit d8429f9b4a
47 changed files with 330 additions and 508 deletions

View File

@@ -1,6 +1,6 @@
import { Controller, Get, Param, UseGuards } from '@nestjs/common';
import { AdminAuthGuard } from '../jwt-auth/admin-auth.guard.js';
import { AuthService } from './auth.service.js';
import { Controller, Get, UseGuards } from '@nestjs/common';
import { AdminAuthGuard } from '../jwt-auth/admin-auth.guard';
import { AuthService } from './auth.service';
@Controller('auth')
export class AuthController {
@@ -18,15 +18,15 @@ export class AuthController {
return this.authService.getUsers();
}
@UseGuards(AdminAuthGuard)
@Get('user/:userid')
getUser(@Param('userid') userId: string): any {
return this.authService.getUser(userId);
}
// @UseGuards(AdminAuthGuard)
// @Get('user/:userid')
// getUser(@Param('userid') userId: string): any {
// return this.authService.getUser(userId);
// }
@UseGuards(AdminAuthGuard)
@Get('user/:userid/lastlogin') //e0811669-c7eb-4e5e-a699-e8334d5c5b01 -> aknuth
getLastLogin(@Param('userid') userId: string): any {
return this.authService.getLastLogin(userId);
}
// @UseGuards(AdminAuthGuard)
// @Get('user/:userid/lastlogin') //e0811669-c7eb-4e5e-a699-e8334d5c5b01 -> aknuth
// getLastLogin(@Param('userid') userId: string): any {
// return this.authService.getLastLogin(userId);
// }
}

View File

@@ -1,12 +1,11 @@
import { Module } from '@nestjs/common';
import { PassportModule } from '@nestjs/passport';
import { JwtStrategy } from '../jwt.strategy.js';
import { AuthController } from './auth.controller.js';
import { AuthService } from './auth.service.js';
import { AuthController } from './auth.controller';
import { AuthService } from './auth.service';
@Module({
imports: [PassportModule],
providers: [AuthService, JwtStrategy],
providers: [AuthService],
controllers: [AuthController],
exports: [AuthService],
})

View File

@@ -1,7 +1,5 @@
import { Injectable } from '@nestjs/common';
import ky from 'ky';
import { KeycloakUser } from 'src/models/main.model';
import urlcat from 'urlcat';
@Injectable()
export class AuthService {
@@ -13,16 +11,19 @@ export class AuthService {
params.append('password', process.env.KEYCLOAK_ADMIN_PASSWORD);
const URL = `${process.env.KEYCLOAK_HOST}${process.env.KEYCLOAK_TOKEN_URL}`;
const response = await ky
.post(URL, {
body: params.toString(),
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
Authorization: process.env.KEYCLOAK_ADMIN_TOKEN,
},
})
.json();
return (<any>response).access_token;
const response = await fetch(URL, {
method: 'POST',
body: params.toString(),
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
Authorization: process.env.KEYCLOAK_ADMIN_TOKEN,
},
});
if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`);
}
const data = await response.json();
return (<any>data).access_token;
} catch (error) {
if (error.name === 'HTTPError') {
const errorJson = await error.response.json();
@@ -36,43 +37,46 @@ export class AuthService {
public async getUsers(): Promise<KeycloakUser[]> {
const token = await this.getAccessToken();
const URL = `${process.env.KEYCLOAK_HOST}${process.env.KEYCLOAK_ADMIN_REALM}${process.env.REALM}${process.env.KEYCLOAK_USERS_URL}`;
const response = await ky
.get(URL, {
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
Authorization: `Bearer ${token}`,
},
})
.json();
return response as KeycloakUser[];
}
public async getUser(userid: string) {
const token = await this.getAccessToken();
const URLPATH = `${process.env.KEYCLOAK_ADMIN_REALM}${process.env.REALM}${process.env.KEYCLOAK_USER_URL}`;
const URL = urlcat(process.env.KEYCLOAK_HOST, URLPATH, { userid });
const response = await ky
.get(URL, {
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
Authorization: `Bearer ${token}`,
},
})
.json();
return response;
const response = await fetch(URL, {
method: 'GET',
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
Authorization: `Bearer ${token}`,
},
});
if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`);
}
const data = await response.json();
return data as KeycloakUser[];
}
// public async getUser(userid: string) {
// const token = await this.getAccessToken();
// const URLPATH = `${process.env.KEYCLOAK_ADMIN_REALM}${process.env.REALM}${process.env.KEYCLOAK_USER_URL}`;
// const URL = urlcat(process.env.KEYCLOAK_HOST, URLPATH, { userid });
// const response = await ky
// .get(URL, {
// headers: {
// 'Content-Type': 'application/x-www-form-urlencoded',
// Authorization: `Bearer ${token}`,
// },
// })
// .json();
// return response;
// }
public async getLastLogin(userid: string) {
const token = await this.getAccessToken();
const URLPATH = `${process.env.KEYCLOAK_ADMIN_REALM}${process.env.REALM}${process.env.KEYCLOAK_LASTLOGIN_URL}`;
const URL = urlcat(process.env.KEYCLOAK_HOST, URLPATH, { userid });
const response = await ky
.get(URL, {
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
Authorization: `Bearer ${token}`,
},
})
.json();
return response;
}
// public async getLastLogin(userid: string) {
// const token = await this.getAccessToken();
// const URLPATH = `${process.env.KEYCLOAK_ADMIN_REALM}${process.env.REALM}${process.env.KEYCLOAK_LASTLOGIN_URL}`;
// const URL = urlcat(process.env.KEYCLOAK_HOST, URLPATH, { userid });
// const response = await ky
// .get(URL, {
// headers: {
// 'Content-Type': 'application/x-www-form-urlencoded',
// Authorization: `Bearer ${token}`,
// },
// })
// .json();
// return response;
// }
}