Skip to content

Commit

Permalink
Improve error handling and remove unused URL import in oauth2.js
Browse files Browse the repository at this point in the history
 - Removed the unused URL import from oauth2.js.
 - Added URLSearchParams import for handling form data.
 - Wrapped the credentials retrieval in a try-catch block to handle potential errors.
 - Changed the response status code to 404 when credentials are not found.
 - Changed the response status code to 500 for server errors.
 - Updated the success HTML response to use proper indentation and formatting.
 - Changed the response status code to 400 when credentials are missing in the token exchange endpoint.
  • Loading branch information
caputomarcos committed May 23, 2024
1 parent 85d90aa commit 9621caf
Showing 1 changed file with 29 additions and 25 deletions.
54 changes: 29 additions & 25 deletions src/oauth2.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,9 @@ module.exports = function (RED) {
'use strict';

const axios = require('axios');
const { URL } = require('url');
const http = require('http');
const https = require('https');

const { URLSearchParams } = require('url'); // Use URLSearchParams for form data
const Logger = require('node-red-contrib-oauth2/src/libs/logger');

/**
Expand All @@ -20,6 +19,7 @@ module.exports = function (RED) {
RED.nodes.createNode(this, config);
this.logger = new Logger({ name: 'identifier', count: null, active: config.debug || false, label: 'debug' });
this.logger.debug('Constructor: Initializing node with config', config);

// Node configuration properties
this.name = config.name || '';
this.container = config.container || '';
Expand Down Expand Up @@ -330,11 +330,15 @@ module.exports = function (RED) {
* @param {Object} res - The response object.
*/
RED.httpAdmin.get('/oauth2/credentials/:token', (req, res) => {
const credentials = RED.nodes.getCredentials(req.params.token);
if (credentials) {
res.json({ code: credentials.code, redirect_uri: credentials.redirect_uri });
} else {
res.send('oauth2.error.no-credentials');
try {
const credentials = RED.nodes.getCredentials(req.params.token);
if (credentials) {
res.json({ code: credentials.code, redirect_uri: credentials.redirect_uri });
} else {
res.status(404).send('oauth2.error.no-credentials');
}
} catch (error) {
res.status(500).send('oauth2.error.server-error');
}
});

Expand All @@ -356,25 +360,25 @@ module.exports = function (RED) {
RED.nodes.addCredentials(node_id, credentials);

res.send(`
<HTML>
<HEAD>
<script language="javascript" type="text/javascript">
function closeWindow() {
window.open('', '_parent', '');
window.close();
}
function delay() {
setTimeout("closeWindow()", 1000);
}
</script>
</HEAD>
<BODY onload="javascript:delay();">
<p>Success! This page can be closed if it doesn't do so automatically.</p>
</BODY>
</HTML>
`);
<HTML>
<HEAD>
<script language="javascript" type="text/javascript">
function closeWindow() {
window.open('', '_parent', '');
window.close();
}
function delay() {
setTimeout("closeWindow()", 1000);
}
</script>
</HEAD>
<BODY onload="javascript:delay();">
<p>Success! This page can be closed if it doesn't do so automatically.</p>
</BODY>
</HTML>
`);
} else {
res.send('oauth2.error.no-credentials');
res.status(400).send('oauth2.error.no-credentials');
}
});

Expand Down

0 comments on commit 9621caf

Please sign in to comment.