Skip to content

Instantly share code, notes, and snippets.

@paul121
Created November 27, 2019 21:43
Show Gist options
  • Save paul121/7475d42fa2e9c07cacb2b6050425dd21 to your computer and use it in GitHub Desktop.
Save paul121/7475d42fa2e9c07cacb2b6050425dd21 to your computer and use it in GitHub Desktop.
famrOS node-red auth flow
[
{
"id": "58ff452f.d398fc",
"type": "tab",
"label": "Test farmOS Flow",
"disabled": false,
"info": ""
},
{
"id": "9793e818.72b8b8",
"type": "ui_text",
"z": "58ff452f.d398fc",
"group": "b5079d0.289126",
"order": 4,
"width": 0,
"height": 0,
"name": "",
"label": "Server Name",
"format": "{{msg.payload.name}}",
"layout": "row-spread",
"x": 1050,
"y": 420,
"wires": []
},
{
"id": "88b453e3.fc91b",
"type": "http request",
"z": "58ff452f.d398fc",
"name": "",
"method": "use",
"ret": "obj",
"paytoqs": false,
"url": "",
"tls": "",
"persist": false,
"proxy": "",
"authType": "",
"x": 590,
"y": 440,
"wires": [
[
"9793e818.72b8b8",
"48e94484.4d3f6c",
"be262591.529b9",
"57797f06.f8162",
"6ad6e304.35c3cc"
]
]
},
{
"id": "48e94484.4d3f6c",
"type": "ui_text",
"z": "58ff452f.d398fc",
"group": "b5079d0.289126",
"order": 5,
"width": 0,
"height": 0,
"name": "",
"label": "API Version",
"format": "{{msg.payload.api_version}}",
"layout": "row-spread",
"x": 1050,
"y": 460,
"wires": []
},
{
"id": "886badef.c8fb1",
"type": "ui_dropdown",
"z": "58ff452f.d398fc",
"name": "",
"label": "Farm Terms",
"tooltip": "",
"place": "Select option",
"group": "b5079d0.289126",
"order": 6,
"width": 0,
"height": 0,
"passthru": false,
"options": [
{
"label": "",
"value": "",
"type": "str"
}
],
"payload": "",
"topic": "",
"x": 1050,
"y": 540,
"wires": [
[]
]
},
{
"id": "be262591.529b9",
"type": "function",
"z": "58ff452f.d398fc",
"name": "Filter Terms",
"func": "termDict = msg.payload.resources.taxonomy_term;\noptions = [];\n\nObject.keys(termDict).forEach(function (key) {\n var label = termDict[key].label;\n var vid = termDict[key].vid;\n var opt = {};\n opt[label] = vid;\n options.push(opt);\n});\nmsg.options = options;\nreturn msg;",
"outputs": 1,
"noerr": 0,
"x": 870,
"y": 540,
"wires": [
[
"886badef.c8fb1"
]
]
},
{
"id": "d9b95b2c.26ebe",
"type": "ui_dropdown",
"z": "58ff452f.d398fc",
"name": "",
"label": "Farm Assets",
"tooltip": "",
"place": "Select option",
"group": "b5079d0.289126",
"order": 7,
"width": 0,
"height": 0,
"passthru": false,
"options": [
{
"label": "",
"value": "",
"type": "str"
}
],
"payload": "",
"topic": "",
"x": 1050,
"y": 600,
"wires": [
[]
]
},
{
"id": "57797f06.f8162",
"type": "function",
"z": "58ff452f.d398fc",
"name": "Filter Assets",
"func": "assets = msg.payload.resources.farm_asset;\noptions = [];\n\nObject.keys(assets).forEach(function (key) {\n var label = assets[key].label;\n var value = assets[key];\n var opt = {};\n opt[label] = value;\n options.push(opt);\n});\nmsg.options = options;\nreturn msg;",
"outputs": 1,
"noerr": 0,
"x": 870,
"y": 600,
"wires": [
[
"d9b95b2c.26ebe"
]
]
},
{
"id": "17438159.6e513f",
"type": "ui_dropdown",
"z": "58ff452f.d398fc",
"name": "",
"label": "Farm Logs",
"tooltip": "",
"place": "Select option",
"group": "b5079d0.289126",
"order": 8,
"width": 0,
"height": 0,
"passthru": false,
"options": [
{
"label": "",
"value": "",
"type": "str"
}
],
"payload": "",
"topic": "",
"x": 1050,
"y": 660,
"wires": [
[]
]
},
{
"id": "6ad6e304.35c3cc",
"type": "function",
"z": "58ff452f.d398fc",
"name": "Filter Logs",
"func": "logs = msg.payload.resources.log;\noptions = [];\n\nObject.keys(logs).forEach(function (key) {\n var label = logs[key].label;\n var value = logs[key];\n var opt = {};\n opt[label] = value;\n options.push(opt);\n});\nmsg.options = options;\nreturn msg;",
"outputs": 1,
"noerr": 0,
"x": 870,
"y": 660,
"wires": [
[
"17438159.6e513f"
]
]
},
{
"id": "bd8bb6d6.d16e98",
"type": "ui_gauge",
"z": "58ff452f.d398fc",
"name": "",
"group": "b5079d0.289126",
"order": 9,
"width": 0,
"height": 0,
"gtype": "gage",
"title": "# Logs",
"label": "units",
"format": "{{value}}",
"min": 0,
"max": "100",
"colors": [
"#00b500",
"#e6e600",
"#ca3838"
],
"seg1": "",
"seg2": "",
"x": 1050,
"y": 720,
"wires": []
},
{
"id": "12088869.8da06",
"type": "http request",
"z": "58ff452f.d398fc",
"name": "",
"method": "use",
"ret": "obj",
"paytoqs": false,
"url": "",
"tls": "",
"persist": false,
"proxy": "",
"authType": "",
"x": 590,
"y": 500,
"wires": [
[
"6ac73876.d9bd7"
]
]
},
{
"id": "df4262fa.d3bc2",
"type": "ui_button",
"z": "58ff452f.d398fc",
"name": "",
"group": "b5079d0.289126",
"order": 3,
"width": 0,
"height": 0,
"passthru": false,
"label": "Refresh!!",
"tooltip": "",
"color": "",
"bgcolor": "",
"icon": "",
"payload": "",
"payloadType": "str",
"topic": "",
"x": 100,
"y": 460,
"wires": [
[
"51cd3f66.50444"
]
]
},
{
"id": "6ac73876.d9bd7",
"type": "function",
"z": "58ff452f.d398fc",
"name": "Parse # Logs",
"func": "var size = Object.keys(msg.payload.list).length;\n\nmsg.payload = size;\nreturn msg;",
"outputs": 1,
"noerr": 0,
"x": 870,
"y": 720,
"wires": [
[
"bd8bb6d6.d16e98"
]
]
},
{
"id": "26110d8.1b23872",
"type": "ui_gauge",
"z": "58ff452f.d398fc",
"name": "",
"group": "b5079d0.289126",
"order": 10,
"width": 0,
"height": 0,
"gtype": "gage",
"title": "# Assets",
"label": "units",
"format": "{{value}}",
"min": 0,
"max": "50",
"colors": [
"#00b500",
"#e6e600",
"#ca3838"
],
"seg1": "10",
"seg2": "12",
"x": 1060,
"y": 780,
"wires": []
},
{
"id": "838b1e95.678388",
"type": "function",
"z": "58ff452f.d398fc",
"name": "Parse # Assets",
"func": "var size = Object.keys(msg.payload.list).length;\n\nmsg.payload = size;\nreturn msg;",
"outputs": 1,
"noerr": 0,
"x": 880,
"y": 780,
"wires": [
[
"26110d8.1b23872"
]
]
},
{
"id": "150edf61.428f89",
"type": "http request",
"z": "58ff452f.d398fc",
"name": "",
"method": "use",
"ret": "obj",
"paytoqs": false,
"url": "",
"tls": "",
"persist": false,
"proxy": "",
"authType": "",
"x": 590,
"y": 560,
"wires": [
[
"838b1e95.678388"
]
]
},
{
"id": "82f91b36.552c78",
"type": "oauth2",
"z": "58ff452f.d398fc",
"name": "",
"container": "payload",
"access_token_url": "http://localhost/oauth2/token",
"grant_type": "set_by_credentials",
"username": "paul",
"password": "test",
"client_id": "farmos_api_client",
"client_secret": "client_secret",
"scope": "farmos_restws_access",
"x": 570,
"y": 120,
"wires": [
[
"4df99b8f.7086f4"
]
]
},
{
"id": "4df99b8f.7086f4",
"type": "function",
"z": "58ff452f.d398fc",
"name": "Save OAuth Token",
"func": "if (msg.payload.authorization) {\n var token = msg.payload.authorization;\n flow.set('token', token);\n msg.payload = token;\n} else {\n msg.payload = \"Authorization error.\";\n}\nreturn msg;",
"outputs": 1,
"noerr": 0,
"x": 750,
"y": 120,
"wires": [
[
"f026dcfb.02ce58"
]
]
},
{
"id": "51cd3f66.50444",
"type": "function",
"z": "58ff452f.d398fc",
"name": "Prepare HTTP request",
"func": "msg.headers = {};\n\nvar token = flow.get('token');\nif (token) {\n msg.headers[\"authorization\"] = token;\n}\n\nvar crsf = flow.get('crsf');\nif (crsf) {\n msg.headers = {};\n msg.headers[\"X-CSRF-Token\"] = crsf;\n}\n\nvar cookies = flow.get('cookies');\nif (cookies) {\n msg.cookies = cookies;\n}\n\nreturn msg;",
"outputs": 1,
"noerr": 0,
"x": 180,
"y": 520,
"wires": [
[
"6c1b445f.c9f364",
"7f8834a6.bb5454",
"27dc9e9f.6bc5da"
]
]
},
{
"id": "f026dcfb.02ce58",
"type": "ui_text",
"z": "58ff452f.d398fc",
"group": "16448850.68666",
"order": 2,
"width": 0,
"height": 0,
"name": "",
"label": "Auth Token",
"format": "{{msg.payload}}",
"layout": "row-spread",
"x": 940,
"y": 120,
"wires": []
},
{
"id": "9c6d219d.1bf7c",
"type": "ui_form",
"z": "58ff452f.d398fc",
"name": "",
"label": "OAuth",
"group": "16448850.68666",
"order": 1,
"width": 0,
"height": 0,
"options": [
{
"label": "farmOS URL",
"value": "url",
"type": "text",
"required": true
},
{
"label": "Username",
"value": "username",
"type": "text",
"required": true
},
{
"label": "Password",
"value": "password",
"type": "password",
"required": true
}
],
"formValue": {
"url": "",
"username": "",
"password": ""
},
"payload": "",
"submit": "submit",
"cancel": "cancel",
"topic": "",
"x": 110,
"y": 120,
"wires": [
[
"5da51681.9cd4a8"
]
]
},
{
"id": "5da51681.9cd4a8",
"type": "function",
"z": "58ff452f.d398fc",
"name": "Creat OAuth2Request",
"func": "msg.oauth2Request = { \n \"access_token_url\": msg.payload.url.concat(\"/oauth2/token\"), \n \"credentials\": {\n \"grant_type\": \"password\",\n \"username\": msg.payload.username,\n \"password\": msg.payload.password,\n \"client_id\": \"farmos_api_client\",\n \"client_secret\": \"client_secret\",\n \"scope\": \"farmos_restws_access\"\n }\n };\nreturn msg;",
"outputs": 1,
"noerr": 0,
"x": 360,
"y": 120,
"wires": [
[
"82f91b36.552c78"
]
]
},
{
"id": "fe205c8.47cd0a",
"type": "ui_form",
"z": "58ff452f.d398fc",
"name": "",
"label": "Drupal Auth",
"group": "16448850.68666",
"order": 3,
"width": 0,
"height": 0,
"options": [
{
"label": "farmOS URL",
"value": "url",
"type": "text",
"required": true
},
{
"label": "Username",
"value": "name",
"type": "text",
"required": true
},
{
"label": "Password",
"value": "pass",
"type": "password",
"required": true
}
],
"formValue": {
"url": "",
"name": "",
"pass": ""
},
"payload": "",
"submit": "submit",
"cancel": "cancel",
"topic": "",
"x": 130,
"y": 240,
"wires": [
[
"8d1065d8.45cb68"
]
]
},
{
"id": "8d1065d8.45cb68",
"type": "function",
"z": "58ff452f.d398fc",
"name": "Creat Drupal Auth Payload",
"func": "flow.set('url', msg.payload.url);\nmsg.url = msg.payload.url.concat(\"/user/login\");\nmsg.headers = {'content-type':'application/x-www-form-urlencoded'};\nmsg.payload[\"form_id\"] = \"user_login\";\nmsg.method = \"POST\";\nreturn msg;",
"outputs": 1,
"noerr": 0,
"x": 380,
"y": 240,
"wires": [
[
"2b2cf51b.d1baba"
]
]
},
{
"id": "2b2cf51b.d1baba",
"type": "http request",
"z": "58ff452f.d398fc",
"name": "",
"method": "use",
"ret": "txt",
"paytoqs": false,
"url": "",
"tls": "",
"persist": false,
"proxy": "",
"authType": "",
"x": 630,
"y": 240,
"wires": [
[
"38832e14.bfe862"
]
]
},
{
"id": "38832e14.bfe862",
"type": "function",
"z": "58ff452f.d398fc",
"name": "Save Cookie",
"func": "var cookies = {};\nfor (var key in msg.responseCookies) {\n cookies[key] = msg.responseCookies[key].value;\n}\nflow.set('cookies', cookies);\n\nmsg.cookies = cookies;\nmsg.method = \"GET\";\nmsg.url = flow.get('url').concat(\"/restws/session/token\");\nreturn msg;",
"outputs": 1,
"noerr": 0,
"x": 850,
"y": 240,
"wires": [
[
"ea9f325d.ac10c8"
]
]
},
{
"id": "ea9f325d.ac10c8",
"type": "http request",
"z": "58ff452f.d398fc",
"name": "",
"method": "use",
"ret": "txt",
"paytoqs": false,
"url": "",
"tls": "",
"persist": false,
"proxy": "",
"authType": "",
"x": 1040,
"y": 240,
"wires": [
[
"d3ccd87a.cfbb38"
]
]
},
{
"id": "d3ccd87a.cfbb38",
"type": "function",
"z": "58ff452f.d398fc",
"name": "Save CSRF token",
"func": "flow.set('csrf', msg.payload);\nreturn msg;",
"outputs": 1,
"noerr": 0,
"x": 1260,
"y": 240,
"wires": [
[
"bc40b647.41e408"
]
]
},
{
"id": "6c1b445f.c9f364",
"type": "function",
"z": "58ff452f.d398fc",
"name": "",
"func": "msg.url = flow.get('url').concat('/farm.json');\nmsg.method = \"GET\";\nreturn msg;",
"outputs": 1,
"noerr": 0,
"x": 430,
"y": 440,
"wires": [
[
"88b453e3.fc91b"
]
]
},
{
"id": "7f8834a6.bb5454",
"type": "function",
"z": "58ff452f.d398fc",
"name": "",
"func": "msg.url = flow.get('url').concat('/log.json');\nmsg.method = \"GET\";\nreturn msg;",
"outputs": 1,
"noerr": 0,
"x": 430,
"y": 500,
"wires": [
[
"12088869.8da06"
]
]
},
{
"id": "27dc9e9f.6bc5da",
"type": "function",
"z": "58ff452f.d398fc",
"name": "",
"func": "msg.url = flow.get('url').concat('/farm_asset.json');\nmsg.method = \"GET\";\nreturn msg;",
"outputs": 1,
"noerr": 0,
"x": 430,
"y": 560,
"wires": [
[
"150edf61.428f89"
]
]
},
{
"id": "bc40b647.41e408",
"type": "ui_text",
"z": "58ff452f.d398fc",
"group": "16448850.68666",
"order": 3,
"width": 0,
"height": 0,
"name": "",
"label": "CSRF Token",
"format": "{{msg.payload}}",
"layout": "row-spread",
"x": 1230,
"y": 300,
"wires": []
},
{
"id": "b5079d0.289126",
"type": "ui_group",
"z": "",
"name": "Server Info",
"tab": "7820596d.bb40b",
"disp": true,
"width": "6",
"collapse": false
},
{
"id": "16448850.68666",
"type": "ui_group",
"z": "",
"name": "Authorize farmOS",
"tab": "7820596d.bb40b",
"disp": true,
"width": "6",
"collapse": false
},
{
"id": "7820596d.bb40b",
"type": "ui_tab",
"z": "",
"name": "Home",
"icon": "dashboard",
"disabled": false,
"hidden": false
}
]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment