ottertrail 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/LICENSE +21 -0
- data/README.md +144 -0
- data/lib/ottertrail/version.rb +5 -0
- data/lib/ottertrail.rb +105 -0
- data/sig/ottertrail.rbs +21 -0
- metadata +51 -0
checksums.yaml
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
---
|
|
2
|
+
SHA256:
|
|
3
|
+
metadata.gz: aa5db014faa43fb8e579efc1d72039e3b3abdb837b598e1b315d0029b0c2cdd6
|
|
4
|
+
data.tar.gz: d901e97fc0eb1ed16838400a766a9ba7f35bbcb43736dc2c23cb606299214968
|
|
5
|
+
SHA512:
|
|
6
|
+
metadata.gz: 825e6b0ea42f83860c593232ee934c95929369f11a18e67019d3d4e890c6bc673d7c3d8fc98e3761a3dfaa62cb35dd4046f51fa3e7ab2f32baba9fb5e522f0c3
|
|
7
|
+
data.tar.gz: d10cc4c25fea5d6ca544bad103ba1b161ce6c106e554d7968906add6122e4fe0b63775f9746ba6cdc878af01617a8212781aa9ced307d01706a29ed9d44e4fc6
|
data/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 The Ottertrail authors
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
data/README.md
ADDED
|
@@ -0,0 +1,144 @@
|
|
|
1
|
+
# ottertrail
|
|
2
|
+
|
|
3
|
+
The Ruby SDK for [Ottertrail](https://ottertrail.dev), web analytics and error tracking.
|
|
4
|
+
|
|
5
|
+
Version 0.1 has one method, `Ottertrail.sign_user_id`. It signs the ID of a signed-in user on your server, so that Ottertrail can trust that the ID came from you.
|
|
6
|
+
|
|
7
|
+
## Install
|
|
8
|
+
|
|
9
|
+
```sh
|
|
10
|
+
bundle add ottertrail
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
Or `gem install ottertrail`. It has no dependencies and includes RBS type signatures.
|
|
14
|
+
|
|
15
|
+
## Quick start
|
|
16
|
+
|
|
17
|
+
Ottertrail's tracker can attach a signed-in user's ID to events. Anyone with the project's public key can send any user ID. This SDK signs IDs on your server, so Ottertrail can check them.
|
|
18
|
+
|
|
19
|
+
1. In Ottertrail, open the project's settings. On the General tab, under Keys, press Show beside the secret key, and copy it.
|
|
20
|
+
2. Set `OTTERTRAIL_SECRET_KEY` in your server's environment. Replace `sk_...` with the key you copied, then start or restart your app. Keep the key out of browser code and out of source control:
|
|
21
|
+
|
|
22
|
+
```sh
|
|
23
|
+
export OTTERTRAIL_SECRET_KEY=sk_...
|
|
24
|
+
```
|
|
25
|
+
|
|
26
|
+
3. On your server, sign the signed-in user's ID when you render their page or return their sign-in details:
|
|
27
|
+
|
|
28
|
+
```ruby
|
|
29
|
+
require "ottertrail"
|
|
30
|
+
|
|
31
|
+
# Reads the key from OTTERTRAIL_SECRET_KEY.
|
|
32
|
+
signature = Ottertrail.sign_user_id(user.id)
|
|
33
|
+
```
|
|
34
|
+
|
|
35
|
+
4. Update the tracker snippet from the General tab in your page template. Replace `USER_ID` with the signed-in user's ID, and `SIGNATURE` with the signature from step 3. Use your template's HTML escaping for the ID. Leave out both attributes for visitors who are not signed in:
|
|
36
|
+
|
|
37
|
+
```html
|
|
38
|
+
<script defer src="https://your-instance.ottertrail.app/t.js" data-key="pk_..."
|
|
39
|
+
data-user-id="USER_ID" data-user-signature="SIGNATURE"></script>
|
|
40
|
+
```
|
|
41
|
+
|
|
42
|
+
In a single-page app, return the user ID and its signature from your server. Once the tracker has loaded, call `window.ottertrail?.identify(userId, signature)` after sign-in, and when a page load restores a signed-in session. Call `window.ottertrail?.reset()` at sign-out.
|
|
43
|
+
|
|
44
|
+
5. Open your site and sign in. The user shows on the project's Users page.
|
|
45
|
+
6. Once your site sends a signature with every user ID, open General in the project's settings. Under Signed user IDs, turn on "Only accept signed user IDs". Events with a missing or wrong signature are still stored, without the user ID.
|
|
46
|
+
7. To check it, sign in as a user who has not used the site before. The user shows on the Users page only when the signature is correct.
|
|
47
|
+
|
|
48
|
+
## `Ottertrail.sign_user_id(user_id, secret_key = nil)`
|
|
49
|
+
|
|
50
|
+
```ruby
|
|
51
|
+
require "ottertrail"
|
|
52
|
+
|
|
53
|
+
Ottertrail.sign_user_id(current_user.id) # => "f9ce1dc6..."
|
|
54
|
+
```
|
|
55
|
+
|
|
56
|
+
Returns the signature of `user_id` as 64 lower-case hex characters: the HMAC-SHA256 of the trimmed ID, keyed with the project's secret key.
|
|
57
|
+
|
|
58
|
+
- `user_id`: a `String`, or an `Integer` from -(2\*\*53 - 1) to 2\*\*53 - 1, which is signed as its decimal text. It is trimmed with the rules of JavaScript's `trim()`, as the tracker trims it. These differ from Ruby's `strip`. A string in another encoding is converted to UTF-8 first.
|
|
59
|
+
- `secret_key`: the project's secret key, `sk_` followed by 64 lower-case hex characters, from the project's settings. If you leave it out, the method reads `ENV["OTTERTRAIL_SECRET_KEY"]`.
|
|
60
|
+
|
|
61
|
+
Put the signature in the page beside the ID: in `data-user-signature` on the tracker's script tag, or as the second argument of `ottertrail.identify(id, signature)`.
|
|
62
|
+
|
|
63
|
+
After your site sends signatures, turn on "Only accept signed user IDs" in the project's settings. Events with a missing or wrong signature are still stored, without the user ID.
|
|
64
|
+
|
|
65
|
+
### Example with Sinatra
|
|
66
|
+
|
|
67
|
+
```ruby
|
|
68
|
+
require "sinatra"
|
|
69
|
+
require "ottertrail"
|
|
70
|
+
|
|
71
|
+
get "/" do
|
|
72
|
+
# Your own sign-in sets current_user.
|
|
73
|
+
identity = ""
|
|
74
|
+
if current_user
|
|
75
|
+
id = Rack::Utils.escape_html(current_user.id.to_s)
|
|
76
|
+
signature = Ottertrail.sign_user_id(current_user.id)
|
|
77
|
+
identity = %( data-user-id="#{id}" data-user-signature="#{signature}")
|
|
78
|
+
end
|
|
79
|
+
|
|
80
|
+
<<~HTML
|
|
81
|
+
<!doctype html>
|
|
82
|
+
<html>
|
|
83
|
+
<head>
|
|
84
|
+
<script defer src="https://your-instance.ottertrail.app/t.js" data-key="pk_..."#{identity}></script>
|
|
85
|
+
</head>
|
|
86
|
+
<body>Hello</body>
|
|
87
|
+
</html>
|
|
88
|
+
HTML
|
|
89
|
+
end
|
|
90
|
+
```
|
|
91
|
+
|
|
92
|
+
Run it with the key in the environment:
|
|
93
|
+
|
|
94
|
+
```sh
|
|
95
|
+
OTTERTRAIL_SECRET_KEY=sk_... ruby app.rb
|
|
96
|
+
```
|
|
97
|
+
|
|
98
|
+
### Example with Rails
|
|
99
|
+
|
|
100
|
+
Add a helper, and put `<%= ottertrail_tag %>` in the `<head>` of your layout:
|
|
101
|
+
|
|
102
|
+
```ruby
|
|
103
|
+
module ApplicationHelper
|
|
104
|
+
def ottertrail_tag
|
|
105
|
+
data = {key: "pk_..."}
|
|
106
|
+
if current_user
|
|
107
|
+
data[:user_id] = current_user.id
|
|
108
|
+
data[:user_signature] = Ottertrail.sign_user_id(current_user.id)
|
|
109
|
+
end
|
|
110
|
+
tag.script(defer: true, src: "https://your-instance.ottertrail.app/t.js", data:)
|
|
111
|
+
end
|
|
112
|
+
end
|
|
113
|
+
```
|
|
114
|
+
|
|
115
|
+
Set `OTTERTRAIL_SECRET_KEY` where your app runs, or pass the key from your credentials: `Ottertrail.sign_user_id(current_user.id, Rails.application.credentials.ottertrail_secret_key)`.
|
|
116
|
+
|
|
117
|
+
### Keep the secret key on the server
|
|
118
|
+
|
|
119
|
+
Anyone who has the secret key can send events with any user ID. Use it only in server code. Never put it in a page, in JavaScript that runs in a browser, or in a public repository. The public key (`pk_...`) is the one that goes in the page.
|
|
120
|
+
|
|
121
|
+
### Errors
|
|
122
|
+
|
|
123
|
+
`Ottertrail.sign_user_id` raises a `TypeError` when:
|
|
124
|
+
|
|
125
|
+
- the user ID is not a `String` or an `Integer`: `true`, `false`, and floats such as `42.0` are refused,
|
|
126
|
+
- the secret key is not a `String`.
|
|
127
|
+
|
|
128
|
+
It raises an `ArgumentError` when:
|
|
129
|
+
|
|
130
|
+
- an `Integer` ID is outside -(2\*\*53 - 1) to 2\*\*53 - 1, the integers that JavaScript holds exactly,
|
|
131
|
+
- the ID is not valid UTF-8, or cannot be converted to UTF-8, such as a binary string with bytes that are not ASCII (call `force_encoding(Encoding::UTF_8)` on a binary string that holds UTF-8),
|
|
132
|
+
- the trimmed ID is empty or longer than 200 characters, counted as JavaScript counts them: a character above U+FFFF, such as an emoji, counts 2,
|
|
133
|
+
- the ID has a control character, such as a line break,
|
|
134
|
+
- there is no secret key, or it is not `sk_` followed by 64 lower-case hex characters.
|
|
135
|
+
|
|
136
|
+
The error message never contains the secret key.
|
|
137
|
+
|
|
138
|
+
## Ruby versions
|
|
139
|
+
|
|
140
|
+
Ruby 3.3 and later. Each version is tested on Ruby 3.3, 3.4, and 4.0.
|
|
141
|
+
|
|
142
|
+
## License
|
|
143
|
+
|
|
144
|
+
[MIT](LICENSE)
|
data/lib/ottertrail.rb
ADDED
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "openssl"
|
|
4
|
+
|
|
5
|
+
require_relative "ottertrail/version"
|
|
6
|
+
|
|
7
|
+
# The Ruby SDK for Ottertrail.
|
|
8
|
+
module Ottertrail
|
|
9
|
+
SECRET_KEY_VARIABLE = "OTTERTRAIL_SECRET_KEY"
|
|
10
|
+
SECRET_KEY = /\Ask_[0-9a-f]{64}\z/
|
|
11
|
+
MAX_USER_ID = 200
|
|
12
|
+
MAX_SAFE_INTEGER = (2**53) - 1
|
|
13
|
+
# The characters that JavaScript's String.prototype.trim removes, and no others: Ruby's strip
|
|
14
|
+
# removes NUL too, and keeps the Unicode spaces.
|
|
15
|
+
NOT_JS_WHITESPACE = /[^\t\n\v\f\r \u00A0\u1680\u2000-\u200A\u2028\u2029\u202F\u205F\u3000\uFEFF]/
|
|
16
|
+
CONTROL_OR_SURROGATE = /[\p{Cc}\p{Cs}]/
|
|
17
|
+
private_constant :SECRET_KEY_VARIABLE, :SECRET_KEY, :MAX_USER_ID, :MAX_SAFE_INTEGER,
|
|
18
|
+
:NOT_JS_WHITESPACE, :CONTROL_OR_SURROGATE
|
|
19
|
+
|
|
20
|
+
class << self
|
|
21
|
+
# Signs the ID of a signed-in user, so that Ottertrail can trust it.
|
|
22
|
+
#
|
|
23
|
+
# Put the signature in the page beside the ID: in +data-user-signature+ on the tracker's script
|
|
24
|
+
# tag, or as the second argument of +ottertrail.identify(id, signature)+.
|
|
25
|
+
#
|
|
26
|
+
# @param user_id [String, Integer] The user's ID: a string, or an integer from -(2**53 - 1) to
|
|
27
|
+
# 2**53 - 1. It is trimmed, as the tracker trims it.
|
|
28
|
+
# @param secret_key [String, nil] The project's secret key. Defaults to
|
|
29
|
+
# +ENV["OTTERTRAIL_SECRET_KEY"]+.
|
|
30
|
+
# @return [String] The HMAC-SHA256 of the trimmed ID, as 64 lower-case hex characters.
|
|
31
|
+
# @raise [TypeError] If the ID is not a String or an Integer, or the key is not a String.
|
|
32
|
+
# @raise [ArgumentError] If the ID or the key is not valid.
|
|
33
|
+
def sign_user_id(user_id, secret_key = nil)
|
|
34
|
+
id = clean_user_id(user_id)
|
|
35
|
+
key = clean_secret_key(secret_key.nil? ? ENV[SECRET_KEY_VARIABLE] : secret_key)
|
|
36
|
+
OpenSSL::HMAC.hexdigest("SHA256", key, id).force_encoding(Encoding::UTF_8)
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
private
|
|
40
|
+
|
|
41
|
+
def clean_user_id(user_id)
|
|
42
|
+
text = case user_id
|
|
43
|
+
when String then utf8(user_id)
|
|
44
|
+
when Integer
|
|
45
|
+
unless user_id.between?(-MAX_SAFE_INTEGER, MAX_SAFE_INTEGER)
|
|
46
|
+
raise ArgumentError,
|
|
47
|
+
"Ottertrail: a user ID that is an integer must be a safe integer, from -(2**53 - 1) to 2**53 - 1."
|
|
48
|
+
end
|
|
49
|
+
user_id.to_s
|
|
50
|
+
else
|
|
51
|
+
raise TypeError, "Ottertrail: the user ID must be a string or an integer."
|
|
52
|
+
end
|
|
53
|
+
# The tracker trims the ID before it sends it, so the trimmed text is what the instance checks.
|
|
54
|
+
id = js_trim(text)
|
|
55
|
+
if id.empty? || utf16_length(id) > MAX_USER_ID
|
|
56
|
+
raise ArgumentError,
|
|
57
|
+
"Ottertrail: the user ID must be 1 to #{MAX_USER_ID} characters after trimming."
|
|
58
|
+
end
|
|
59
|
+
if CONTROL_OR_SURROGATE.match?(id)
|
|
60
|
+
raise ArgumentError,
|
|
61
|
+
"Ottertrail: the user ID must not contain control characters or unpaired surrogates."
|
|
62
|
+
end
|
|
63
|
+
id
|
|
64
|
+
end
|
|
65
|
+
|
|
66
|
+
def utf8(text)
|
|
67
|
+
converted = (text.encoding == Encoding::UTF_8) ? text : text.encode(Encoding::UTF_8)
|
|
68
|
+
return converted if converted.valid_encoding?
|
|
69
|
+
|
|
70
|
+
raise ArgumentError, "Ottertrail: the user ID must be valid UTF-8."
|
|
71
|
+
rescue EncodingError
|
|
72
|
+
raise ArgumentError, "Ottertrail: the user ID must be valid UTF-8."
|
|
73
|
+
end
|
|
74
|
+
|
|
75
|
+
# Searches for the first and the last other character, which takes linear time on any input.
|
|
76
|
+
def js_trim(text)
|
|
77
|
+
first = text.index(NOT_JS_WHITESPACE)
|
|
78
|
+
return "" if first.nil?
|
|
79
|
+
|
|
80
|
+
text[first..text.rindex(NOT_JS_WHITESPACE)] || ""
|
|
81
|
+
end
|
|
82
|
+
|
|
83
|
+
# JavaScript's length: a character above U+FFFF counts 2.
|
|
84
|
+
def utf16_length(text)
|
|
85
|
+
return text.length if text.length > MAX_USER_ID
|
|
86
|
+
|
|
87
|
+
text.encode(Encoding::UTF_16LE).bytesize / 2
|
|
88
|
+
end
|
|
89
|
+
|
|
90
|
+
# The messages never include the value: it is a secret.
|
|
91
|
+
def clean_secret_key(secret_key)
|
|
92
|
+
if secret_key.nil? || secret_key == ""
|
|
93
|
+
raise ArgumentError,
|
|
94
|
+
"Ottertrail: no secret key. Pass it to Ottertrail.sign_user_id, or set OTTERTRAIL_SECRET_KEY."
|
|
95
|
+
end
|
|
96
|
+
raise TypeError, "Ottertrail: the secret key must be a string." unless secret_key.is_a?(String)
|
|
97
|
+
# ascii_only? is false for bytes that are not valid, so the match never raises an error that
|
|
98
|
+
# could quote the key.
|
|
99
|
+
return secret_key if secret_key.ascii_only? && SECRET_KEY.match?(secret_key)
|
|
100
|
+
|
|
101
|
+
raise ArgumentError,
|
|
102
|
+
"Ottertrail: the secret key is not valid. Use the project's secret key: sk_ followed by 64 lower-case hex characters."
|
|
103
|
+
end
|
|
104
|
+
end
|
|
105
|
+
end
|
data/sig/ottertrail.rbs
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
module Ottertrail
|
|
2
|
+
VERSION: String
|
|
3
|
+
|
|
4
|
+
# Signs the ID of a signed-in user, so that Ottertrail can trust it.
|
|
5
|
+
def self.sign_user_id: (String | Integer user_id, ?String? secret_key) -> String
|
|
6
|
+
|
|
7
|
+
# Private: what the type check of lib/ needs.
|
|
8
|
+
|
|
9
|
+
SECRET_KEY_VARIABLE: String
|
|
10
|
+
SECRET_KEY: Regexp
|
|
11
|
+
MAX_USER_ID: Integer
|
|
12
|
+
MAX_SAFE_INTEGER: Integer
|
|
13
|
+
NOT_JS_WHITESPACE: Regexp
|
|
14
|
+
CONTROL_OR_SURROGATE: Regexp
|
|
15
|
+
|
|
16
|
+
private def self.clean_user_id: (untyped user_id) -> String
|
|
17
|
+
private def self.utf8: (String text) -> String
|
|
18
|
+
private def self.js_trim: (String text) -> String
|
|
19
|
+
private def self.utf16_length: (String text) -> Integer
|
|
20
|
+
private def self.clean_secret_key: (untyped secret_key) -> String
|
|
21
|
+
end
|
metadata
ADDED
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: ottertrail
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
version: 0.1.0
|
|
5
|
+
platform: ruby
|
|
6
|
+
authors:
|
|
7
|
+
- The Ottertrail authors
|
|
8
|
+
bindir: bin
|
|
9
|
+
cert_chain: []
|
|
10
|
+
date: 1980-01-02 00:00:00.000000000 Z
|
|
11
|
+
dependencies: []
|
|
12
|
+
description: Signs the IDs of signed-in users with a project's secret key, so that
|
|
13
|
+
Ottertrail can trust the user IDs that a site's tracker sends.
|
|
14
|
+
executables: []
|
|
15
|
+
extensions: []
|
|
16
|
+
extra_rdoc_files: []
|
|
17
|
+
files:
|
|
18
|
+
- LICENSE
|
|
19
|
+
- README.md
|
|
20
|
+
- lib/ottertrail.rb
|
|
21
|
+
- lib/ottertrail/version.rb
|
|
22
|
+
- sig/ottertrail.rbs
|
|
23
|
+
homepage: https://github.com/ottertrail/ottertrail-ruby
|
|
24
|
+
licenses:
|
|
25
|
+
- MIT
|
|
26
|
+
metadata:
|
|
27
|
+
homepage_uri: https://github.com/ottertrail/ottertrail-ruby
|
|
28
|
+
source_code_uri: https://github.com/ottertrail/ottertrail-ruby
|
|
29
|
+
bug_tracker_uri: https://github.com/ottertrail/ottertrail-ruby/issues
|
|
30
|
+
changelog_uri: https://github.com/ottertrail/ottertrail-ruby/releases
|
|
31
|
+
documentation_uri: https://github.com/ottertrail/ottertrail-ruby#readme
|
|
32
|
+
allowed_push_host: https://rubygems.org
|
|
33
|
+
rubygems_mfa_required: 'true'
|
|
34
|
+
rdoc_options: []
|
|
35
|
+
require_paths:
|
|
36
|
+
- lib
|
|
37
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
|
38
|
+
requirements:
|
|
39
|
+
- - ">="
|
|
40
|
+
- !ruby/object:Gem::Version
|
|
41
|
+
version: '3.3'
|
|
42
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
43
|
+
requirements:
|
|
44
|
+
- - ">="
|
|
45
|
+
- !ruby/object:Gem::Version
|
|
46
|
+
version: '0'
|
|
47
|
+
requirements: []
|
|
48
|
+
rubygems_version: 4.0.20
|
|
49
|
+
specification_version: 4
|
|
50
|
+
summary: 'Ruby SDK for Ottertrail: sign user IDs on your server.'
|
|
51
|
+
test_files: []
|