Quick authentication setup for development and testing using pre-generated tokens
Personal Access Tokens (PATs) provide a simple authentication method using a pre-generated static token. This approach is ideal for development, testing, and scripts where you need quick setup without the complexity of OAuth flows.
Unlike OAuth-based methods, PATs do not automatically refresh:
# PAT remains static - no automatic refreshclient = Zitadel::Client::Zitadel.with_access_token( ENV['ZITADEL_HOST'], ENV['ZITADEL_PAT'])# This works until the token expiresclient.users.list_users# After expiration, you'll get an error# You must manually generate a new token
The PersonalAccessTokenAuthenticator is thread-safe since it only reads the static token:
require 'zitadel-client'# Safe to share across threadsclient = Zitadel::Client::Zitadel.with_access_token( ENV['ZITADEL_HOST'], ENV['ZITADEL_PAT'])threads = 10.times.map do Thread.new do # All threads safely use the same token client.settings.get_general_settings endendthreads.each(&:join)
PATs have a fixed expiration. Plan for token rotation:
require 'zitadel-client'def create_client token = ENV['ZITADEL_PAT'] if token.nil? || token.empty? raise "ZITADEL_PAT not set. Generate a new token at: #{ENV['ZITADEL_HOST']}/users/me/pats" end Zitadel::Client::Zitadel.with_access_token( ENV['ZITADEL_HOST'], token )endbegin client = create_client client.settings.get_general_settingsrescue Zitadel::Client::ZitadelError => e if e.message.include?('unauthorized') || e.message.include?('invalid_token') puts "Token expired. Generate a new PAT at: #{ENV['ZITADEL_HOST']}/users/me/pats" end raiseend
PATs inherit the permissions of the user who created them:
# PAT has same permissions as the creating userclient = Zitadel::Client::Zitadel.with_access_token( ENV['ZITADEL_HOST'], ENV['ZITADEL_PAT'])# This succeeds if the user has permissionclient.users.list_users# This fails if the user lacks permissionbegin client.organizations.add_organization(...)rescue Zitadel::Client::ApiError => e puts "Permission denied: #{e.message}"end
Create PATs from service users with minimal required permissions, not from admin accounts.
Enable debug logging to see authentication details:
client = Zitadel::Client::Zitadel.with_access_token( ENV['ZITADEL_HOST'], ENV['ZITADEL_PAT']) do |config| config.debug = trueend# Check if token is being sent correctlybegin client.settings.get_general_settingsrescue => e puts "Error: #{e.message}" # Debug output will show the full HTTP request/responseend