-
Notifications
You must be signed in to change notification settings - Fork 24
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
Junjiequan
merged 11 commits into
master
from
SWAP-3487-scicat-fix-test-fail-for-the-loopback-recent-pr-a
Sep 20, 2023
Merged
Changes from 3 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
6efe728
fix: saving logbook token in memory
Junjiequan 9fbbc80
Merge branch 'master' into SWAP-3487-scicat-fix-test-fail-for-the-loo…
Junjiequan 7f24a71
fix: minor naming change
Junjiequan c79e428
feat: included unit testing for logbooks
Junjiequan 61060ae
fix: minor refactor of if check for re-login
Junjiequan 81c074d
fix: removed headers from request
Junjiequan 8a19493
fix: remove synapse auth from scicat BE
Junjiequan e871b48
fix: adjust test for new change
Junjiequan 8187d27
fix: minor fix for test
Junjiequan 01d03a5
Merge branch 'master' into SWAP-3487-scicat-fix-test-fail-for-the-loo…
Junjiequan df73d73
fix: minor ENV change for loopback e2e environment
Junjiequan File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,6 +16,7 @@ export class LogbooksService { | |
private baseUrl; | ||
private username; | ||
private password; | ||
private accessToken: string | null; | ||
|
||
constructor( | ||
private readonly configService: ConfigService, | ||
|
@@ -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"); | ||
|
@@ -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( | ||
|
@@ -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}`, | ||
{ headers: { Authorization: `Bearer ${accessToken.token}` } }, | ||
{ headers: { Authorization: `Bearer ${this.accessToken}` } }, | ||
), | ||
); | ||
Logger.log("Found logbook " + name, "LogbooksService.findByName"); | ||
|
@@ -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", | ||
|
@@ -141,7 +156,7 @@ export class LogbooksService { | |
this.httpService.post<{ event_id: string }>( | ||
this.baseUrl + `/Logbooks/${name}/message`, | ||
data, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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( | ||
|
@@ -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[] => { | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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