forked from akhenry/openmct-yamcs
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Improve the way the plugin authenticates itself against yamcs.
Also allow the plugin to work in the case where no authentication agaisnt yamcs server is needed
- Loading branch information
Showing
4 changed files
with
90 additions
and
55 deletions.
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
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
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 |
---|---|---|
@@ -0,0 +1,62 @@ | ||
import { | ||
getCookie | ||
} from '../utils.js'; | ||
|
||
export default class AuthProvider { | ||
#openmct; | ||
#username; | ||
#password; | ||
#authEndpoint; | ||
#useRefresh | ||
|
||
constructor({openmct, authEndpoint, username, password}) { | ||
console.log(authEndpoint); | ||
this.#openmct = openmct; | ||
this.#username = username; | ||
this.#password = password; | ||
this.#authEndpoint = authEndpoint + "/auth/token"; | ||
this.#useRefresh = false; | ||
|
||
this.#fetchAccessToken(); | ||
} | ||
|
||
async #fetchAccessToken() { | ||
const params = new URLSearchParams(); | ||
|
||
if (this.#useRefresh) { | ||
params.append("refresh_token", getCookie("refresh_token")); | ||
params.append("grant_type", "refresh_token"); | ||
|
||
} else { | ||
params.append("username", this.#username); | ||
params.append("password", this.#password); | ||
params.append("grant_type", "password"); | ||
} | ||
|
||
try { | ||
const response = await fetch(this.#authEndpoint, { | ||
method: "POST", | ||
headers: { | ||
"Content-Type": "application/x-www-form-urlencoded", | ||
}, | ||
body: params.toString(), | ||
}); | ||
|
||
if (!response.ok) { | ||
throw new Error(`Failed to retrieve API token: ${response.status}`); | ||
} | ||
|
||
const data = await response.json(); | ||
document.cookie = `access_token=${data.access_token}; path=/; SameSite=Strict;`; | ||
document.cookie = `refresh_token=${data.refresh_token}; path=/; SameSite=Strict;`; | ||
|
||
this.#useRefresh = true; | ||
setTimeout(() => { | ||
this.#fetchAccessToken(); | ||
}, 8 * 60 * 1000); // 8 minutes of expiration | ||
|
||
} catch (error) { | ||
console.error("Error fetching access token:", error); | ||
} | ||
} | ||
} |
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