| 1 | package Net::Z3950::FOLIO::Session; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use warnings; | ||
| 5 | |||
| 6 | use DateTime; | ||
| 7 | use Cpanel::JSON::XS qw(decode_json encode_json); | ||
| 8 | use Net::Z3950::FOLIO::Config; | ||
| 9 | use Net::Z3950::FOLIO::ResultSet; | ||
| 10 | |||
| 11 | |||
| 12 | sub _throw { return Net::Z3950::FOLIO::_throw(@_); } | ||
| 13 | |||
| 14 | |||
| 15 | sub new { | ||
| 16 | my $class = shift(); | ||
| 17 | my($ghandle, $name) = @_; | ||
| 18 | |||
| 19 | my $ua = new LWP::UserAgent(); | ||
| 20 | my $jar = HTTP::Cookies->new(); | ||
| 21 | $ua->cookie_jar($jar); | ||
| 22 | $ua->agent("z2folio $Net::Z3950::FOLIO::VERSION"); | ||
| 23 | |||
| 24 | return bless { | ||
| 25 | ghandle => $ghandle, | ||
| 26 | ua => $ua, | ||
| 27 | name => $name, | ||
| 28 | resultsets => {}, # indexed by setname | ||
| 29 | }, $class; | ||
| 30 | } | ||
| 31 | |||
| 32 | |||
| 33 | sub reloadConfigFile { | ||
| 34 | my $this = shift(); | ||
| 35 | my $ghandle = $this->{ghandle}; | ||
| 36 | |||
| 37 | $this->{cfg} = new Net::Z3950::FOLIO::Config($ghandle->{cfgbase}, split(/\|/, $this->{name})); | ||
| 38 | } | ||
| 39 | |||
| 40 | |||
| 41 | sub login { | ||
| 42 | my $this = shift(); | ||
| 43 | my($user, $pass) = @_; | ||
| 44 | |||
| 45 | my $cfg = $this->{cfg}; | ||
| 46 | my $login = $cfg->{login} || {}; | ||
| 47 | my $username = $user || $login->{username}; | ||
| 48 | my $password = $pass || $login->{password}; | ||
| 49 | _throw(1014, "credentials not supplied") | ||
| 50 | if !defined $username || !defined $password; | ||
| 51 | |||
| 52 | my $url = $cfg->{okapi}->{url} . '/authn/login-with-expiry'; | ||
| 53 | my $req = $this->_makeHTTPRequest(POST => $url); | ||
| 54 | $req->content(qq[{ "username": "$username", "password": "$password" }]); | ||
| 55 | # warn "req=", $req->content(); | ||
| 56 | my $res = $this->{ua}->request($req); | ||
| 57 | # warn "res=", $res->content(); | ||
| 58 | _throw(1014, $res->content()) | ||
| 59 | if !$res->is_success(); | ||
| 60 | |||
| 61 | $this->_setRefreshTokenExpiration($res); | ||
| 62 | } | ||
| 63 | |||
| 64 | |||
| 65 | sub _setRefreshTokenExpiration { | ||
| 66 | my $this = shift(); | ||
| 67 | my($res) = @_; | ||
| 68 | |||
| 69 | my $json = decode_json($res->content()); | ||
| 70 | my $refreshTokenEpoch = _isoStringToEpoch($json->{refreshTokenExpiration}); | ||
| 71 | my $accessTokenEpoch = _isoStringToEpoch($json->{accessTokenExpiration}); | ||
| 72 | my $minEpoch = $refreshTokenEpoch < $accessTokenEpoch ? $refreshTokenEpoch : $accessTokenEpoch; | ||
| 73 | my $nowEpoch = DateTime->now()->epoch(); | ||
| 74 | my $secs = $minEpoch - $nowEpoch; | ||
| 75 | |||
| 76 | # Choose when to get a new token, based on when the shorter-lived | ||
| 77 | # of the two tokens expires. One simple option would be when half | ||
| 78 | # of the allocated time has expired. Another would be a constant | ||
| 79 | # time (e.g. one minute) before the expiry is due. For now, we'll | ||
| 80 | # go with the second. | ||
| 81 | $this->{refreshTokenExpiration} = $minEpoch - 60; | ||
| 82 | } | ||
| 83 | |||
| 84 | |||
| 85 | sub _isoStringToEpoch { | ||
| 86 | my($isoString) = @_; | ||
| 87 | |||
| 88 | # Format: 2024-07-02T16:48:56Z | ||
| 89 | my $match = ($isoString =~ /(\d\d\d\d)-(\d\d)-(\d\d)T(\d\d):(\d\d):(\d\d)Z/); | ||
| 90 | _throw(2, "Non-ISO date returned as refreshTokenExpiration: $isoString") | ||
| 91 | if !$match; | ||
| 92 | |||
| 93 | my($year, $mon, $mday, $hour, $min, $sec) = ($1, $2, $3, $4, $5, $6); | ||
| 94 | my $dt = new DateTime( | ||
| 95 | year => $year, | ||
| 96 | month => $mon, | ||
| 97 | day => $mday, | ||
| 98 | hour => $hour, | ||
| 99 | minute => $min, | ||
| 100 | second => $sec, | ||
| 101 | ); | ||
| 102 | |||
| 103 | return $dt->epoch(); | ||
| 104 | } | ||
| 105 | |||
| 106 | sub maybeRefreshToken { | ||
| 107 | my $this = shift(); | ||
| 108 | |||
| 109 | my $cj = $this->{ua}->cookie_jar(); | ||
| 110 | if ($cj->as_string()) { | ||
| 111 | my $accessToken = $cj->as_string(); | ||
| 112 | $accessToken =~ s/.*folioAccessToken=(.*?);.*/$1/s; | ||
| 113 | $this->{accessToken} = $accessToken; | ||
| 114 | |||
| 115 | # We have some cookies, so initial login must have succeeded | ||
| 116 | my $now = DateTime->now(); | ||
| 117 | my $nowEpoch = $now->epoch(); | ||
| 118 | if ($nowEpoch > $this->{refreshTokenExpiration}) { | ||
| 119 | warn "update required"; | ||
| 120 | my $url = $this->{cfg}->{okapi}->{url} . '/authn/refresh'; | ||
| 121 | my $req = $this->_makeHTTPRequest(POST => $url); | ||
| 122 | # No content required | ||
| 123 | my $res = $this->{ua}->request($req); | ||
| 124 | _throw(1014, $res->content()) | ||
| 125 | if !$res->is_success(); | ||
| 126 | |||
| 127 | $this->_setRefreshTokenExpiration($res); | ||
| 128 | } | ||
| 129 | } | ||
| 130 | } | ||
| 131 | |||
| 132 | |||
| 133 | sub rerunSearch { | ||
| 134 | my $this = shift(); | ||
| 135 | my($setname) = @_; | ||
| 136 | |||
| 137 | my $cql = $this->{cql}; | ||
| 138 | my $rs = new Net::Z3950::FOLIO::ResultSet($this, $setname, $cql); | ||
| 139 | $this->{resultsets}->{$setname} = $rs; | ||
| 140 | |||
| 141 | my $chunkSize = $this->{cfg}->{chunkSize} || 10; | ||
| 142 | $this->doSearch($rs, 0, $chunkSize); | ||
| 143 | return $rs->totalCount(); | ||
| 144 | } | ||
| 145 | |||
| 146 | |||
| 147 | sub doSearch { | ||
| 148 | my $this = shift(); | ||
| 149 | my($rs, $offset, $limit) = @_; | ||
| 150 | |||
| 151 | my $okapiCfg = $this->{cfg}->{okapi}; | ||
| 152 | my $qf = $this->{cfg}->{queryFilter}; | ||
| 153 | my $cql = $rs->{cql}; | ||
| 154 | if ($qf) { | ||
| 155 | $cql = $cql ? "($cql) and ($qf)" : $qf; | ||
| 156 | } | ||
| 157 | my $sortspec = $this->{sortspec}; | ||
| 158 | if ($sortspec) { | ||
| 159 | $cql = "($cql) sortby $sortspec"; | ||
| 160 | warn "search: added sortspec, yielding '$cql'"; | ||
| 161 | } | ||
| 162 | |||
| 163 | my $url = $okapiCfg->{url}; | ||
| 164 | my $graphqlUrl = $okapiCfg->{graphqlUrl}; | ||
| 165 | my $req = $this->_makeHTTPRequest(POST => ($graphqlUrl || $url) . '/graphql'); | ||
| 166 | if ($graphqlUrl) { | ||
| 167 | $req->header('X-Okapi-Url' => $url); | ||
| 168 | # Okapi-moderated modules do not need an explicit token, but side-loaded mod-graphql does | ||
| 169 | $req->header('X-Okapi-Token' => $this->{accessToken}); | ||
| 170 | } | ||
| 171 | |||
| 172 | my %variables = (); | ||
| 173 | # warn "searching for $cql"; | ||
| 174 | $variables{cql} = $cql if $cql; | ||
| 175 | $variables{offset} = $offset if $offset; | ||
| 176 | $variables{limit} = $limit if $limit; | ||
| 177 | my %body = ( | ||
| 178 | query => $this->{cfg}->{graphql}, | ||
| 179 | variables => \%variables, | ||
| 180 | ); | ||
| 181 | $req->content(encode_json(\%body)); | ||
| 182 | my $res = $this->{ua}->request($req); | ||
| 183 | _throw(3, $res->content()) if !$res->is_success(); | ||
| 184 | |||
| 185 | my $obj = decode_json($res->content()); | ||
| 186 | # warn "result: ", Net::Z3950::FOLIO::Record::_formatJSON($obj); | ||
| 187 | my $errors = $obj->{errors}; | ||
| 188 | _throw(1, join(', ', map { $_->{message} } @$errors)) if $errors; | ||
| 189 | my $data = $obj->{data} or _throw(1, "no data in response"); | ||
| 190 | my $isi = ($data->{search_instances} || $data->{instance_storage_instances}); | ||
| 191 | if (!$isi) { | ||
| 192 | _throw(1, "no instance_storage_instances in response data " . $res->content()); | ||
| 193 | } | ||
| 194 | $rs->totalCount($isi->{totalRecords} + 0); | ||
| 195 | $rs->insertRecords($offset, $isi->{instances}); | ||
| 196 | |||
| 197 | return $rs; | ||
| 198 | } | ||
| 199 | |||
| 200 | |||
| 201 | sub _getSRSRecords { | ||
| 202 | my $this = shift(); | ||
| 203 | my($rs, $offset, $limit) = @_; | ||
| 204 | |||
| 205 | my $okapiCfg = $this->{cfg}->{okapi}; | ||
| 206 | my @ids = (); | ||
| 207 | for (my $i = 0; $i < $limit && $offset + $i < $rs->totalCount(); $i++) { | ||
| 208 | my $rec = $rs->record($offset + $i); | ||
| 209 | push @ids, $rec->id(); | ||
| 210 | } | ||
| 211 | |||
| 212 | my $req = $this->_makeHTTPRequest(POST => $okapiCfg->{url} . '/source-storage/source-records?idType=INSTANCE'); | ||
| 213 | $req->content(encode_json(\@ids)); | ||
| 214 | my $res = $this->{ua}->request($req); | ||
| 215 | my $content = $res->content(); | ||
| 216 | _throw(3, $content) if !$res->is_success(); | ||
| 217 | |||
| 218 | # warn "got content ", $content; | ||
| 219 | my $json = decode_json($content); | ||
| 220 | my $srs = $json->{sourceRecords}; | ||
| 221 | return map { _JSON2MARC($_->{parsedRecord}->{content}) } @$srs; | ||
| 222 | } | ||
| 223 | |||
| 224 | |||
| 225 | # We would like to use MARC::Record->new_from_json() for this (from | ||
| 226 | # MARC::File::JSON), but that uses a different JSON encoding from the | ||
| 227 | # one used for FOLIO's SRS records, so we have to do it by hand. | ||
| 228 | # | ||
| 229 | sub _JSON2MARC { | ||
| 230 | my($content) = shift(); | ||
| 231 | |||
| 232 | my $marc = new MARC::Record(); | ||
| 233 | $marc->leader($content->{leader}); | ||
| 234 | my $fields = $content->{fields}; | ||
| 235 | my $n = @$fields; | ||
| 236 | for (my $i = 0; $i < $n; $i++) { | ||
| 237 | my $field = $fields->[$i]; | ||
| 238 | my @keys = keys %$field; | ||
| 239 | warn "field #", ($i+1), " of $n has ", scalar(@keys), " fields" if @keys != 1; | ||
| 240 | foreach my $key (@keys) { | ||
| 241 | my $value = $field->{$key}; | ||
| 242 | if ($key =~ /^00/) { | ||
| 243 | $marc->append_fields(new MARC::Field($key, $value)); | ||
| 244 | } else { | ||
| 245 | # *sigh* I have to gather an array of single-key hashes into one hash | ||
| 246 | my @subfields; | ||
| 247 | for (my $j = 0; $j < @{$value->{subfields}}; $j++) { | ||
| 248 | foreach my $k2 (keys %{ $value->{subfields}->[$j] }) { | ||
| 249 | push @subfields, $k2, $value->{subfields}->[$j]->{$k2}; | ||
| 250 | } | ||
| 251 | } | ||
| 252 | if (@subfields) { | ||
| 253 | $marc->append_fields(new MARC::Field($key, $value->{ind1}, $value->{ind2}, @subfields)); | ||
| 254 | } | ||
| 255 | } | ||
| 256 | } | ||
| 257 | } | ||
| 258 | |||
| 259 | return $marc; | ||
| 260 | } | ||
| 261 | |||
| 262 | |||
| 263 | sub sortSpecs2CQL { | ||
| 264 | my $this = shift(); | ||
| 265 | my($sequence) = @_; | ||
| 266 | |||
| 267 | my @res = (); | ||
| 268 | foreach my $item (@$sequence) { | ||
| 269 | push @res, $this->_singleSortSpec2CQL($item); | ||
| 270 | } | ||
| 271 | |||
| 272 | my $spec = join(' ', @res); | ||
| 273 | return $spec; | ||
| 274 | } | ||
| 275 | |||
| 276 | |||
| 277 | sub _singleSortSpec2CQL { | ||
| 278 | my $this = shift(); | ||
| 279 | my($item) = @_; | ||
| 280 | my $indexMap = $this->{cfg}->{indexMap}; | ||
| 281 | |||
| 282 | my $set = $item->{ATTRSET}; | ||
| 283 | if ($set ne Net::Z3950::FOLIO::ATTRSET_BIB1() && lc($set) ne 'bib-1') { | ||
| 284 | # Unknown attribute set (anything except BIB-1) | ||
| 285 | _throw(121, $set); | ||
| 286 | } | ||
| 287 | |||
| 288 | my @modifiers = ( | ||
| 289 | [ missing => _translateSortParam($item->{MISSING}, 213, { | ||
| 290 | 1 => 'missingFail', | ||
| 291 | 2 => 'missingLow', | ||
| 292 | })], | ||
| 293 | [ relation => _translateSortParam($item->{RELATION}, 214, { | ||
| 294 | 0 => 'ascending', | ||
| 295 | 1 => 'descending', | ||
| 296 | })], | ||
| 297 | [ case => _translateSortParam($item->{CASE}, 215, { | ||
| 298 | 0 => 'respectCase', | ||
| 299 | 1 => 'ignoreCase', | ||
| 300 | })], | ||
| 301 | ); | ||
| 302 | |||
| 303 | my($accessPoint, $cqlIndex, $entry); | ||
| 304 | my $attrs = $item->{SORT_ATTR}; | ||
| 305 | foreach my $attr (@$attrs) { | ||
| 306 | my $type = $attr->{ATTR_TYPE}; | ||
| 307 | _throw(237, "sort-attribute of type $type (only 1 is supported)") if defined $type && $type != 1; | ||
| 308 | |||
| 309 | $accessPoint = $attr->{ATTR_VALUE}; | ||
| 310 | $entry = $indexMap->{$accessPoint}; | ||
| 311 | _throw(207, "undefined sort-index $accessPoint") if !defined $entry; | ||
| 312 | if (ref $entry) { | ||
| 313 | $cqlIndex = $entry->{cql}; | ||
| 314 | } else { | ||
| 315 | $cqlIndex = $entry; | ||
| 316 | $entry = undef; | ||
| 317 | } | ||
| 318 | last; | ||
| 319 | } | ||
| 320 | |||
| 321 | my $res = $cqlIndex; | ||
| 322 | |||
| 323 | my $omitList = $entry ? $entry->{omitSortIndexModifiers} : []; | ||
| 324 | foreach my $modifier (@modifiers) { | ||
| 325 | my($name, $value) = @$modifier; | ||
| 326 | if (!$omitList || ! grep { $_ eq $name } @$omitList) { | ||
| 327 | $res .= "/sort.$value"; | ||
| 328 | } else { | ||
| 329 | # warn "omitting '$name' sort-modifier for access-point $accessPoint ($cqlIndex)"; | ||
| 330 | } | ||
| 331 | }; | ||
| 332 | |||
| 333 | return $res; | ||
| 334 | } | ||
| 335 | |||
| 336 | |||
| 337 | sub _translateSortParam { | ||
| 338 | my($zval, $diag, $map) = @_; | ||
| 339 | |||
| 340 | my $cqlVal = $map->{$zval}; | ||
| 341 | _throw($diag, $zval) if !$cqlVal; | ||
| 342 | return $cqlVal; | ||
| 343 | } | ||
| 344 | |||
| 345 | |||
| 346 | sub _makeHTTPRequest() { | ||
| 347 | my $this = shift(); | ||
| 348 | my(%args) = @_; | ||
| 349 | |||
| 350 | my $req = new HTTP::Request(%args); | ||
| 351 | $req->header('X-Okapi-tenant' => $this->{cfg}->{okapi}->{tenant}); | ||
| 352 | $req->header('Content-type' => 'application/json'); | ||
| 353 | $req->header('Accept' => 'application/json'); | ||
| 354 | return $req; | ||
| 355 | } | ||
| 356 | |||
| 357 | |||
| 358 | 1; |