@@ -3010,4 +3010,63 @@ def wait_check_socket(conn)
30103010 . to raise_error ( TypeError )
30113011 end
30123012 end
3013+
3014+ describe "OAuth support" , :postgresql_18 do
3015+ before :all do
3016+ skip "requires a PostgreSQL 18 cluster" unless $pg_server. version >= 18
3017+
3018+ system "make" , "-s" , "-C" , ( TEST_DIRECTORY + "spec/oauth" ) . to_s
3019+ raise "Building OAuth validator library failed!" unless $?. success?
3020+
3021+ require 'webrick'
3022+
3023+ PG . connect ( @conninfo ) do |conn |
3024+ conn . exec ( "DROP USER IF EXISTS testuseroauth" )
3025+ conn . exec ( "CREATE USER testuseroauth" )
3026+ end
3027+ end
3028+
3029+ before :each do
3030+ @old_env , ENV [ "PGOAUTHDEBUG" ] = ENV [ "PGOAUTHDEBUG" ] , "UNSAFE"
3031+ end
3032+
3033+ def start_fake_oauth ( port )
3034+ server = WEBrick ::HTTPServer . new ( Port : port , Logger : WEBrick ::Log . new ( nil , WEBrick ::BasicLog ::WARN ) )
3035+ server . mount_proc ( "/.well-known/openid-configuration" ) do |req , res |
3036+ res [ "Content-Type" ] = "application/json"
3037+ res . body = %!{"issuer":"http://localhost:#{ port } ","token_endpoint":"http://localhost:#{ port } /token","device_authorization_endpoint":"http://localhost:#{ @port + 3 } /devauth"}!
3038+ end
3039+ server . mount_proc ( "/devauth" ) do |req , res |
3040+ res [ "Content-Type" ] = "application/json"
3041+ res . body = %!{"device_code":"42","user_code":"666","verification_uri":"http://localhost:#{ port } /verify","expires_in":60}!
3042+ end
3043+ server . mount_proc ( "/token" ) do |req , res |
3044+ res [ "Content-Type" ] = "application/json"
3045+ res . body = %!{"access_token":"yes","token_type":""}!
3046+ end
3047+ Thread . new { server . start }
3048+ server
3049+ end
3050+
3051+ it "should work with no hook" do
3052+ oauth_server = start_fake_oauth ( @port + 3 )
3053+
3054+ begin
3055+ PG . connect ( "host=localhost port=#{ @port } dbname=test user=testuseroauth oauth_issuer=http://localhost:#{ @port + 3 } oauth_client_id=foo" ) do |conn |
3056+ conn . exec ( "SELECT 1" )
3057+ end
3058+ rescue PG ::ConnectionBad => e
3059+ if e . message =~ /no OAuth flows are available/
3060+ skip "requires libpq-oauth to be installed"
3061+ end
3062+ raise
3063+ ensure
3064+ oauth_server . shutdown
3065+ end
3066+ end
3067+
3068+ after :each do
3069+ ENV [ "PGOAUTHDEBUG" ] = @old_env
3070+ end
3071+ end
30133072end
0 commit comments