-
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.
Merge branch 'feature/436478-vocabularios' into 'develop'
Feature/436478 vocabularios See merge request upm-inesdata/inesdata-public-portal-backend!5
- Loading branch information
Showing
5 changed files
with
147 additions
and
86 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,36 @@ | ||
'use strict'; | ||
|
||
const axios = require('axios'); | ||
const tokenService = require('../../services/token-service'); | ||
|
||
|
||
module.exports = { | ||
getVocabularies: async (ctx, next) => { | ||
try { | ||
const accessToken = await tokenService.getAccessToken(); | ||
if (!accessToken) { | ||
ctx.body = { message: 'Access token not available. Please authenticate first.' }; | ||
ctx.status = 401; | ||
return; | ||
} | ||
|
||
|
||
// URL for fetching the vocabularies | ||
const vocabulariesBaseUrl = `${process.env.VOCABULARIES_BASE_URL}/shared/connector-vocabularies/request`; | ||
|
||
// Fetch the vocabularies using the access token | ||
const vocabulariesResponse = await axios.post(vocabulariesBaseUrl, {}, { | ||
headers: { | ||
Authorization: `Bearer ${accessToken}` | ||
} | ||
}); | ||
|
||
// Respond with the fetched data | ||
ctx.body = vocabulariesResponse.data; | ||
} catch (err) { | ||
console.error('Error:', err); | ||
ctx.body = { message: 'Error fetching vocabularies!', details: err.message }; | ||
ctx.status = 500; | ||
} | ||
} | ||
}; |
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,9 @@ | ||
module.exports = { | ||
"routes": [ | ||
{ | ||
"method": "POST", | ||
"path": "/get-vocabularies", | ||
"handler": "get-vocabularies.getVocabularies" | ||
} | ||
] | ||
}; |
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,91 @@ | ||
const axios = require('axios'); | ||
|
||
const tokenData = { | ||
accessToken: null, | ||
refreshToken: null, | ||
expiresIn: null, | ||
refreshExpiresIn: null, | ||
obtainedAt: null, | ||
refreshObtainedAt: null | ||
}; | ||
|
||
const getTokenFromAuthServer = async () => { | ||
try { | ||
const params = new URLSearchParams(); | ||
params.append('grant_type', 'password'); | ||
params.append('client_id', process.env.TOKEN_CLIENT_ID); | ||
params.append('username', process.env.TOKEN_USERNAME); | ||
params.append('password', process.env.TOKEN_PASSWORD); | ||
|
||
const tokenUrl = `${process.env.KEYCLOAK_BASE_URL}/realms/dataspace/protocol/openid-connect/token`; | ||
|
||
const response = await axios.post(tokenUrl, params); | ||
const data = response.data; | ||
|
||
tokenData.accessToken = data.access_token; | ||
tokenData.refreshToken = data.refresh_token; | ||
tokenData.expiresIn = data.expires_in; // expires_in is in seconds | ||
tokenData.refreshExpiresIn = data.refresh_expires_in; // refresh_expires_in is in seconds | ||
tokenData.obtainedAt = Date.now(); // store the time the token was obtained | ||
tokenData.refreshObtainedAt = Date.now(); // store the time the refresh token was obtained | ||
|
||
return data; | ||
} catch (error) { | ||
console.error('Error obtaining token from auth server:', error); | ||
throw new Error('Failed to obtain token from auth server'); | ||
} | ||
}; | ||
|
||
const refreshToken = async () => { | ||
try { | ||
const params = new URLSearchParams(); | ||
params.append('grant_type', 'refresh_token'); | ||
params.append('client_id', process.env.TOKEN_CLIENT_ID); | ||
params.append('refresh_token', tokenData.refreshToken); | ||
|
||
const tokenUrl = `${process.env.KEYCLOAK_BASE_URL}/realms/dataspace/protocol/openid-connect/token`; | ||
|
||
const response = await axios.post(tokenUrl, params); | ||
const data = response.data; | ||
|
||
tokenData.accessToken = data.access_token; | ||
tokenData.refreshToken = data.refresh_token; | ||
tokenData.expiresIn = data.expires_in; | ||
tokenData.refreshExpiresIn = data.refresh_expires_in; | ||
tokenData.obtainedAt = Date.now(); | ||
|
||
return data; | ||
} catch (error) { | ||
console.error('Error refreshing token:', error); | ||
throw new Error('Failed to refresh token'); | ||
} | ||
}; | ||
|
||
const getAccessToken = async () => { | ||
try { | ||
if (!tokenData.accessToken || !tokenData.refreshToken) { | ||
await getTokenFromAuthServer(); | ||
} else { | ||
const currentTime = Date.now(); | ||
const accessTokenExpired = (currentTime - tokenData.obtainedAt) / 1000 > tokenData.expiresIn; | ||
const refreshTokenExpired = (currentTime - tokenData.refreshObtainedAt) / 1000 > tokenData.refreshExpiresIn; | ||
|
||
if (refreshTokenExpired) { | ||
await getTokenFromAuthServer(); | ||
} else if (accessTokenExpired) { | ||
await refreshToken(); | ||
} | ||
} | ||
|
||
return tokenData.accessToken; | ||
} catch (error) { | ||
console.error('Error getting access token:', error); | ||
throw new Error('Failed to get access token'); | ||
} | ||
}; | ||
|
||
module.exports = { | ||
getTokenFromAuthServer, | ||
refreshToken, | ||
getAccessToken | ||
}; |