nuabase 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/DEVELOPMENT.md +5 -0
- data/LICENSE +21 -0
- data/README.md +102 -0
- data/Rakefile +4 -0
- data/lib/nuabase/nua_token_generator.rb +53 -0
- data/lib/nuabase/version.rb +5 -0
- data/lib/nuabase.rb +9 -0
- data/sig/nuabase.rbs +4 -0
- metadata +66 -0
checksums.yaml
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
---
|
|
2
|
+
SHA256:
|
|
3
|
+
metadata.gz: f89d067530102ee8cdb37b20b324dca303ca41b10954259558d2e1c4d40d91b4
|
|
4
|
+
data.tar.gz: dccc82369f84feefd1282d278ab67cf133d7b5d4670da363a8f2a4ae64bf66ce
|
|
5
|
+
SHA512:
|
|
6
|
+
metadata.gz: a2b1cdab5555cb04011e6386270cb3436e684b7271da0c73c8303a9f8cc294621a87ec0b7333512ba6f36d967be1eeea4232f40d220d44fc05fba21b5919a89e
|
|
7
|
+
data.tar.gz: 1bb7801e7d67105076f11ca8562fe956d28b72453c3c0a0610326653d36646f06e30a01ccbe2e06e3fffbfa9d9aeba345b738325edde0fe85acea309ea01c8b7
|
data/DEVELOPMENT.md
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
## Development
|
|
2
|
+
|
|
3
|
+
After checking out the repo, run `bin/setup` to install dependencies. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
|
4
|
+
|
|
5
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
data/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 Nuabase
|
|
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,102 @@
|
|
|
1
|
+
# Nuabase
|
|
2
|
+
|
|
3
|
+
## Installation
|
|
4
|
+
|
|
5
|
+
Install the gem and add to the application's Gemfile by executing:
|
|
6
|
+
|
|
7
|
+
$ bundle add nuabase
|
|
8
|
+
|
|
9
|
+
## Usage
|
|
10
|
+
|
|
11
|
+
### Prerequisites
|
|
12
|
+
|
|
13
|
+
Obtain a Signing Key Secret from the [Nuabase Console](https://console.nuabase.com/dashboard/signing-keys/new).
|
|
14
|
+
|
|
15
|
+
This key is a secret and must be stored securely on your backend server. It must **not** be exposed to the client-side code. We recommend storing it as an encrypted Rails credential or as an environment variable named `NUABASE_SIGNING_KEY_SECRET`.
|
|
16
|
+
|
|
17
|
+
The Signing Key Secret is used by your backend to generate short-lived JWT tokens via this SDK. The typical workflow is:
|
|
18
|
+
|
|
19
|
+
1. Expose an endpoint on your backend (e.g., `POST /.well-known/nuabase/token`).
|
|
20
|
+
2. **IMPORTANT**: This endpoint MUST be authenticated. You must verify the user's identity before generating a token. Do not expose this endpoint publicly.
|
|
21
|
+
3. Your frontend, loaded by an authenticated user, calls this endpoint.
|
|
22
|
+
4. Your backend uses the Nuabase SDK to generate a token for that specific user.
|
|
23
|
+
5. The frontend receives the token and uses it to directly make authenticated LLM calls to the Nuabase server, using the [Nuabase TypeScript SDK](https://github.com/nuabase/ts-sdk).
|
|
24
|
+
|
|
25
|
+
### Basic Usage
|
|
26
|
+
|
|
27
|
+
```ruby
|
|
28
|
+
require 'nuabase'
|
|
29
|
+
|
|
30
|
+
# Initialize the generator with your signing key secret and the user ID
|
|
31
|
+
generator = Nuabase::NuaTokenGenerator.new(
|
|
32
|
+
signing_key_secret: 'pk_...', # Your Nuabase Signing Key Secret
|
|
33
|
+
user_id: 'user_123' # The ID of the user in your system
|
|
34
|
+
)
|
|
35
|
+
|
|
36
|
+
# Generate the token
|
|
37
|
+
token_data = generator.generate
|
|
38
|
+
|
|
39
|
+
# token_data is a Hash containing:
|
|
40
|
+
# {
|
|
41
|
+
# access_token: "eyJhbGci...",
|
|
42
|
+
# expires_in: 180,
|
|
43
|
+
# expires_at: 1732398765
|
|
44
|
+
# }
|
|
45
|
+
puts token_data[:access_token]
|
|
46
|
+
```
|
|
47
|
+
|
|
48
|
+
### Rails Integration
|
|
49
|
+
|
|
50
|
+
You can integrate Nuabase into your Rails application by creating a controller to serve the token.
|
|
51
|
+
|
|
52
|
+
1. Add the route in `config/routes.rb`:
|
|
53
|
+
|
|
54
|
+
```ruby
|
|
55
|
+
Rails.application.routes.draw do
|
|
56
|
+
# ... other routes
|
|
57
|
+
|
|
58
|
+
namespace :nuabase, path: ".well-known/nuabase", defaults: { format: :json } do
|
|
59
|
+
resource :token, only: :create
|
|
60
|
+
end
|
|
61
|
+
end
|
|
62
|
+
```
|
|
63
|
+
|
|
64
|
+
2. Create the controller `app/controllers/nuabase/tokens_controller.rb`:
|
|
65
|
+
|
|
66
|
+
```ruby
|
|
67
|
+
module Nuabase
|
|
68
|
+
class TokensController < ApplicationController
|
|
69
|
+
# IMPORTANT: Ensure the user is authenticated.
|
|
70
|
+
# Replace `authenticate_user!` with your application's authentication filter.
|
|
71
|
+
before_action :authenticate_user!
|
|
72
|
+
|
|
73
|
+
def create
|
|
74
|
+
# Replace `current_user` with whatever object stores the authenticated user in your app
|
|
75
|
+
token = Nuabase::NuaTokenGenerator.new(
|
|
76
|
+
user_id: current_user.id,
|
|
77
|
+
signing_key_secret: ENV['NUABASE_SIGNING_KEY_SECRET']
|
|
78
|
+
).generate
|
|
79
|
+
|
|
80
|
+
render json: token, status: :ok
|
|
81
|
+
end
|
|
82
|
+
end
|
|
83
|
+
end
|
|
84
|
+
```
|
|
85
|
+
|
|
86
|
+
### Token Expiration and Automatic Refresh
|
|
87
|
+
|
|
88
|
+
Tokens expire after 180 seconds by default. You can override the TTL by passing `expiry_seconds:` when instantiating `Nuabase::NuaTokenGenerator`:
|
|
89
|
+
|
|
90
|
+
```ruby
|
|
91
|
+
token_data = Nuabase::NuaTokenGenerator.new(
|
|
92
|
+
signing_key_secret: 'pk_...',
|
|
93
|
+
user_id: 'user_123',
|
|
94
|
+
expiry_seconds: 300 # token will last for 5 minutes
|
|
95
|
+
).generate
|
|
96
|
+
```
|
|
97
|
+
|
|
98
|
+
Keep the expiration short, to prevent abuse of leaked token. The Nuabase TypeScript SDK will automatically refresh the token when it expires.
|
|
99
|
+
|
|
100
|
+
## Contributing
|
|
101
|
+
|
|
102
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/nuabase/ruby-sdk
|
data/Rakefile
ADDED
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
require 'jwt'
|
|
2
|
+
require 'securerandom'
|
|
3
|
+
require "base64"
|
|
4
|
+
|
|
5
|
+
module Nuabase
|
|
6
|
+
class NuaTokenGenerator
|
|
7
|
+
class InvalidSecretKeyError < StandardError; end
|
|
8
|
+
|
|
9
|
+
SIGNING_KEY_PREFIX = "pk_".freeze
|
|
10
|
+
ALGORITHM = 'HS256'.freeze
|
|
11
|
+
EXPIRY_SECONDS = 180
|
|
12
|
+
|
|
13
|
+
def initialize(signing_key_secret:, user_id:, expiry_seconds: EXPIRY_SECONDS)
|
|
14
|
+
@user_id = user_id
|
|
15
|
+
@expiry_seconds = expiry_seconds
|
|
16
|
+
parse_signing_key(signing_key_secret)
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
def generate
|
|
20
|
+
current_time = Time.now.to_i
|
|
21
|
+
|
|
22
|
+
payload = {
|
|
23
|
+
iss: @client_id,
|
|
24
|
+
sub: @user_id,
|
|
25
|
+
kid: @key_id,
|
|
26
|
+
jti: SecureRandom.uuid,
|
|
27
|
+
exp: current_time + @expiry_seconds,
|
|
28
|
+
iat: current_time,
|
|
29
|
+
aud: "https://api.nuabase.com"
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
token = JWT.encode(payload, @secret_b64, ALGORITHM)
|
|
33
|
+
|
|
34
|
+
{
|
|
35
|
+
access_token: token,
|
|
36
|
+
expires_in: @expiry_seconds,
|
|
37
|
+
expires_at: payload[:exp]
|
|
38
|
+
}
|
|
39
|
+
end
|
|
40
|
+
|
|
41
|
+
private
|
|
42
|
+
|
|
43
|
+
def parse_signing_key(signing_key_secret)
|
|
44
|
+
raise InvalidSecretKeyError, "invalid Nuabase token secret" unless signing_key_secret.start_with?(SIGNING_KEY_PREFIX)
|
|
45
|
+
body = signing_key_secret[SIGNING_KEY_PREFIX.length..] # strip "pk_"
|
|
46
|
+
parts = body.split(".", 3)
|
|
47
|
+
raise InvalidSecretKeyError, "invalid API key format" unless parts.size == 3
|
|
48
|
+
client_id_b64, key_id_b64, @secret_b64 = parts
|
|
49
|
+
@client_id = Base64.urlsafe_decode64(client_id_b64)
|
|
50
|
+
@key_id = Base64.urlsafe_decode64(key_id_b64)
|
|
51
|
+
end
|
|
52
|
+
end
|
|
53
|
+
end
|
data/lib/nuabase.rb
ADDED
data/sig/nuabase.rbs
ADDED
metadata
ADDED
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: nuabase
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
version: 0.1.0
|
|
5
|
+
platform: ruby
|
|
6
|
+
authors:
|
|
7
|
+
- Jasim A Basheer
|
|
8
|
+
autorequire:
|
|
9
|
+
bindir: exe
|
|
10
|
+
cert_chain: []
|
|
11
|
+
date: 2025-11-23 00:00:00.000000000 Z
|
|
12
|
+
dependencies:
|
|
13
|
+
- !ruby/object:Gem::Dependency
|
|
14
|
+
name: jwt
|
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
|
16
|
+
requirements:
|
|
17
|
+
- - ">="
|
|
18
|
+
- !ruby/object:Gem::Version
|
|
19
|
+
version: '0'
|
|
20
|
+
type: :runtime
|
|
21
|
+
prerelease: false
|
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
23
|
+
requirements:
|
|
24
|
+
- - ">="
|
|
25
|
+
- !ruby/object:Gem::Version
|
|
26
|
+
version: '0'
|
|
27
|
+
description: Ruby SDK for Nuabase
|
|
28
|
+
email:
|
|
29
|
+
- jasim@protoship.io
|
|
30
|
+
executables: []
|
|
31
|
+
extensions: []
|
|
32
|
+
extra_rdoc_files: []
|
|
33
|
+
files:
|
|
34
|
+
- DEVELOPMENT.md
|
|
35
|
+
- LICENSE
|
|
36
|
+
- README.md
|
|
37
|
+
- Rakefile
|
|
38
|
+
- lib/nuabase.rb
|
|
39
|
+
- lib/nuabase/nua_token_generator.rb
|
|
40
|
+
- lib/nuabase/version.rb
|
|
41
|
+
- sig/nuabase.rbs
|
|
42
|
+
homepage: https://github.com/nuabase/ruby-sdk
|
|
43
|
+
licenses: []
|
|
44
|
+
metadata:
|
|
45
|
+
homepage_uri: https://github.com/nuabase/ruby-sdk
|
|
46
|
+
source_code_uri: https://github.com/nuabase/ruby-sdk
|
|
47
|
+
post_install_message:
|
|
48
|
+
rdoc_options: []
|
|
49
|
+
require_paths:
|
|
50
|
+
- lib
|
|
51
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
|
52
|
+
requirements:
|
|
53
|
+
- - ">="
|
|
54
|
+
- !ruby/object:Gem::Version
|
|
55
|
+
version: 3.0.0
|
|
56
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
57
|
+
requirements:
|
|
58
|
+
- - ">="
|
|
59
|
+
- !ruby/object:Gem::Version
|
|
60
|
+
version: '0'
|
|
61
|
+
requirements: []
|
|
62
|
+
rubygems_version: 3.5.17
|
|
63
|
+
signing_key:
|
|
64
|
+
specification_version: 4
|
|
65
|
+
summary: Nuabase Ruby SDK
|
|
66
|
+
test_files: []
|