-
Notifications
You must be signed in to change notification settings - Fork 1
Webrtc 170 #12
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
base: main
Are you sure you want to change the base?
Webrtc 170 #12
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
REACT_APP_ACCOUNT_USERNAME=+191xxxxxxx | ||
REACT_APP_ACCOUNT_DISPLAY_NAME=+1919xxxxxx | ||
REACT_APP_ACCOUNT_PASSWORD=+1919xxxxxxx | ||
REACT_APP_AUTH_TOKEN=xxxxxxxx | ||
REACT_APP_ACCOUNT_USERNAME=xxxxxxxxxx | ||
REACT_APP_ACCOUNT_DISPLAY_NAME=xxxxxxxxx | ||
REACT_APP_ACCOUNT_PASSWORD=xxxxxxxxx | ||
REACT_APP_AUTH_URL=https://authtoken.url | ||
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. this will always be the id.bandwidth URL correct? Should we just set it for the user since it is not a secret value? |
||
REACT_APP_AUTH_CREDENTIALS='username:pass' |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
// Store token and expiry in localStorage | ||
const TOKEN_STORAGE_KEY = 'auth_token'; | ||
const EXPIRY_STORAGE_KEY = 'token_expiry'; | ||
|
||
// Your API details | ||
const AUTH_URL = process.env.REACT_APP_AUTH_URL; | ||
const header = process.env.REACT_APP_AUTH_CREDENTIALS; | ||
const BASIC_AUTH_CREDENTIALS = btoa(header); // Base64 encoding for Basic Auth | ||
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. instead of having repeated variables (username, password, and authCredentials which is just username:password) - can we simplify the env vars and construct the header from the provided user/pass env vars? |
||
const REFRESH_INTERVAL = 60 * 1000; // Check every 60 seconds | ||
|
||
// Function to fetch new token | ||
const fetchAuthToken = async () => { | ||
try { | ||
var headers = { | ||
'Authorization': 'Basic ' + BASIC_AUTH_CREDENTIALS, | ||
'Content-Type': 'application/x-www-form-urlencoded', | ||
'Origin': '*', | ||
'Access-Control-Allow-Headers': '*', | ||
'Access-Control-Allow-Origin': '*', | ||
}; | ||
const response = await fetch(`${AUTH_URL}`, { | ||
method: 'post', | ||
headers: headers, | ||
body: 'grant_type=client_credentials' | ||
},); | ||
|
||
const data = await response.json(); | ||
|
||
console.log("Data: " + data); | ||
|
||
const { access_token, expires_in } = data; | ||
|
||
// Store token and expiry time | ||
localStorage.setItem(TOKEN_STORAGE_KEY, access_token); | ||
localStorage.setItem(EXPIRY_STORAGE_KEY, Date.now() + expires_in * 1000); | ||
return access_token; | ||
} catch (error) { | ||
console.error("Error fetching auth token", error); | ||
return null; | ||
} | ||
}; | ||
|
||
// Function to check and refresh token periodically | ||
const startTokenRefreshLoop = () => { | ||
setInterval(async () => { | ||
const token = localStorage.getItem(TOKEN_STORAGE_KEY); | ||
const expiryTime = localStorage.getItem(EXPIRY_STORAGE_KEY); | ||
|
||
if (!token || !expiryTime) { | ||
console.log("[oAuth Service] No token found, fetching a new one..."); | ||
await fetchAuthToken(); | ||
return; | ||
} | ||
|
||
const timeLeft = expiryTime - Date.now(); | ||
console.log(`[oAuth Service] Token expires in ${Math.floor(timeLeft / 1000)} seconds`); | ||
|
||
// Refresh token if it will expire in less than 2 minutes | ||
if (timeLeft < 2 * 60 * 1000) { | ||
console.log("[oAuth Service] Refreshing token before expiry..."); | ||
await fetchAuthToken(); | ||
} | ||
}, REFRESH_INTERVAL); | ||
}; | ||
|
||
|
||
// Function to check token validity and refresh if needed | ||
const getAuthToken = async () => { | ||
const token = localStorage.getItem(TOKEN_STORAGE_KEY); | ||
const expiryTime = parseInt(localStorage.getItem(EXPIRY_STORAGE_KEY), 10); | ||
|
||
if (token && expiryTime && Date.now() < expiryTime - 60000) { // Refresh 1 min before expiry | ||
return token; | ||
} | ||
|
||
return await fetchAuthToken(); | ||
}; | ||
|
||
// Initialize authentication flow | ||
const initAuth = async () => { | ||
await fetchAuthToken(); // Get initial token | ||
startTokenRefreshLoop(); // Start background refresh | ||
}; | ||
|
||
export { initAuth, TOKEN_STORAGE_KEY }; |
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.
My only qualm with this is that we encourage customers to fetch auth tokens from the frontend, and storing the BW Username and pass there is not safe
it is a sample app at the end of the day, but the tokens shouldn't be generated client side, a backend should handle that so that the user/pass is nowhere in the client