Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: saving logbook token in memory #725

Merged
50 changes: 42 additions & 8 deletions src/logbooks/logbooks.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ export class LogbooksService {
private baseUrl;
private username;
private password;
private accessToken: string | null;

constructor(
private readonly configService: ConfigService,
Expand All @@ -27,13 +28,17 @@ export class LogbooksService {
this.password = this.configService.get<string>("logbook.password");
}

async login(username: string, password: string): Promise<{ token: string }> {
async login(
username: string,
password: string,
): Promise<{ token: string | null }> {
const credentials = { username, password };
try {
const res = await firstValueFrom(
this.httpService.post(this.baseUrl + "/Users/login", credentials),
);
return res.data;
this.accessToken = res.data.token;
return { token: this.accessToken };
} catch (error) {
handleAxiosRequestError(error, "LogbooksService.login");
throw new InternalServerErrorException("Logbook login failed");
Expand All @@ -44,11 +49,15 @@ export class LogbooksService {
if (this.logbookEnabled) {
if (this.username && this.password) {
try {
const accessToken = await this.login(this.username, this.password);
await this.checkTokenStatus();
if (!this.accessToken) {
await this.login(this.username, this.password);
}

Logger.log("Fetching Logbooks", "LogbooksService.findAll");
const res = await firstValueFrom(
this.httpService.get<Logbook[]>(this.baseUrl + "/Logbooks", {
headers: { Authorization: `Bearer ${accessToken.token}` },
headers: { Authorization: `Bearer ${this.accessToken}` },
}),
);
const nonEmptyLogbooks = res.data.filter(
Expand Down Expand Up @@ -84,13 +93,16 @@ export class LogbooksService {
if (this.logbookEnabled) {
if (this.username && this.password) {
try {
const accessToken = await this.login(this.username, this.password);
await this.checkTokenStatus();
if (!this.accessToken) {
await this.login(this.username, this.password);
}
Logger.log("Fetching logbook " + name, "LogbooksService.findByName");
Logger.log(filters, "LogbooksService.findByName");
const res = await firstValueFrom(
this.httpService.get<Logbook>(
this.baseUrl + `/Logbooks/${name}?filter=${filters}`,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same as my previous comment

{ headers: { Authorization: `Bearer ${accessToken.token}` } },
{ headers: { Authorization: `Bearer ${this.accessToken}` } },
),
);
Logger.log("Found logbook " + name, "LogbooksService.findByName");
Expand Down Expand Up @@ -132,7 +144,10 @@ export class LogbooksService {
if (this.logbookEnabled) {
if (this.username && this.password) {
try {
const accessToken = await this.login(this.username, this.password);
await this.checkTokenStatus();
if (!this.accessToken) {
await this.login(this.username, this.password);
}
Logger.log(
"Sending message to room " + name,
"LogbooksService.sendMessage",
Expand All @@ -141,7 +156,7 @@ export class LogbooksService {
this.httpService.post<{ event_id: string }>(
this.baseUrl + `/Logbooks/${name}/message`,
data,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same as above

{ headers: { Authorization: `Bearer ${accessToken.token}` } },
{ headers: { Authorization: `Bearer ${this.accessToken}` } },
),
);
Logger.log(
Expand All @@ -163,6 +178,25 @@ export class LogbooksService {
}
return null;
}

async checkTokenStatus() {
try {
const res = await firstValueFrom(
this.httpService.get(this.baseUrl + "/Users/getTokenStatus"),
);
const shouldRenewToken = res.data;

if (shouldRenewToken) {
this.accessToken = null;
}
return;
} catch (error) {
handleAxiosRequestError(error, "LogbooksService.getLoopbackServerStatus");
throw new InternalServerErrorException(
"Logbook get loopback server status failed",
);
}
}
}

const sortMessages = (messages: Message[], sortField: string): Message[] => {
Expand Down
Loading