| 1 | use warnings; | ||
| 2 | use strict; | ||
| 3 | |||
| 4 | package Net::OAuth2::Scheme::Mixin::VTable; | ||
| 5 | BEGIN { | ||
| 6 | $Net::OAuth2::Scheme::Mixin::VTable::VERSION = '0.020002_099'; | ||
| 7 | } | ||
| 8 | # ABSTRACT: the 'vtable', 'vtable_cache', and 'vtable_pull_queue' option groups | ||
| 9 | |||
| 10 | use Net::OAuth2::Scheme::Option::Defines; | ||
| 11 | |||
| 12 | # parse_token | ||
| 13 | # @token -> validator_id, @payload | ||
| 14 | # vtable_lookup | ||
| 15 | # validator_id -> @validator | ||
| 16 | # validate | ||
| 17 | # @validator, @payload -> valid?, issue_time, expires_in, scope, client_id | ||
| 18 | |||
| 19 | # @token = (token_string [, param => value]*) | ||
| 20 | # @validator = (@secrets [, expiration, scope, ext]) | ||
| 21 | # Token scheme determines format of @secrets, @payload and @validator | ||
| 22 | # Expiration+@scope may live in either @payload or @validator | ||
| 23 | # | ||
| 24 | # What needs to be communicated/shared privately | ||
| 25 | # between Authorization and Resource servers | ||
| 26 | # RS -> AS: server_id | ||
| 27 | # AS -> RS: vtable == (secret_id -> expiration, @data) hash/map | ||
| 28 | |||
| 29 | # INTERFACE vtable | ||
| 30 | # DEFINES | ||
| 31 | # (AS) vtable_insert | ||
| 32 | # (RS) vtable_lookup | ||
| 33 | Define_Group vtable => 'shared_cache', | ||
| 34 | qw(vtable_insert vtable_lookup); | ||
| 35 | |||
| 36 | # IMPLEMENTATION vtable_shared_cache FOR vtable | ||
| 37 | # SUMMARY | ||
| 38 | # secure shared cache, everybody can read/write | ||
| 39 | # REQUIRES | ||
| 40 | # vtable_cache | ||
| 41 | # NOTES | ||
| 42 | # vtable_insert = vtable_put | ||
| 43 | # vtable_lookup = vtable_get | ||
| 44 | # this works for | ||
| 45 | # (*) AS and RS being the same process | ||
| 46 | # (*) AS and RS being on the same host | ||
| 47 | # cache is file-based or shared-memory cache | ||
| 48 | # (*) AS and RS are on the same server farm | ||
| 49 | # cache is memcached | ||
| 50 | sub pkg_vtable_shared_cache { | ||
| 51 | my __PACKAGE__ $self = shift; | ||
| 52 | if ($self->is_auth_server) { | ||
| 53 | $self->install( vtable_insert => $self->uses('vtable_put')); | ||
| 54 | } | ||
| 55 | if ($self->is_resource_server) { | ||
| 56 | $self->install( vtable_lookup => $self->uses('vtable_get')); | ||
| 57 | } | ||
| 58 | return $self; | ||
| 59 | } | ||
| 60 | |||
| 61 | # IMPLEMENTATION vtable_authserv_push FOR vtable | ||
| 62 | # SUMMARY | ||
| 63 | # RS-local cache with AS pushing each new entry | ||
| 64 | # REQUIRES | ||
| 65 | # vtable_cache (+ (RS) vtable_put) | ||
| 66 | # vtable_push(entry) => send entry | ||
| 67 | # push handler => recv entry, vtable_pushed(entry) | ||
| 68 | # EXPORTS | ||
| 69 | # vtable_pushed (for push handler) | ||
| 70 | # NOTES | ||
| 71 | # vtable_insert = vtable_push | ||
| 72 | # vtable_lookup = vtable_get | ||
| 73 | # vtable_pushed = vtable_put | ||
| 74 | |||
| 75 | sub pkg_vtable_authserv_push { | ||
| 76 | my __PACKAGE__ $self = shift; | ||
| 77 | if ($self->is_auth_server) { | ||
| 78 | $self->install( vtable_insert => $self->uses('vtable_push')); | ||
| 79 | } | ||
| 80 | if ($self->is_resource_server) { | ||
| 81 | $self->install( vtable_lookup => $self->uses('vtable_get')); | ||
| 82 | $self->install( vtable_pushed => $self->uses('vtable_put')); | ||
| 83 | $self->export('vtable_pushed'); | ||
| 84 | } | ||
| 85 | return $self; | ||
| 86 | } | ||
| 87 | |||
| 88 | # IMPLEMENTATION vtable_resource_pull FOR vtable | ||
| 89 | # SUMMARY | ||
| 90 | # RS-local cache with RS pulling on cache miss | ||
| 91 | # REQUIRES | ||
| 92 | # vtable_cache | ||
| 93 | # vtable_pull_queue | ||
| 94 | # vtable_pull => send query, return response | ||
| 95 | # pull handler => recv query, respond vtable_dump(query) | ||
| 96 | # EXPORTS | ||
| 97 | # vtable_dump (for pull handler) | ||
| 98 | # NOTES | ||
| 99 | # vtable_insert = vtable_enqueue | ||
| 100 | # vtable_lookup is | ||
| 101 | # vtable_get or | ||
| 102 | # (vtable_load(vtable_pull(vtable_query)) and | ||
| 103 | # retry vtable_get) | ||
| 104 | |||
| 105 | sub pkg_vtable_resource_pull { | ||
| 106 | my __PACKAGE__ $self = shift; | ||
| 107 | if ($self->is_auth_server) { | ||
| 108 | $self->install( vtable_insert => $self->uses('vtable_enqueue')); | ||
| 109 | $self->export('vtable_dump'); | ||
| 110 | } | ||
| 111 | if ($self->is_resource_server) { | ||
| 112 | my ( $vtable_get, $vtable_load, $vtable_query, $vtable_pull) = $self->uses_all | ||
| 113 | (qw(vtable_get vtable_load vtable_query vtable_pull)); | ||
| 114 | $self->install( vtable_lookup => sub { | ||
| 115 | my $v_id = shift; | ||
| 116 | my ($error, @found) = $vtable_get->($v_id); | ||
| 117 | unless ($error || @found) { | ||
| 118 | ($error) = $vtable_load->($vtable_pull->($vtable_query->())); | ||
| 119 | return $error if $error; | ||
| 120 | ($error,@found) = $vtable_get->($v_id); | ||
| 121 | } | ||
| 122 | return ($error, @found); | ||
| 123 | }); | ||
| 124 | } | ||
| 125 | return $self; | ||
| 126 | } | ||
| 127 | |||
| 128 | # INTERFACE vtable_cache | ||
| 129 | # DEFINES | ||
| 130 | # vtable_put : id,expiration,@stuff ->; | ||
| 131 | # vtable_get : id -> expiration,@stuff | ||
| 132 | Define_Group vtable_cache => 'object', | ||
| 133 | qw(vtable_put vtable_get); | ||
| 134 | |||
| 135 | |||
| 136 | Default_Value cache_grace => 300; | ||
| 137 | Default_Value cache_prefix => 'vtab'; | ||
| 138 | |||
| 139 | # vtable_cache_object | ||
| 140 | # IMPLEMENTS vtable_cache | ||
| 141 | # REQUIRES | ||
| 142 | # cache => Cache::Memory, Cache::File, or Cache::Memcached object | ||
| 143 | # OPTIONS | ||
| 144 | # cache_grace => number; | ||
| 145 | # delay cache expiration by this many seconds (default = 300) | ||
| 146 | # cache_prefix => string | ||
| 147 | # keys are prefixed with this (default = 'vtab:') | ||
| 148 | sub pkg_vtable_cache_object { | ||
| 149 | my __PACKAGE__ $self = shift; | ||
| 150 | my ( $cache, $grace, $prefix) = $self->uses_all( | ||
| 151 | qw(cache cache_grace cache_prefix)); | ||
| 152 | |||
| 153 | $prefix .= ':' if length($prefix) && $prefix !~ m/:\z/; | ||
| 154 | $self->croak("cache_prefix ($prefix) cannot contain interior colon (:)") | ||
| 155 | if length($prefix) && $prefix =~ m{:[^:]}; | ||
| 156 | |||
| 157 | $self->install( vtable_get => sub { | ||
| 158 | my $v = $cache->thaw($prefix . $_[0]); | ||
| 159 | return (undef, @{defined($v) ? $v : []}); | ||
| 160 | }); | ||
| 161 | $self->install( vtable_put => sub { | ||
| 162 | my $id = shift; | ||
| 163 | $cache->freeze($prefix . $id, [@_], $_[0] + $grace); | ||
| 164 | return (); | ||
| 165 | }); | ||
| 166 | } | ||
| 167 | |||
| 168 | # INTERFACE vtable_pull_queue | ||
| 169 | # DEFINES | ||
| 170 | # (AS) vtable_enqueue | ||
| 171 | # vtable_dump | ||
| 172 | # (RS) vtable_query | ||
| 173 | # vtable_load | ||
| 174 | Define_Group vtable_pull_queue => 'default', | ||
| 175 | qw(vtable_enqueue | ||
| 176 | vtable_dump | ||
| 177 | vtable_query | ||
| 178 | vtable_load); | ||
| 179 | |||
| 180 | # default implementation | ||
| 181 | # REQUIRES | ||
| 182 | # (RS) vtable_put | ||
| 183 | sub pkg_vtable_pull_queue_default { | ||
| 184 | my __PACKAGE__ $self = shift; | ||
| 185 | if ($self->is_auth_server) { | ||
| 186 | my $cache_grace = $self->uses('cache_grace'); | ||
| 187 | my $vpqueue = {}; | ||
| 188 | my $latest = []; | ||
| 189 | |||
| 190 | $self->install( vtable_enqueue => sub { | ||
| 191 | my ($v_id, $expiration, $now) = @_; | ||
| 192 | |||
| 193 | # insert maintaining @$latest in order of increasing expiration | ||
| 194 | my $i = $#{$latest}; | ||
| 195 | --$i while ($i >= 0 && $latest->[$i]->[1] > $expiration); | ||
| 196 | splice @{$latest}, $i+1, 0, [@_]; | ||
| 197 | |||
| 198 | # prune expired entries from @$latest | ||
| 199 | $i = 0; | ||
| 200 | ++$i while ($i < @$latest && $latest->[$i]->[1] + $cache_grace < $now); | ||
| 201 | splice @{$latest}, 0, $i; | ||
| 202 | |||
| 203 | # prune expired entries from each batch of %$vpqueue | ||
| 204 | for my $entries (values %{$vpqueue}) { | ||
| 205 | $i = $#{$entries}; | ||
| 206 | --$i while ($i >= 0 && $entries->[$i]->[1] + $cache_grace < $now); | ||
| 207 | splice @{$entries}, $i+1; | ||
| 208 | } | ||
| 209 | # remove empty batches from %$vpqueue | ||
| 210 | delete @{$vpqueue}{grep {!@{$vpqueue->{$_}}} keys %{$vpqueue}}; | ||
| 211 | |||
| 212 | # never fails (?) | ||
| 213 | return (); | ||
| 214 | }); | ||
| 215 | |||
| 216 | $self->install( vtable_dump => sub { | ||
| 217 | my ($now, $last_recv) = @_; | ||
| 218 | # remove batches whose receipt has been acknowledged | ||
| 219 | delete @{$vpqueue}{grep {$_ <= $last_recv} keys %$vpqueue}; | ||
| 220 | |||
| 221 | # insert @$latest entries into vpqueue | ||
| 222 | if (@$latest) { | ||
| 223 | unless (exists $vpqueue->{$now}) { | ||
| 224 | # make a new batch | ||
| 225 | $vpqueue->{$now} = [reverse @{$latest}]; | ||
| 226 | } | ||
| 227 | else { | ||
| 228 | # merge into current batch; this should be rare | ||
| 229 | # and when it happens @$latest should be short | ||
| 230 | my $nqueue = $vpqueue->{$now}; | ||
| 231 | for my $e (@{$latest}) { | ||
| 232 | my $i = 0; | ||
| 233 | ++$i while ($i < @{$nqueue} && $nqueue->[$i]->[1] > $e->[1]); | ||
| 234 | splice @{$nqueue}, $i, 0, $e; | ||
| 235 | } | ||
| 236 | } | ||
| 237 | $latest = []; | ||
| 238 | } | ||
| 239 | # send everything | ||
| 240 | my @r = (); | ||
| 241 | push @r, @$_ for values %{$vpqueue}; | ||
| 242 | return (undef, $now, \@r); | ||
| 243 | }); | ||
| 244 | } | ||
| 245 | if ($self->is_resource_server) { | ||
| 246 | my $vtable_put = $self->uses('vtable_put'); | ||
| 247 | my $last_recv; | ||
| 248 | $self->install( vtable_query => sub { | ||
| 249 | my $now = time(); | ||
| 250 | return ($now, (defined($last_recv) && $now == $last_recv | ||
| 251 | ? $last_recv - 1 : $last_recv)); | ||
| 252 | }); | ||
| 253 | $self->install( vtable_load => sub { | ||
| 254 | my ($error, $now, $recvd) = @_; | ||
| 255 | return ($error) if $error; | ||
| 256 | for my $entry (@$recvd) { | ||
| 257 | $vtable_put->(@$entry); | ||
| 258 | } | ||
| 259 | $last_recv = $now; | ||
| 260 | return (undef, scalar(@$recvd)); | ||
| 261 | }); | ||
| 262 | } | ||
| 263 | return $self; | ||
| 264 | } | ||
| 265 | |||
| 266 | |||
| 267 | 1; | ||
| 268 | |||
| 269 | |||
| 270 | __END__ |