oauth-device-flow

Version, currently 0.1.02 versions

github.com/aluminumio/oauth-device-flow.cr

OAuth 2.0 Device Authorization Grant (RFC 8628) client for Crystal CLIs. Full UX, auto-refresh, pluggable token store, zero runtime dependencies.

0 stars
1 dependent
License: MIT

Nothing has been indexed for 0.1.0 yet. The tag is recorded, its shard.yml has not been read, so the manifest and dependency list below are empty because they are unknown rather than because they are absent.

Installation

# Add this to your shard.yml
dependencies:
  oauth-device-flow:
    github: aluminumio/oauth-device-flow.cr
    version: ~> 0.1.0

Then run:

shards install

shard.yml

No shard.yml has been indexed for 0.1.0. You can read it on the repository.

Dependencies

Unknown: the shard.yml for this version has not been read yet.

README

This README is the one indexed from the repository at its latest ref, not from the tag for this version.

oauth-device-flow

OAuth 2.0 Device Authorization Grant (RFC 8628) for Crystal CLIs. Full UX, auto-refresh, pluggable token store, zero runtime dependencies.

Install

# shard.yml
dependencies:
  oauth-device-flow:
    github: aluminumio/oauth-device-flow.cr
    version: ~> 0.1.0
shards install

Use

require "oauth-device-flow"

store = OAuth::DeviceFlow::NetrcStore.new(machine: "rightdocuments.com")
client = OAuth::DeviceFlow::Client.new(
  base_url:  "https://rightdocuments.com",
  client_id: ENV["RIGHTDOCUMENTS_CLIENT_ID"],
  store:     store,
)

client.authenticate(scope: "documents:read documents:write") unless client.authenticated?

# On every API request — auto-refreshes if expired:
HTTP::Client.get(
  "https://rightdocuments.com/api/v1/me",
  headers: HTTP::Headers{"Authorization" => "Bearer #{client.access_token}"},
)

API

MethodReturnsBehavior
Client#authenticate(scope)TokenFull RFC 8628 flow: request device code, open browser, poll, persist.
Client#access_tokenStringLoads from store; auto-refreshes if expired. Raises NotAuthenticated if empty.
Client#authenticated?BoolTrue if any token is stored (no expiry check, no network).
Client#refresh!TokenForce a refresh. Raises InvalidGrant if no refresh_token.
Client#logoutNilClears the store.

Stores

  • MemoryStore — for tests.
  • NetrcStore.new(machine:) — ~/.netrc adapter, 0600 permissions, preserves other entries.

To plug in your own (Keychain, libsecret, JSON file): subclass OAuth::DeviceFlow::Store and implement load, save, clear.

Errors

All under OAuth::DeviceFlow::Error::*, all descend from OAuth::DeviceFlow::Error::Base: AuthorizationPending, SlowDown, AccessDenied, ExpiredToken, InvalidClient, InvalidGrant, NotAuthenticated, NetworkError.

authenticate only surfaces AccessDenied (user clicked Deny) or ExpiredToken (timeout) for normal failures — pending and slow_down are handled internally by the polling loop.

License

MIT