Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 25 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ that logs in to SQRL-enabled websites from scripts and test suites.
| Gem | Version | Downloads | Require | Use it when | Quick start |
| --- | --- | --- | --- | --- | --- |
| [`sqrl-core`](gems/sqrl-core) | [![Gem Version](https://img.shields.io/gem/v/sqrl-core?logo=rubygems&logoColor=white)](https://rubygems.org/gems/sqrl-core) | [![Downloads](https://img.shields.io/gem/dt/sqrl-core)](https://rubygems.org/gems/sqrl-core) | `require "sqrl/core"` (or `"sqrl"`) | you need the protocol, the cryptography or the identity formats directly | `SQRL::Session`, `SQRL::Server::Handler`, `SQRL::S4Identity` |
| [`sqrl-rack`](gems/sqrl-rack) | [![Gem Version](https://img.shields.io/gem/v/sqrl-rack?logo=rubygems&logoColor=white)](https://rubygems.org/gems/sqrl-rack) | [![Downloads](https://img.shields.io/gem/dt/sqrl-rack)](https://rubygems.org/gems/sqrl-rack) | `require "sqrl/rack"` | a Rack application or Sinatra | `use SQRL::Rack::Middleware` / `register Sinatra::SQRL` |
| [`sqrl-rack`](gems/sqrl-rack) | [![Gem Version](https://img.shields.io/gem/v/sqrl-rack?logo=rubygems&logoColor=white)](https://rubygems.org/gems/sqrl-rack) | [![Downloads](https://img.shields.io/gem/dt/sqrl-rack)](https://rubygems.org/gems/sqrl-rack) | `require "sqrl/rack"` | Rack, Sinatra or Roda | `use SQRL::Rack::Middleware` / `register Sinatra::SQRL` / `plugin :sqrl` |
| [`omniauth-sqrl`](gems/omniauth-sqrl) | [![Gem Version](https://img.shields.io/gem/v/omniauth-sqrl?logo=rubygems&logoColor=white)](https://rubygems.org/gems/omniauth-sqrl) | [![Downloads](https://img.shields.io/gem/dt/omniauth-sqrl)](https://rubygems.org/gems/omniauth-sqrl) | `require "omniauth/sqrl"` | you already use OmniAuth | `provider :sqrl` |
| [`sqrl-rails`](gems/sqrl-rails) | [![Gem Version](https://img.shields.io/gem/v/sqrl-rails?logo=rubygems&logoColor=white)](https://rubygems.org/gems/sqrl-rails) | [![Downloads](https://img.shields.io/gem/dt/sqrl-rails)](https://rubygems.org/gems/sqrl-rails) | `require "sqrl/rails"` | Rails, including the Rails 8 `generate authentication` stack | `bin/rails generate sqrl:install` |
| [`devise-sqrl`](gems/devise-sqrl) | [![Gem Version](https://img.shields.io/gem/v/devise-sqrl?logo=rubygems&logoColor=white)](https://rubygems.org/gems/devise-sqrl) | [![Downloads](https://img.shields.io/gem/dt/devise-sqrl)](https://rubygems.org/gems/devise-sqrl) | `require "devise/sqrl"` | Devise | `devise :sqrl_authenticatable` |
Expand All @@ -50,10 +50,15 @@ All six gems share one version and live in this repository. Per-framework guides
test vector. Everything is built on Ruby's bundled `openssl` gem; the core has no other
runtime dependency.
- A server engine whose state sits behind nut, identity and pending-login store interfaces
(in-memory and ActiveRecord implementations), with nut single use, IP matching, browser
session binding and one-time CPS tokens.
(in-memory, Redis, ActiveRecord and `ActiveSupport::Cache` implementations, all checked by
one shared store-contract spec), with nut single use, IP matching, browser session
binding, one-time CPS tokens, rate-limit and prune hooks and host allow-listing.
- Account policies surfaced to applications: `sqrlonly` refuses password login and
`hardlock` refuses password recovery in the Rails 8 and Devise integrations, and users can
list and unlink their SQRL identities.
- A browser script (`sqrl.js`) that performs the standard same-device probe on
`localhost:25519` and polls for logins from another device, plus server-side QR codes.
`localhost:25519`, polls for logins from another device and fetches a fresh code when one
expires, plus server-side QR codes.
- A bot client with login-page discovery, a cookie-carrying transport, a Capybara helper, a
command-line tool and a 23-case conformance suite.

Expand Down Expand Up @@ -82,13 +87,25 @@ your own server.
```sh
bundle install
bundle exec rake spec:gems # every gem's specs
bundle exec rake spec:integration # boots each example app and drives it with the bot client and a headless browser
bundle exec rspec spec/interop # the conformance suite against the Rust reference server (needs its binary)
bundle exec rake spec:integration # boots each example app over HTTPS and drives it with the bot client and a headless browser
bundle exec rspec spec/interop # the conformance suite against the Rust reference server
bundle exec rubocop # lint (enforced in CI)
bundle exec rake build # packages the six gems into pkg/; `rake release_check` lists them
```

Each application under [`examples/`](examples) is a complete, independently bundled
application of its framework (`bin/dev` starts it). The integration specs start every one of
them on a free port and run the conformance suite, bot flows and browser flows against it.
application of its framework (`bin/dev` starts it). Run `bundle install` inside each one
before the integration specs: they start every example on a free port with its own bundle
and run the conformance suite, bot flows and browser flows against it, twice (the second
time with `ask` and `sin` enabled).

What the suites need: a Chromium binary for the browser flows (`BROWSER_PATH`, or the
Playwright Chromium that CI installs), the Rust toolchain and a checkout of
`SQRL-extension` for the interop run (`SQRL_RUST_TEST_SERVER` points at the built
`sqrl-test-server`; the specs skip without it), and optionally a local `redis-server` for
the Redis store specs (they also run against an in-process double). `gemfiles/` holds the
Rails 7.1, 7.2 and 8.0 bundles that CI runs `sqrl-rails` and `devise-sqrl` against.
Deferred work and ideas are tracked as GitHub issues.

Ruby 3.1 or newer for `sqrl-core`, `sqrl-rack`, `omniauth-sqrl` and `sqrl-client`;
Ruby 3.2 or newer for `sqrl-rails` and `devise-sqrl`. One EnScrypt iteration takes about
Expand Down
6 changes: 5 additions & 1 deletion Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,11 @@ task :build do
end
end

desc "Check that every gem builds cleanly and that the tag would match the version"
desc "Check that every gem builds cleanly and, when GITHUB_REF_NAME is set, that the tag matches SQRL::VERSION"
task release_check: :build do
Dir["pkg/*.gem"].each { |g| puts "built #{g}" }
tag = ENV.fetch("GITHUB_REF_NAME", nil)
if tag && tag != "v#{SQRL::VERSION}"
abort "tag #{tag} does not match SQRL::VERSION #{SQRL::VERSION}"
end
end
28 changes: 19 additions & 9 deletions docs/integrations/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ applications hold runnable code.

| Framework | Guide | Gem README | Example application |
| --- | --- | --- | --- |
| Rack and Sinatra | [rack-sinatra.md](rack-sinatra.md) | [gems/sqrl-rack](../../gems/sqrl-rack/README.md) | [examples/rack-app](../../examples/rack-app), [examples/sinatra-app](../../examples/sinatra-app) |
| Rack, Sinatra and Roda | [rack-sinatra.md](rack-sinatra.md) | [gems/sqrl-rack](../../gems/sqrl-rack/README.md) | [examples/rack-app](../../examples/rack-app), [examples/sinatra-app](../../examples/sinatra-app), [examples/roda-app](../../examples/roda-app) |
| OmniAuth | [omniauth.md](omniauth.md) | [gems/omniauth-sqrl](../../gems/omniauth-sqrl/README.md) | [examples/omniauth-app](../../examples/omniauth-app) |
| Rails and Rails 8 Authentication | [rails.md](rails.md) | [gems/sqrl-rails](../../gems/sqrl-rails/README.md) | [examples/rails-auth-app](../../examples/rails-auth-app) |
| Devise | [devise.md](devise.md) | [gems/devise-sqrl](../../gems/devise-sqrl/README.md) | [examples/devise-app](../../examples/devise-app) |
Expand All @@ -25,19 +25,29 @@ applications hold runnable code.
4. Another device: `sqrl.js` polls the cookie-bound status endpoint and, once authenticated,
navigates to the callback, which completes that browser's own pending login once.
5. The framework adapter turns the authenticated identity into its own notion of a signed-in
user: a session key (Rack), an auth hash (OmniAuth), `start_new_session_for` (Rails 8),
or `sign_in` (Devise). Unknown identities create an account, link to the signed-in user,
or are refused, according to `on_unknown_identity`.
user: a session key (Rack, Sinatra, Roda), an auth hash (OmniAuth),
`start_new_session_for` (Rails 8), or `sign_in` (Devise). Unknown identities create an
account, link to the signed-in user, or are refused, according to `on_unknown_identity`.

When a nut expires before the user acts, the page's "Get a new code" button calls the
refresh endpoint and swaps in a fresh URL and QR code without a reload.

## Deployment notes that apply everywhere

- Set the public `host` explicitly in production; it is the string identities are derived
from, so a change breaks every existing SQRL association.
- Use a persistent, atomic store in production (the ActiveRecord stores from `sqrl-rails`,
or your own implementation of the three interfaces in `SQRL::Server`). The in-memory
stores are for development and tests.
- Use a persistent, atomic store in production: ActiveRecord or `ActiveSupport::Cache`
(`sqrl-rails`), Redis (`SQRL::Server::Redis` in `sqrl-core`), or your own implementation
of the three interfaces in `SQRL::Server`. The in-memory stores are for development and
tests.
- Serve HTTPS. SQRL URLs are always `sqrl://`; plain HTTP exists only as the loopback-only
`insecure_http` testing mode.
- The SQRL endpoint is exempt from CSRF protection by design: it is authenticated by
signatures. Nothing else is.
- Keep `require_ip_match` on. Clients behind a different egress than the browser must send
`noiptest`, which the user has to opt into.
- Prune expired nuts and pending logins (`sqrl:prune` in Rails; `Handler#prune!` elsewhere).
`noiptest`, which the user has to opt into. `poll_requires_ip_match` is a separate,
off-by-default hardening; see [docs/security.md](../security.md) before turning it on.
- Rate-limit the login page and the endpoint (`before_issue` / `before_handle` hooks,
Rails `rate_limit`, or Rack::Attack).
- Prune expired nuts and pending logins: `prune_every` does it opportunistically;
`sqrl:prune` (Rails) or `Handler#prune!` from a scheduler otherwise.
9 changes: 7 additions & 2 deletions docs/integrations/client.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,13 @@ agent.http.get("https://example.com/account") # same cookie jar, already logge
tests never need a rescue code or EnScrypt.
- `SQRL::Client::Capybara.login(page, identity:)` runs the protocol out of band and then has
the browser visit the CPS URL, so the browser session is the one logged in.
- The `sqrl-client` executable exposes the same operations and exports cookies in Netscape
or JSON format for `curl` and friends.
- The `sqrl-client` executable exposes the same operations plus identity management
(`identity create|import|show|export|rekey|change-password|previous`), takes `--alt-id`,
and exports cookies in Netscape or JSON format for `curl` and friends.
- The agent speaks `sqrl://` over HTTPS only. `insecure: true` (CLI `--insecure`) is a
loopback-only testing mode for plain-HTTP sites and `qrl://` links such as the browser
extension's test server; a self-signed development certificate is trusted with
`Transport.new(ca_file:)` (CLI `--ca-file`), and `--no-verify-tls` disables verification.

## Conformance suite

Expand Down
21 changes: 16 additions & 5 deletions docs/integrations/devise.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@ bin/rails db:migrate

`devise_for :users` then provides `GET /users/sqrl` (a stand-alone login page),
`POST /users/sqrl/cli` (the SQRL endpoint, exempt from forgery protection),
`GET /users/sqrl/status`, `GET /users/sqrl/callback` and `GET /users/sqrl/sqrl.js`.
`GET /users/sqrl/status`, `GET /users/sqrl/callback`, `GET /users/sqrl/refresh`,
`GET /users/sqrl/sqrl.js`, and `GET /users/sqrl/identities` with
`DELETE /users/sqrl/identities/:id` for managing linked identities.
The generator appends `<%= render "devise/shared/sqrl", resource_name: resource_name %>`
to `devise/sessions/new.html.erb`, so the password form and the SQRL block live on the
same page.
Expand All @@ -34,14 +36,23 @@ rememberable and the other Devise hooks therefore behave exactly as for a passwo

| Option | Default | Meaning |
| --- | --- | --- |
| `config.sqrl_on_unknown_identity` | `:create` | `:create` a resource, `:link` to the signed-in resource, or `:refuse` |
| `config.sqrl_create_resource` | `SQRL::Rails::UserFactory` | `(identity, mapping) -> resource` |
| `config.sqrl_on_unknown_identity` | `nil` (defers to `config.sqrl.on_unknown_identity`, `:create`) | `:create` a resource, `:link` to the signed-in resource, or `:refuse` |
| `config.sqrl_create_resource` | `SQRL::Rails::UserFactory` | `(identity, resource_class) -> resource` |
| `config.sqrl_remember_me` | `false` | set the remember-me cookie on SQRL sign-in |
| `config.sqrl_login_block` | `{}` | options for the rendered login block |
| `config.sqrl_failure_path` | the SQRL login page | where a refused callback redirects |
| `config.sqrl_only_blocked_strategies` | `[:database_authenticatable]` | Warden strategies refused for `sqrlonly` users |

Protocol options (`host`, `require_ip_match`, `ask`, `sin`, stores) come from
`Rails.application.config.sqrl`, exactly as for `sqrl-rails`.
Protocol options (`host`, `insecure_http`, `require_ip_match`, `poll_requires_ip_match`,
`ask`, `sin`, `rate_limit`, hooks, stores) come from `Rails.application.config.sqrl`,
exactly as for `sqrl-rails`.

## Account policies

A user whose identity asked for `sqrlonly` can no longer sign in through Devise's password
strategy (`devise.failure.sqrl_only`); one whose identity asked for `hardlock` cannot
request or perform a password reset (`errors.messages.sqrl_hard_locked`). SQRL logins,
remember-me cookies and restored sessions are unaffected.

## Model API

Expand Down
10 changes: 7 additions & 3 deletions docs/integrations/omniauth.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,9 @@ end
- `POST /auth/sqrl` (the request phase) renders the login page. OmniAuth 2 requires POST and
an authenticity token here: Rails apps get it from `omniauth-rails_csrf_protection`,
Sinatra apps render `Rack::Protection::AuthenticityToken.token(session)` in the form.
- `POST /auth/sqrl/cli`, `GET /auth/sqrl/status` and `GET /auth/sqrl/sqrl.js` are served by
the strategy itself (OmniAuth's `other_phase`), so they never reach your controllers.
- `POST /auth/sqrl/cli`, `GET /auth/sqrl/status`, `GET /auth/sqrl/refresh` and
`GET /auth/sqrl/sqrl.js` are served by the strategy itself (OmniAuth's `other_phase`), so
they never reach your controllers.
- `GET /auth/sqrl/callback` builds the auth hash: `uid` is the identity key (base64url),
`info` is empty by design, and `extra["sqrl"]` carries `suk`, `vuk`, `created`,
`rekeyed_from`, `options`, `disabled`, `via` (`cps` or `poll`) and `user_ref`.
Expand All @@ -23,4 +24,7 @@ end
provider; `devise-sqrl` is the native alternative.

Always pass `stores:` in production. The default in-memory stores are process-wide and
exist only so development works without configuration.
exist only so development works without configuration. The strategy takes the same
protocol and hardening options as the Rack middleware (`require_ip_match`,
`poll_requires_ip_match`, `allowed_hosts`, `host_cache_limit`, `before_issue`,
`before_handle`, `prune_every`, `insecure_http`); the gem README lists them.
47 changes: 38 additions & 9 deletions docs/integrations/rack-sinatra.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Rack and Sinatra
# Rack, Sinatra and Roda

Gem: `sqrl-rack`. Examples: `examples/rack-app`, `examples/sinatra-app`.
Gem: `sqrl-rack`. Examples: `examples/rack-app`, `examples/sinatra-app`, `examples/roda-app`.

## Rack

Expand All @@ -13,11 +13,16 @@ use SQRL::Rack::Middleware,
after_login: "/"
```

The middleware owns `POST /sqrl/cli`, `GET /sqrl/status`, `GET /sqrl/callback` and
`GET /sqrl/sqrl.js`. Every other request receives `env["sqrl.server"]`, a facade with
`login_url`, `login_block`, `script_tag`, `qr_svg`, `identity`, `authenticated?` and
`logout!`. Without `on_login`, the identity key is stored in the session under
`sqrl.identity` and `authenticated?` reads it back.
The middleware owns `POST /sqrl/cli`, `GET /sqrl/status`, `GET /sqrl/callback`,
`GET /sqrl/refresh` and `GET /sqrl/sqrl.js`. Every other request receives
`env["sqrl.server"]`, a facade with `login_url`, `login_block`, `script_tag`, `qr_svg`,
`refresh_url`, `identity`, `authenticated?` and `logout!`. Without `on_login`, the identity
key is stored in the session under `sqrl.identity` and `authenticated?` reads it back.

Every `SQRL::Server::Config` option passes through (`ask`, `sin`, `require_ip_match`,
`allowed_hosts`, `before_issue`, `before_handle`, `prune_every`, …), plus the middleware's
own `poll_requires_ip_match` (default off), `host_cache_limit`, `failure_path` and
`insecure_http`. The gem README has the full tables.

## Sinatra

Expand All @@ -34,8 +39,32 @@ end
```

Helpers: `sqrl_login_url`, `sqrl_qr_svg`, `sqrl_script_tag`, `sqrl_login_block`,
`sqrl_authenticated?`, `current_sqrl_identity`, `sqrl_logout!`. `enable :sessions` must
come before `register`.
`sqrl_authenticated?`, `current_sqrl_identity`, `sqrl_logout!` and `sqrl` (the facade).
Sessions must be enabled; `set :sqrl` may come before or after `register` because the
options are read on the first request.

## Roda

```ruby
require "roda/plugins/sqrl"

class App < Roda
use Rack::Session::Cookie, secret: ENV.fetch("SESSION_SECRET"), same_site: :lax
plugin :sqrl, host: "example.com", stores: SQRL::Server::Memory.stores

sqrl_on_login { |identity, env| env["rack.session"]["user"] = identity.idk_b64 }

route do |r|
r.get("login") { sqrl_login_block }
r.root { sqrl_authenticated? ? "Hello #{current_sqrl_identity.idk_b64}" : r.redirect("/login") }
end
end
```

`plugin :sqrl` takes every middleware option and adds the same helpers as the Sinatra
extension. The session must come from a Rack session middleware added with `use` before the
plugin; Roda's own `sessions` plugin runs inside the routing tree, after the middleware, and
cannot be used for this. `roda` itself is not a dependency of `sqrl-rack`.

## Notes

Expand Down
Loading
Loading