|
| 1 | +/** |
| 2 | + * MIT License |
| 3 | + * |
| 4 | + * Copyright (c) 2019 Marcos Caputo <caputo.marcos@gmail.com> https://github.com/caputomarcos/node-red-contrib-oauth2 |
| 5 | + * |
| 6 | + * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 7 | + * of this software and associated documentation files (the "Software"), to deal |
| 8 | + * in the Software without restriction, including without limitation the rights |
| 9 | + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 10 | + * copies of the Software, and to permit persons to whom the Software is |
| 11 | + * furnished to do so, subject to the following conditions: |
| 12 | + * |
| 13 | + * The above copyright notice and this permission notice shall be included in all |
| 14 | + * copies or substantial portions of the Software. |
| 15 | + * |
| 16 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 17 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 18 | + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 19 | + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 20 | + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 21 | + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 22 | + * SOFTWARE. |
| 23 | + * |
| 24 | + **/ |
| 25 | + |
| 26 | +import * as request from 'request'; |
| 27 | +import * as querystring from 'querystring'; |
| 28 | +import * as RED from "node-red"; |
| 29 | +import { Red } from "node-red"; |
| 30 | +import { Logger } from "../../Logger"; |
| 31 | +import { NoderedUtil } from "openflow-api"; |
| 32 | + |
| 33 | +export interface Ioauth2 { |
| 34 | + name: string; |
| 35 | + container: string; |
| 36 | + access_token_url: string; |
| 37 | + grant_type: string; |
| 38 | + username: string; |
| 39 | + password: string; |
| 40 | + client_id: string; |
| 41 | + client_secret: string; |
| 42 | + scope: string; |
| 43 | +} |
| 44 | +export class oauth2 { |
| 45 | + public node: Red = null; |
| 46 | + public name: string = ""; |
| 47 | + public restKey: string; |
| 48 | + public appID: string; |
| 49 | + constructor(public config: Ioauth2) { |
| 50 | + RED.nodes.createNode(this, config); |
| 51 | + try { |
| 52 | + this.node = this; |
| 53 | + this.node.status({}); |
| 54 | + |
| 55 | + config.name = config.name || ""; |
| 56 | + config.container = config.container || "oauth2Response"; |
| 57 | + config.access_token_url = config.access_token_url || ""; |
| 58 | + config.grant_type = config.grant_type || "password"; |
| 59 | + config.username = config.username || ""; |
| 60 | + config.password = config.password || ""; |
| 61 | + config.client_id = config.client_id || ""; |
| 62 | + config.client_secret = config.client_secret || ""; |
| 63 | + config.scope = config.scope || ""; |
| 64 | + |
| 65 | + this.node.on("input", this.oninput); |
| 66 | + this.node.on("close", this.onclose); |
| 67 | + } catch (error) { |
| 68 | + NoderedUtil.HandleError(this, error); |
| 69 | + } |
| 70 | + } |
| 71 | + async oninput(msg: any) { |
| 72 | + // set an empty form |
| 73 | + let Form = {}; |
| 74 | + |
| 75 | + // TODO - ??? =) |
| 76 | + let Method = "Post"; |
| 77 | + let Authorization = ''; |
| 78 | + // Choice a grant_type |
| 79 | + if ((this.node.grant_type === "set_by_credentials" || this.node.grant_type == null) && msg.oauth2Request) { |
| 80 | + this.node.access_token_url = msg.oauth2Request.access_token_url; |
| 81 | + this.node.client_id = msg.oauth2Request.credentials.client_id; |
| 82 | + this.node.client_secret = msg.oauth2Request.credentials.client_secret; |
| 83 | + Form = msg.oauth2Request.credentials; |
| 84 | + if (msg.oauth2Request.username && msg.oauth2Request.password) { |
| 85 | + // TODO - ??? =) |
| 86 | + Authorization = 'Basic ' + Buffer.from(`${msg.oauth2Request.username}:${msg.oauth2Request.password}`).toString('base64'); |
| 87 | + } else { |
| 88 | + // TODO - ??? =) |
| 89 | + Authorization = 'Basic ' + Buffer.from(`${this.node.client_id}:${this.node.client_secret}`).toString('base64'); |
| 90 | + } |
| 91 | + } else if (this.node.grant_type === "password") { |
| 92 | + Form = { |
| 93 | + 'username': this.node.username, |
| 94 | + 'password': this.node.password, |
| 95 | + 'grant_type': this.node.grant_type, |
| 96 | + 'client_id': this.node.client_id, |
| 97 | + 'client_secret': this.node.client_secret |
| 98 | + }; |
| 99 | + // TODO - ??? =) |
| 100 | + Authorization = 'Basic ' + Buffer.from(`${this.node.client_id}:${this.node.client_secret}`).toString('base64'); |
| 101 | + } else if (this.node.grant_type === "client_credentials") { |
| 102 | + Form = { |
| 103 | + 'grant_type': this.node.grant_type, |
| 104 | + 'client_id': this.node.client_id, |
| 105 | + 'client_secret': this.node.client_secret, |
| 106 | + 'scope': this.node.scope |
| 107 | + }; |
| 108 | + // TODO - ??? =) |
| 109 | + Authorization = 'Basic ' + Buffer.from(`${this.node.client_id}:${this.node.client_secret}`).toString('base64'); |
| 110 | + } |
| 111 | + |
| 112 | + let Body = querystring.stringify(Form); |
| 113 | + |
| 114 | + // set Headers |
| 115 | + // TODO - improve 'Authorization': 'Basic ' ??? =) |
| 116 | + let Headers = { |
| 117 | + // 'Accept': 'application/json', |
| 118 | + 'Content-Type': 'application/x-www-form-urlencoded', |
| 119 | + 'Content-Length': Buffer.byteLength(Body), |
| 120 | + 'Authorization': Authorization |
| 121 | + }; |
| 122 | + |
| 123 | + // Put all together |
| 124 | + let Options = { |
| 125 | + method: Method, |
| 126 | + url: this.node.access_token_url, |
| 127 | + headers: Headers, |
| 128 | + body: Body, |
| 129 | + json: false |
| 130 | + }; |
| 131 | + |
| 132 | + // make a post request |
| 133 | + request.post(Options, (err, response, body) => { |
| 134 | + if (msg.oauth2Request) delete msg.oauth2Request; |
| 135 | + let oauth2Body: any = null; |
| 136 | + try { |
| 137 | + oauth2Body = JSON.parse(body ? body : JSON.stringify("{}")); |
| 138 | + if (response && response.statusCode && response.statusCode === 200) { |
| 139 | + msg[this.node.container] = { |
| 140 | + authorization: `${oauth2Body.token_type} ${oauth2Body.access_token}`, |
| 141 | + oauth2Response: { |
| 142 | + statusCode: response.statusCode, |
| 143 | + statusMessage: response.statusMessage, |
| 144 | + body: oauth2Body |
| 145 | + } |
| 146 | + }; |
| 147 | + |
| 148 | + this.node.status({ |
| 149 | + fill: "green", |
| 150 | + shape: "dot", |
| 151 | + text: `HTTP ${response.statusCode}, has token!` |
| 152 | + }); |
| 153 | + } else if (response && response.statusCode && response.statusCode !== 200) { |
| 154 | + msg[this.node.container] = { |
| 155 | + oauth2Response: { |
| 156 | + statusCode: response.statusCode, |
| 157 | + statusMessage: response.statusMessage, |
| 158 | + body: oauth2Body |
| 159 | + } |
| 160 | + }; |
| 161 | + this.node.status({ |
| 162 | + fill: "red", |
| 163 | + shape: "dot", |
| 164 | + text: `HTTP ${response.statusCode}, hasn't token!` |
| 165 | + }); |
| 166 | + } |
| 167 | + } catch (error) { |
| 168 | + var errormessage = error.message ? error.message : error; |
| 169 | + msg[this.node.container] = { |
| 170 | + oauth2Response: { |
| 171 | + statusCode: response.statusCode, |
| 172 | + statusMessage: errormessage, |
| 173 | + body: oauth2Body |
| 174 | + } |
| 175 | + }; |
| 176 | + this.node.status({ |
| 177 | + fill: "red", |
| 178 | + shape: "dot", |
| 179 | + text: `HTTP ${response.statusCode}, hasn't token!` |
| 180 | + }); |
| 181 | + |
| 182 | + } |
| 183 | + if (err && err.code) { |
| 184 | + this.node.status({ fill: "red", shape: "dot", text: `ERR ${err.code}` }); |
| 185 | + msg.err = JSON.parse(JSON.stringify(err)); |
| 186 | + } else if (err && err.message && err.stack) { |
| 187 | + this.node.status({ fill: "red", shape: "dot", text: `ERR ${err.message}` }); |
| 188 | + msg.err = { message: err.message, stack: err.stack }; |
| 189 | + } |
| 190 | + this.node.send(msg); |
| 191 | + }); |
| 192 | + } |
| 193 | + async onclose(removed: boolean, done: any) { |
| 194 | + done(); |
| 195 | + } |
| 196 | +} |
0 commit comments