Skip to content

Commit

Permalink
Merge pull request #66 from caputomarcos/65-small-improvements
Browse files Browse the repository at this point in the history
small improvements #65
  • Loading branch information
caputomarcos authored Apr 7, 2023
2 parents 49a927d + f29a23b commit d8bbe26
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 18 deletions.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,11 @@ This sample used the [go-oauth2-server](https://github.com/RichardKnop/go-oauth2
| embedded credentials | boolean | This specifies whether to include the client credentials in the token request body for authentication purposes. |
| reject Unauthorized | boolean | This specifies whether to reject unauthorized requests. The rejectUnauthorized parameter controls SSL/TLS certificate validation for the server, with true enforcing validation and false disabling it. |

**`Note:`**

> * If running behind a proxy, the standard `http_proxy=...` environment variable should be set and `Node-RED restarted, or use Proxy Configuration`. If Proxy Configuration was set, the configuration take precedence over environment variable.
> * The OAuth redirect URL is set by default to `/oauth2/redirect`, which is the endpoint responsible for receiving the authorization `code`.
# Outputs

Expand Down
8 changes: 7 additions & 1 deletion locales/en-US/oauth2.html
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +47,19 @@

: use proxy (boolean) : This specifies whether to use a proxy or not.
* use proxy: `true`
: use proxy(Proxy Configuration) (object) : This specifies the configuration for the proxy.
: Proxy Configuration (object) : This specifies the configuration for the proxy.

: only send non-2xx responses to catch node (boolean) : This specifies whether to only catch non-2xx responses.
: embedded credentials (boolean) : This specifies whether to include the client credentials in the token request body for authentication purposes.
: reject Unauthorized (boolean) : This specifies whether to reject unauthorized requests. The rejectUnauthorized parameter controls SSL/TLS certificate validation for the server, with true enforcing validation and false disabling it.


**`Note:`**

> * If running behind a proxy, the standard `http_proxy=...` environment variable should be set and `Node-RED restarted, or use Proxy Configuration`. If Proxy Configuration was set, the configuration take precedence over environment variable.

> * The OAuth redirect URL is set by default to `/oauth2/redirect`, which is the endpoint responsible for receiving the authorization `code`.

### Outputs

: access_token (string) : This is the access token obtained from the OAuth2 server.
Expand Down
42 changes: 25 additions & 17 deletions oauth2.js
Original file line number Diff line number Diff line change
Expand Up @@ -179,8 +179,10 @@ module.exports = function (RED) {
}

const credentials = RED.nodes.getCredentials(node.id);
options.form.code = credentials.code;
options.form.redirect_uri = credentials.redirectUri;
if (credentials) {
options.form.code = credentials.code;
options.form.redirect_uri = credentials.redirectUri;
}
}
}

Expand Down Expand Up @@ -311,10 +313,14 @@ module.exports = function (RED) {
*/
RED.httpAdmin.get("/oauth2/credentials/:token", function (req, res) {
const credentials = RED.nodes.getCredentials(req.params.token);
res.json({
code: credentials.code,
redirect_uri: credentials.redirect_uri,
});
if (credentials) {
res.json({
code: credentials.code,
redirect_uri: credentials.redirect_uri,
});
} else {
res.send("oauth2.error.no-credentials");
}
});

/**
Expand All @@ -327,10 +333,10 @@ module.exports = function (RED) {
const state = req.query.state.split(":");
const node_id = state[0];
const credentials = RED.nodes.getCredentials(node_id);
credentials.code = req.query.code;
RED.nodes.addCredentials(node_id, credentials);

const html = `<HTML>
if (credentials) {
credentials.code = req.query.code;
RED.nodes.addCredentials(node_id, credentials);
const html = `<HTML>
<HEAD>
<script language=\"javascript\" type=\"text/javascript\">
function closeWindow() {
Expand All @@ -346,7 +352,10 @@ module.exports = function (RED) {
<p>Success! This page can be closed if it doesn't do so automatically.</p>
</BODY>
</HTML>`;
res.send(html);
res.send(html);
}
} else {
res.send("oauth2.error.no-credentials");
}
});

Expand Down Expand Up @@ -402,7 +411,7 @@ module.exports = function (RED) {

res.cookie("csrf", csrfToken);

var l = url.parse(req.query.authorizationEndpoint, true);
const l = url.parse(req.query.authorizationEndpoint, true);
const redirectUrl = url.format({
protocol: l.protocol.replace(":", ""),
hostname: l.hostname,
Expand All @@ -422,10 +431,9 @@ module.exports = function (RED) {
proxy: proxyOptions,
});
res.redirect(response.request.res.responseUrl);

RED.nodes.addCredentials(node_id, credentials);
} catch (error) {
res.sendStatus(500);
res.sendStatus(404);
}
});

Expand All @@ -441,9 +449,9 @@ module.exports = function (RED) {
description: req.query.error_description,
});
}
var state = req.query.state.split(":");
var node_id = state[0];
var credentials = RED.nodes.getCredentials(node_id);
const state = req.query.state.split(":");
const node_id = state[0];
const credentials = RED.nodes.getCredentials(node_id);
if (!credentials || !credentials.clientId || !credentials.clientSecret) {
return res.send("oauth2.error.no-credentials");
}
Expand Down

0 comments on commit d8bbe26

Please sign in to comment.