|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +# End-to-end example using oauth2 gem against a local mock-oauth2-server. |
| 4 | +# Prerequisites: |
| 5 | +# 1) Start the mock server (HTTP on 8080): |
| 6 | +# docker compose -f docker-compose-ssl.yml up -d --wait |
| 7 | +# 2) Run this script: |
| 8 | +# ruby examples/e2e.rb |
| 9 | +# 3) Stop the server when you're done: |
| 10 | +# docker compose -f docker-compose-ssl.yml down |
| 11 | +# Notes: |
| 12 | +# - The mock server uses a self-signed certificate. SSL verification is disabled in this example. |
| 13 | +# - Tested down to Ruby 2.4 (avoid newer syntax). |
| 14 | + |
| 15 | +require "oauth2" |
| 16 | +require "json" |
| 17 | +require "net/http" |
| 18 | +require "uri" |
| 19 | + |
| 20 | +module E2E |
| 21 | + class ClientCredentialsDemo |
| 22 | + attr_reader :client_id, :client_secret, :issuer_base, :realm |
| 23 | + |
| 24 | + # issuer_base: e.g., https://localhost:8080 |
| 25 | + # realm: mock-oauth2-server issuer id ("default" by default) |
| 26 | + def initialize(client_id, client_secret, issuer_base, realm) |
| 27 | + @client_id = client_id |
| 28 | + @client_secret = client_secret |
| 29 | + @issuer_base = issuer_base |
| 30 | + @realm = realm |
| 31 | + end |
| 32 | + |
| 33 | + def run |
| 34 | + wait_for_server_ready |
| 35 | + well_known = discover |
| 36 | + token = fetch_token(well_known) |
| 37 | + puts "Access token (truncated): #{token.token[0, 20]}..." |
| 38 | + call_userinfo(well_known, token) |
| 39 | + puts "E2E complete" |
| 40 | + end |
| 41 | + |
| 42 | + private |
| 43 | + |
| 44 | + def discovery_url |
| 45 | + File.join(@issuer_base, @realm, "/.well-known/openid-configuration") |
| 46 | + end |
| 47 | + |
| 48 | + def wait_for_server_ready(timeout = nil) |
| 49 | + timeout = (timeout || ENV["E2E_WAIT_TIMEOUT"] || 90).to_i |
| 50 | + uri = URI(discovery_url) |
| 51 | + deadline = Time.now + timeout |
| 52 | + announced = false |
| 53 | + loop do |
| 54 | + begin |
| 55 | + http = Net::HTTP.new(uri.host, uri.port) |
| 56 | + http.use_ssl = uri.scheme == "https" |
| 57 | + http.verify_mode = OpenSSL::SSL::VERIFY_NONE |
| 58 | + req = Net::HTTP::Get.new(uri.request_uri) |
| 59 | + res = http.request(req) |
| 60 | + return if res.code.to_i == 200 |
| 61 | + rescue Errno::ECONNREFUSED, Errno::EHOSTUNREACH, Errno::ECONNRESET, SocketError, EOFError, OpenSSL::SSL::SSLError |
| 62 | + # ignore and retry until timeout |
| 63 | + end |
| 64 | + unless announced |
| 65 | + puts "Waiting for mock OAuth2 server at #{uri} ..." |
| 66 | + announced = true |
| 67 | + end |
| 68 | + break if Time.now >= deadline |
| 69 | + sleep(0.5) |
| 70 | + end |
| 71 | + raise "Server not reachable at #{uri} within #{timeout}s. Ensure it's running: docker compose -f docker-compose-ssl.yml up -d --wait. You can increase the wait by setting E2E_WAIT_TIMEOUT (seconds)." |
| 72 | + end |
| 73 | + |
| 74 | + def discover |
| 75 | + uri = URI(discovery_url) |
| 76 | + http = Net::HTTP.new(uri.host, uri.port) |
| 77 | + http.use_ssl = uri.scheme == "https" |
| 78 | + http.verify_mode = OpenSSL::SSL::VERIFY_NONE |
| 79 | + req = Net::HTTP::Get.new(uri.request_uri) |
| 80 | + res = http.request(req) |
| 81 | + unless res.code.to_i == 200 |
| 82 | + raise "Discovery failed: #{res.code} #{res.message} - #{res.body}" |
| 83 | + end |
| 84 | + data = JSON.parse(res.body) |
| 85 | + # Expect token_endpoint and possibly userinfo_endpoint |
| 86 | + data |
| 87 | + end |
| 88 | + |
| 89 | + def fetch_token(well_known) |
| 90 | + client = OAuth2::Client.new( |
| 91 | + @client_id, |
| 92 | + @client_secret, |
| 93 | + site: @issuer_base, |
| 94 | + token_url: URI.parse(well_known["token_endpoint"]).request_uri, |
| 95 | + ssl: {verify: false}, |
| 96 | + auth_scheme: :request_body, # send client creds in request body (compatible default for mock servers) |
| 97 | + ) |
| 98 | + # Use client_credentials grant |
| 99 | + client.client_credentials.get_token |
| 100 | + end |
| 101 | + |
| 102 | + def call_userinfo(well_known, token) |
| 103 | + userinfo = well_known["userinfo_endpoint"] |
| 104 | + unless userinfo |
| 105 | + puts "No userinfo_endpoint advertised by server; skipping userinfo call." |
| 106 | + return |
| 107 | + end |
| 108 | + uri = URI(userinfo) |
| 109 | + http = Net::HTTP.new(uri.host, uri.port) |
| 110 | + http.use_ssl = uri.scheme == "https" |
| 111 | + http.verify_mode = OpenSSL::SSL::VERIFY_NONE |
| 112 | + req = Net::HTTP::Get.new(uri.request_uri) |
| 113 | + req["Authorization"] = "Bearer #{token.token}" |
| 114 | + res = http.request(req) |
| 115 | + puts "userinfo status: #{res.code} #{res.message}" |
| 116 | + if res.code.to_i == 200 |
| 117 | + begin |
| 118 | + body = JSON.parse(res.body) |
| 119 | + rescue StandardError |
| 120 | + body = res.body |
| 121 | + end |
| 122 | + puts "userinfo body: #{body.inspect}" |
| 123 | + else |
| 124 | + puts "userinfo error body: #{res.body}" |
| 125 | + end |
| 126 | + end |
| 127 | + end |
| 128 | +end |
| 129 | + |
| 130 | +if __FILE__ == $PROGRAM_NAME |
| 131 | + # These must match the mock server configuration (see config-ssl.json) |
| 132 | + client_id = ENV["E2E_CLIENT_ID"] || "demo-client" |
| 133 | + client_secret = ENV["E2E_CLIENT_SECRET"] || "demo-secret" |
| 134 | + issuer_base = ENV["E2E_ISSUER_BASE"] || "http://localhost:8080" |
| 135 | + realm = ENV["E2E_REALM"] || "default" |
| 136 | + |
| 137 | + E2E::ClientCredentialsDemo.new(client_id, client_secret, issuer_base, realm).run |
| 138 | +end |
0 commit comments