| 1 |
|
| 2 |
# Copyright (c) 2025, PostgreSQL Global Development Group |
| 3 |
|
| 4 |
=pod |
| 5 |
|
| 6 |
=head1 NAME |
| 7 |
|
| 8 |
OAuth::Server - runs a mock OAuth authorization server for testing |
| 9 |
|
| 10 |
=head1 SYNOPSIS |
| 11 |
|
| 12 |
use OAuth::Server; |
| 13 |
|
| 14 |
my $server = OAuth::Server->new(); |
| 15 |
$server->run; |
| 16 |
|
| 17 |
my $port = $server->port; |
| 18 |
my $issuer = "http://127.0.0.1:$port"; |
| 19 |
|
| 20 |
# test against $issuer... |
| 21 |
|
| 22 |
$server->stop; |
| 23 |
|
| 24 |
=head1 DESCRIPTION |
| 25 |
|
| 26 |
This is glue API between the Perl tests and the Python authorization server |
| 27 |
daemon implemented in t/oauth_server.py. (Python has a fairly usable HTTP server |
| 28 |
in its standard library, so the implementation was ported from Perl.) |
| 29 |
|
| 30 |
This authorization server does not use TLS (it implements a nonstandard, unsafe |
| 31 |
issuer at "http://127.0.0.1:<port>"), so libpq in particular will need to set |
| 32 |
PGOAUTHDEBUG=UNSAFE to be able to talk to it. |
| 33 |
|
| 34 |
=cut |
| 35 |
|
| 36 |
package OAuth::Server; |
| 37 |
|
| 38 |
use warnings; |
| 39 |
use strict; |
| 40 |
use Scalar::Util; |
| 41 |
use Test::More; |
| 42 |
|
| 43 |
=pod |
| 44 |
|
| 45 |
=head1 METHODS |
| 46 |
|
| 47 |
=over |
| 48 |
|
| 49 |
=item OAuth::Server->new() |
| 50 |
|
| 51 |
Create a new OAuth Server object. |
| 52 |
|
| 53 |
=cut |
| 54 |
|
| 55 |
sub new |
| 56 |
{ |
| 57 |
my $class = shift; |
| 58 |
|
| 59 |
my $self = {}; |
| 60 |
bless($self, $class); |
| 61 |
|
| 62 |
return $self; |
| 63 |
} |
| 64 |
|
| 65 |
=pod |
| 66 |
|
| 67 |
=item $server->port() |
| 68 |
|
| 69 |
Returns the port in use by the server. |
| 70 |
|
| 71 |
=cut |
| 72 |
|
| 73 |
sub port |
| 74 |
{ |
| 75 |
my $self = shift; |
| 76 |
|
| 77 |
return $self->{'port'}; |
| 78 |
} |
| 79 |
|
| 80 |
=pod |
| 81 |
|
| 82 |
=item $server->run() |
| 83 |
|
| 84 |
Runs the authorization server daemon in t/oauth_server.py. |
| 85 |
|
| 86 |
=cut |
| 87 |
|
| 88 |
sub run |
| 89 |
{ |
| 90 |
my $self = shift; |
| 91 |
my $port; |
| 92 |
|
| 93 |
my $pid = open(my $read_fh, "-|", $ENV{PYTHON}, "t/oauth_server.py") |
| 94 |
or die "failed to start OAuth server: $!"; |
| 95 |
|
| 96 |
# Get the port number from the daemon. It closes stdout afterwards; that way |
| 97 |
# we can slurp in the entire contents here rather than worrying about the |
| 98 |
# number of bytes to read. |
| 99 |
$port = do { local $/ = undef; <$read_fh> } |
| 100 |
// die "failed to read port number: $!"; |
| 101 |
chomp $port; |
| 102 |
die "server did not advertise a valid port" |
| 103 |
unless Scalar::Util::looks_like_number($port); |
| 104 |
|
| 105 |
$self->{'pid'} = $pid; |
| 106 |
$self->{'port'} = $port; |
| 107 |
$self->{'child'} = $read_fh; |
| 108 |
|
| 109 |
note("OAuth provider (PID $pid) is listening on port $port\n"); |
| 110 |
} |
| 111 |
|
| 112 |
=pod |
| 113 |
|
| 114 |
=item $server->stop() |
| 115 |
|
| 116 |
Sends SIGTERM to the authorization server and waits for it to exit. |
| 117 |
|
| 118 |
=cut |
| 119 |
|
| 120 |
sub stop |
| 121 |
{ |
| 122 |
my $self = shift; |
| 123 |
|
| 124 |
note("Sending SIGTERM to OAuth provider PID: $self->{'pid'}\n"); |
| 125 |
|
| 126 |
kill(15, $self->{'pid'}); |
| 127 |
$self->{'pid'} = undef; |
| 128 |
|
| 129 |
# Closing the popen() handle waits for the process to exit. |
| 130 |
close($self->{'child'}); |
| 131 |
$self->{'child'} = undef; |
| 132 |
} |
| 133 |
|
| 134 |
=pod |
| 135 |
|
| 136 |
=back |
| 137 |
|
| 138 |
=cut |
| 139 |
|
| 140 |
1; |