-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrituals-token-check.html
More file actions
165 lines (136 loc) · 6.5 KB
/
rituals-token-check.html
File metadata and controls
165 lines (136 loc) · 6.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
<script type="text/javascript">
RED.nodes.registerType('rituals-token-check', {
category: 'smart home',
color: '#90CAF9',
defaults: {
name: {value: ""}
},
inputs: 1,
outputs: 2,
outputLabels: ["valid token", "invalid/expired"],
icon: "font-awesome/fa-check-circle",
label: function() {
return this.name || "Token Check";
},
paletteLabel: "token check"
});
</script>
<script type="text/html" data-template-name="rituals-token-check">
<div class="form-row">
<label for="node-input-name"><i class="fa fa-tag"></i> Name</label>
<input type="text" id="node-input-name" placeholder="Token Check">
</div>
</script>
<script type="text/html" data-help-name="rituals-token-check">
<p>Checks if a Rituals authentification token is valid and not expired.</p>
<h3>Outputs</h3>
<ol class="node-ports">
<li>Valid Token
<dl class="message-properties">
<dt>payload.valid <span class="property-type">boolean</span></dt>
<dd>true if token exists and is not expired</dd>
<dt>payload.tokenLength <span class="property-type">number</span></dt>
<dd>Length of the stored token</dd>
<dt>payload.expiresIn <span class="property-type">string</span></dt>
<dd>Time remaining until token expires (e.g. "22 hours")</dd>
<dt>payload.expiresAt <span class="property-type">string</span></dt>
<dd>Date/time when token will expire</dd>
<dt>payload.deviceCount <span class="property-type">number</span></dt>
<dd>Number of devices available</dd>
<dt>payload.firstDeviceHash <span class="property-type">string</span></dt>
<dd>Hash of the first device (for quick access)</dd>
<dt>ritualsToken <span class="property-type">string</span></dt>
<dd>The full authentification token</dd>
<dt>ritualsDevices <span class="property-type">array</span></dt>
<dd>Array of all available devices</dd>
<dt>deviceHash <span class="property-type">string</span></dt>
<dd>Hash of first device (ready to use with controller)</dd>
</dl>
</li>
<li>Invalid/Expired Token
<dl class="message-properties">
<dt>payload.valid <span class="property-type">boolean</span></dt>
<dd>false - token is missing or expired</dd>
<dt>payload.error <span class="property-type">string</span></dt>
<dd>Error message explaining why token is invalid</dd>
<dt>payload.expired <span class="property-type">boolean</span></dt>
<dd>true if token exists but has expired</dd>
<dt>payload.expiredAt <span class="property-type">string</span></dt>
<dd>When the token expired (if applicable)</dd>
<dt>payload.message <span class="property-type">string</span></dt>
<dd>User-friendly message about what to do</dd>
</dl>
</li>
</ol>
<h3>Details</h3>
<p>This node checks the validity of the stored Rituals authentification token. It has two outputs:</p>
<ul>
<li><strong>Output 1 (Valid):</strong> Token exists and is not expired. Message includes token, devices, and first device hash.</li>
<li><strong>Output 2 (Invalid):</strong> Token is missing or expired. Use this to trigger re-authentification.</li>
</ul>
<h3>Usage Examples</h3>
<h4>Example 1: Check Before Control</h4>
<pre>
[Inject] → [Token Check] → [Rituals Controller]
↓ (invalid)
[Rituals Auth]
</pre>
<p>Check token validity before sending commands. If invalid, trigger authentification.</p>
<h4>Example 2: Periodic Health Check</h4>
<pre>
[Inject: every 1 hour] → [Token Check] → [Debug: "Token OK"]
↓ (invalid)
[Rituals Auth]
</pre>
<p>Periodically verify token and re-authenticate if expired.</p>
<h4>Example 3: Startup Validation</h4>
<pre>
[Inject: once on start] → [Token Check] → [Dashboard: "Ready"]
↓ (invalid)
[Rituals Auth] → [Token Check]
</pre>
<p>On Node-RED startup, check if stored token is still valid. If not, authenticate first.</p>
<h3>Token Expiry</h3>
<p>Rituals tokens are valid for <strong>24 hours</strong> from authentification. This node checks:</p>
<ul>
<li>If token exists in context storage</li>
<li>If token expiry time has passed</li>
<li>Time remaining until expiration</li>
</ul>
<h3>Context Storage</h3>
<p>This node reads from both flow and global context:</p>
<ul>
<li><code>flow.ritualsToken</code> or <code>global.ritualsToken</code></li>
<li><code>flow.ritualsTokenExpiry</code> or <code>global.ritualsTokenExpiry</code></li>
<li><code>flow.ritualsDevices</code> or <code>global.ritualsDevices</code></li>
</ul>
<h3>Status Indicators</h3>
<ul>
<li><span style="color: green;">● Green:</span> Token is valid (shows hours remaining)</li>
<li><span style="color: yellow;">◯ Yellow:</span> Token is expired</li>
<li><span style="color: red;">◯ Red:</span> No token found</li>
</ul>
<h3>Function Node Alternative</h3>
<p>You can also check token validity in a function node:</p>
<pre>
const token = global.get('ritualsToken');
const expiry = global.get('ritualsTokenExpiry');
if (!token) {
msg.payload = { valid: false, error: 'No token' };
return [null, msg]; // Invalid output
}
if (Date.now() > expiry) {
msg.payload = { valid: false, error: 'Expired' };
return [null, msg]; // Invalid output
}
msg.payload = { valid: true };
return [msg, null]; // Valid output
</pre>
<h3>Tips</h3>
<ul>
<li>Use this node before every controller operation to ensure valid token</li>
<li>Set up automatic re-authentification on the "invalid" output</li>
<li>Check token on Node-RED startup to validate persistent storage</li>
<li>Monitor the status indicator to see token health at a glance</li>
</ul>
</script>