Class: WorkOS::Webhooks
- Inherits:
-
Object
- Object
- WorkOS::Webhooks
- Defined in:
- lib/workos/webhooks.rb
Constant Summary collapse
- DEFAULT_TOLERANCE_SECONDS =
180
Instance Method Summary collapse
-
#compute_signature(payload:, timestamp:, secret:) ⇒ Object
Compute the HMAC-SHA256 hex signature for a (timestamp, payload) pair.
-
#construct_event(payload:, sig_header:, secret:, tolerance: DEFAULT_TOLERANCE_SECONDS) ⇒ WorkOS::WebhookEvent
Verify a webhook signature and return a typed event struct.
-
#create_webhook_endpoint(endpoint_url:, events:, request_options: {}) ⇒ WorkOS::WebhookEndpointJson
Create a Webhook Endpoint.
-
#delete_webhook_endpoint(id:, request_options: {}) ⇒ void
Delete a Webhook Endpoint.
-
#initialize(client) ⇒ Webhooks
constructor
A new instance of Webhooks.
-
#list_webhook_endpoints(before: nil, after: nil, limit: nil, order: "desc", request_options: {}) ⇒ WorkOS::Types::ListStruct<WorkOS::WebhookEndpointJson>
List Webhook Endpoints.
-
#parse_signature_header(sig_header) ⇒ Object
Parse a “t=<ms>, v1=<sig>” header into [timestamp, signature].
-
#update_webhook_endpoint(id:, endpoint_url: nil, status: nil, events: nil, request_options: {}) ⇒ WorkOS::WebhookEndpointJson
Update a Webhook Endpoint.
-
#verify_event(payload:, sig_header:, secret:, tolerance: DEFAULT_TOLERANCE_SECONDS) ⇒ Hash
Verify a webhook signature and return the deserialized event payload.
-
#verify_header(payload:, sig_header:, secret:, tolerance: DEFAULT_TOLERANCE_SECONDS) ⇒ true
Verify the webhook signature without deserializing the payload.
Constructor Details
#initialize(client) ⇒ Webhooks
Returns a new instance of Webhooks.
9 10 11 |
# File 'lib/workos/webhooks.rb', line 9 def initialize(client) @client = client end |
Instance Method Details
#compute_signature(payload:, timestamp:, secret:) ⇒ Object
Compute the HMAC-SHA256 hex signature for a (timestamp, payload) pair. Exposed publicly so users can build their own verification flow.
215 216 217 |
# File 'lib/workos/webhooks.rb', line 215 def compute_signature(payload:, timestamp:, secret:) WorkOS::Util::Signature.compute(payload: payload, timestamp: , secret: secret) end |
#construct_event(payload:, sig_header:, secret:, tolerance: DEFAULT_TOLERANCE_SECONDS) ⇒ WorkOS::WebhookEvent
Verify a webhook signature and return a typed event struct.
Parses the ‘WorkOS-Signature` header, validates the HMAC-SHA256 signature against the endpoint secret, checks that the event timestamp is within the tolerance window, and returns a typed WorkOS::WebhookEvent.
165 166 167 168 |
# File 'lib/workos/webhooks.rb', line 165 def construct_event(payload:, sig_header:, secret:, tolerance: DEFAULT_TOLERANCE_SECONDS) raw = verify_event(payload: payload, sig_header: sig_header, secret: secret, tolerance: tolerance) WebhookEvent.new(raw) end |
#create_webhook_endpoint(endpoint_url:, events:, request_options: {}) ⇒ WorkOS::WebhookEndpointJson
Create a Webhook Endpoint
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 |
# File 'lib/workos/webhooks.rb', line 62 def create_webhook_endpoint( endpoint_url:, events:, request_options: {} ) body = { "endpoint_url" => endpoint_url, "events" => events }.compact response = @client.request( method: :post, path: "/webhook_endpoints", auth: true, body: body, request_options: ) result = WorkOS::WebhookEndpointJson.new(response.body) result.last_response = WorkOS::Types::ApiResponse.new(http_status: response.code.to_i, http_headers: response.each_header.to_h, request_id: response["x-request-id"]) result end |
#delete_webhook_endpoint(id:, request_options: {}) ⇒ void
This method returns an undefined value.
Delete a Webhook Endpoint
118 119 120 121 122 123 124 125 126 127 128 129 |
# File 'lib/workos/webhooks.rb', line 118 def delete_webhook_endpoint( id:, request_options: {} ) @client.request( method: :delete, path: "/webhook_endpoints/#{WorkOS::Util.encode_path(id)}", auth: true, request_options: ) nil end |
#list_webhook_endpoints(before: nil, after: nil, limit: nil, order: "desc", request_options: {}) ⇒ WorkOS::Types::ListStruct<WorkOS::WebhookEndpointJson>
List Webhook Endpoints
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
# File 'lib/workos/webhooks.rb', line 20 def list_webhook_endpoints( before: nil, after: nil, limit: nil, order: "desc", request_options: {} ) params = { "before" => before, "after" => after, "limit" => limit, "order" => order }.compact response = @client.request( method: :get, path: "/webhook_endpoints", auth: true, params: params, request_options: ) fetch_next = ->(cursor) { list_webhook_endpoints( before: before, after: cursor, limit: limit, order: order, request_options: ) } WorkOS::Types::ListStruct.from_response( response, model: WorkOS::WebhookEndpointJson, filters: {before: before, limit: limit, order: order}, fetch_next: fetch_next ) end |
#parse_signature_header(sig_header) ⇒ Object
Parse a “t=<ms>, v1=<sig>” header into [timestamp, signature]. Exposed publicly for advanced use.
221 222 223 224 225 |
# File 'lib/workos/webhooks.rb', line 221 def parse_signature_header(sig_header) WorkOS::Util::Signature.parse_header(sig_header) rescue ArgumentError => e raise WorkOS::SignatureVerificationError.new(message: e., http_status: nil) end |
#update_webhook_endpoint(id:, endpoint_url: nil, status: nil, events: nil, request_options: {}) ⇒ WorkOS::WebhookEndpointJson
Update a Webhook Endpoint
90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 |
# File 'lib/workos/webhooks.rb', line 90 def update_webhook_endpoint( id:, endpoint_url: nil, status: nil, events: nil, request_options: {} ) body = { "endpoint_url" => endpoint_url, "status" => status, "events" => events }.compact response = @client.request( method: :patch, path: "/webhook_endpoints/#{WorkOS::Util.encode_path(id)}", auth: true, body: body, request_options: ) result = WorkOS::WebhookEndpointJson.new(response.body) result.last_response = WorkOS::Types::ApiResponse.new(http_status: response.code.to_i, http_headers: response.each_header.to_h, request_id: response["x-request-id"]) result end |
#verify_event(payload:, sig_header:, secret:, tolerance: DEFAULT_TOLERANCE_SECONDS) ⇒ Hash
Verify a webhook signature and return the deserialized event payload.
179 180 181 182 |
# File 'lib/workos/webhooks.rb', line 179 def verify_event(payload:, sig_header:, secret:, tolerance: DEFAULT_TOLERANCE_SECONDS) verify_header(payload: payload, sig_header: sig_header, secret: secret, tolerance: tolerance) JSON.parse(payload) end |
#verify_header(payload:, sig_header:, secret:, tolerance: DEFAULT_TOLERANCE_SECONDS) ⇒ true
Verify the webhook signature without deserializing the payload.
192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 |
# File 'lib/workos/webhooks.rb', line 192 def verify_header(payload:, sig_header:, secret:, tolerance: DEFAULT_TOLERANCE_SECONDS) , signature_hash = parse_signature_header(sig_header) max_age = tolerance.to_i issued_at = .to_i / 1000.0 if (Time.now.to_f - issued_at) > max_age raise WorkOS::SignatureVerificationError.new( message: "Timestamp outside the tolerance zone", http_status: nil ) end expected = compute_signature(payload: payload, timestamp: , secret: secret) unless secure_compare(signature_hash, expected) raise WorkOS::SignatureVerificationError.new( message: "Signature hash does not match the expected signature hash for payload", http_status: nil ) end true end |