Module: WorkOS::SSO

Extended by:
T::Sig, Base, Client
Defined in:
lib/workos/sso.rb

Overview

The SSO module provides convenience methods for working with the WorkOS SSO platform. You’ll need a valid API key, a project ID, and to have created an SSO connection on your WorkOS dashboard.

Constant Summary collapse

PROVIDERS =
WorkOS::Types::Provider.values.map(&:serialize).freeze

Instance Attribute Summary

Attributes included from Base

#key

Class Method Summary collapse

Methods included from Client

client, execute_request, get_request, handle_error_response, post_request, user_agent

Class Method Details

.authorization_url(project_id:, redirect_uri:, domain: nil, provider: nil, state: {}) ⇒ Object



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/workos/sso.rb', line 61

def authorization_url(
  project_id:, redirect_uri:, domain: nil, provider: nil, state: {}
)
  validate_domain_and_provider(provider: provider, domain: domain)

  query = URI.encode_www_form({
    client_id: project_id,
    redirect_uri: redirect_uri,
    response_type: 'code',
    state: state,
    domain: domain,
    provider: provider,
  }.compact)

  "https://#{WorkOS::API_HOSTNAME}/sso/authorize?#{query}"
end

.create_connection(source:) ⇒ Object



159
160
161
162
163
164
165
166
167
168
169
# File 'lib/workos/sso.rb', line 159

def create_connection(source:)
  request = post_request(
    auth: true,
    path: '/connections',
    body: { source: source },
  )

  response = execute_request(request: request)

  WorkOS::Connection.new(response.body)
end

.profile(code:, project_id:) ⇒ Object



101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/workos/sso.rb', line 101

def profile(code:, project_id:)
  body = {
    client_id: project_id,
    client_secret: WorkOS.key!,
    grant_type: 'authorization_code',
    code: code,
  }

  response = client.request(post_request(path: '/sso/token', body: body))
  check_and_raise_profile_error(response: response)

  WorkOS::Profile.new(response.body)
end

.promote_draft_connection(token:) ⇒ Object



130
131
132
133
134
135
136
137
138
139
# File 'lib/workos/sso.rb', line 130

def promote_draft_connection(token:)
  request = post_request(
    auth: true,
    path: "/draft_connections/#{token}/activate",
  )

  response = client.request(request)

  response.is_a? Net::HTTPSuccess
end