Zum Inhalt springen

sdk/ruby

Ruby-Server-SDK für netzwerklose JWT-Prüfung,Rack/Rails-Anfrage-Authentifizierung und Webhook-Signaturvalidierung.

Als Markdown anzeigen

Zustand

Implementiert und lokal verifiziert. Die echteIdP-Round-Trip-Verifizierung (JWKS-Abruf, Token-Signierung/Prüfung gegeneine Live-XID-Instanz) wurde noch nicht durchgeführt und muss vor demProduktionseinsatz abgeschlossen werden.

Installieren

# Gemfile
gem "xid"

bundle install

Schnellstart

require "xid"

Xid.configure do |c|
  c.issuer         = "https://xid.dev"
  c.audience       = "your_client_id"
  c.webhook_secret = "whsec_AbCdEf..."
end

# Verify a token
begin
  claims = Xid.verify_token(raw_token)
  puts claims.sub    # => "usr_abc123"
  puts claims.scope  # => "openid profile email"
rescue Xid::TokenVerificationError => e
  puts "Token invalid: #{e.message}"
end

Rack/Rails-Anfrage authentifizieren

# Sinatra before-filter
before do
  auth = Xid.authenticate_request(request)
  halt 401, "Unauthorized" unless auth.signed_in?
  @current_user_id = auth.claims.sub
end

# Rack middleware (env-based)
auth = Xid.authenticate_request(env)
unless auth.signed_in?
  return [401, { "Content-Type" => "application/json" },
          [JSON.generate({ error: auth.reason })]]
end

Webhook prüfen

# Rails controller action
def receive
  raw_body = request.raw_post
  payload = Xid.verify_webhook(request.headers.to_h, raw_body)
  handle_event(payload["type"], payload["data"])
  head :ok
rescue Xid::WebhookVerificationError
  head :bad_request
end

Multi-Issuer-Einrichtung

config_a = Xid::Configuration.new
config_a.issuer   = "https://tenant-a.xid.dev"
config_a.audience = "client_a"
client_a = Xid::Client.new(config_a)
claims = client_a.verify_token(token)

Konfigurationsoptionen

Schlüssel Standard Beschreibung
issuer https://xid.dev OIDC-Issuer-URL
audience nil Erwarteter aud-Claim; nil überspringt die Validierung
jwks_ttl 3600 Lokale JWKS-Cache-TTL in Sekunden
leeway 60 JWT-Taktversatztoleranz in Sekunden
webhook_secret nil Webhook-Signing-Secret mit whsec_-Prefix
webhook_tolerance 300 Webhook-Replay-Fenster in Sekunden
cookie_name __xid_token Cookie-Schlüssel für die Access-Token-Extraktion

Plattformhinweise

  • Setzt das jwt-Gem (ES256/RS256-Unterstützung) voraus. Ruby 3.1+erforderlich.
  • Xid.authenticate_request akzeptiert sowohl einen Rack-Env-Hash alsauch ein Rack-Request-Objekt.
  • Ausnahmehierarchie: Xid::Error -> ConfigurationError,JwksError, TokenVerificationError,WebhookVerificationError.
Navigation

Suchbegriff eingeben...

Mit den Pfeiltasten navigierenEingabetaste zum AuswählenEscape zum Schließen