Skip to content

Instantly share code, notes, and snippets.

@Drowze
Created April 18, 2025 13:53
Show Gist options
  • Save Drowze/900bbed1f5e3511c6cc5b98e696cdbca to your computer and use it in GitHub Desktop.
Save Drowze/900bbed1f5e3511c6cc5b98e696cdbca to your computer and use it in GitHub Desktop.
Using Zitadel with Ruby (gRPC)

With buf.gen.yaml as:

version: v2
plugins:
  - remote: buf.build/grpc/ruby:v1.71.0
    out: gen
    include_imports: true
  - remote: buf.build/protocolbuffers/ruby:v30.2
    out: gen
    include_imports: true

Run:

buf generate https://github.com/zitadel/zitadel#format=git,tag=v2.71.7

Using it:

irb -I gen
ZITADEL_URL = "http://localhost:8080"
# doing a healthcheck:
require 'zitadel/auth_services_pb'
stub = Zitadel::Auth::V1::AuthService::Stub.new("localhost:8080", :this_channel_is_insecure)
stub.healthz(Zitadel::Auth::V1::HealthzRequest.new)

require 'zitadel/org/v2/org_service_services_pb'
stub = Zitadel::Org::V2::OrganizationService::Stub.new(ZITADEL_URL, :this_channel_is_insecure)
response = stub.add_organization(::Zitadel::Org::V2::AddOrganizationRequest.new(name: "TestOrg"), metadata: { authorization: "Bearer #{token}" })
response.details.resource_owner
# => "315704095795052547"
response.to_h
# => {details: {sequence: 4, change_date: {seconds: 1744670304, nanos: 849619000}, resource_owner: "315704095795052547"}, organization_id: "315704095795052547"}
ZITADEL_URL = "localhost:8080"
require "json"
require 'zitadel/org/v2/org_service_services_pb'
$token = File.read("pat-key.json").chomp
def create_organization(name:)
stub = Zitadel::Org::V2::OrganizationService::Stub.new(ZITADEL_URL, :this_channel_is_insecure)
stub.add_organization(
::Zitadel::Org::V2::AddOrganizationRequest.new(name: name),
metadata: { authorization: "Bearer #{$token}" }
)
end
def list_organizations(query: nil, sorting_column: nil, offset: 0, limit: 10)
list_query = Zitadel::Object::V2::ListQuery.new(offset: offset, limit: limit, asc: false)
# sorting_column is an enum.
# see: https://github.com/zitadel/zitadel/blob/a2f60f2e7af4d5c3282d1a568fa04492e8b1a7ba/proto/zitadel/org/v2/query.proto#L84
sorting_column = 1 if sorting_column.to_s == 'name'
queries = if query
name_query = Zitadel::Org::V2::OrganizationNameQuery.new(name: query)
Zitadel::Org::V2::SearchQuery.new(name_query: name_query)
end
stub = Zitadel::Org::V2::OrganizationService::Stub.new(ZITADEL_URL, :this_channel_is_insecure)
stub.list_organizations(
Zitadel::Org::V2::ListOrganizationsRequest.new(
query: list_query,
sorting_column: sorting_column,
queries: queries
),
metadata: { authorization: "Bearer #{$token}" }
)
end
# MyCompany - main organization
# MyCompany (public) - public organization (where users may fall on their
# login if the domain didn't resolve to other organziation)
# ...and two more organizations for testing purposes
["MyCompany", "MyCompany (public)", "NexuSphere Logistics", "InnoviTrack Solutions"].each do |org_name|
create_organization(name: org_name)
rescue
nil
end
organizations = list_organizations.result.reduce({}) { |acc, org| acc.merge(org.name => org) }
my_company_org = organizations["MyCompany"]
my_company_public_org = organizations["MyCompany (public)"]
org_1 = organizations["NexuSphere Logistics"]
org_2 = organizations["InnoviTrack Solutions"]
binding.irb
# p create_organization(name: "MyCompany")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment