--- loncom/Lond.pm 2022/02/17 22:35:50 1.21 +++ loncom/Lond.pm 2023/05/22 21:10:56 1.22 @@ -1,6 +1,6 @@ # The LearningOnline Network # -# $Id: Lond.pm,v 1.21 2022/02/17 22:35:50 raeburn Exp $ +# $Id: Lond.pm,v 1.22 2023/05/22 21:10:56 raeburn Exp $ # # Copyright Michigan State University Board of Trustees # @@ -43,6 +43,9 @@ use Crypt::X509::CRL; use Crypt::PKCS10; use Net::OAuth; use Crypt::CBC; +use Net::OAuth; +use Digest::SHA; +use Digest::MD5 qw(md5_hex); sub dump_with_regexp { my ( $tail, $clientversion ) = @_; @@ -1254,6 +1257,122 @@ sub domlti_itemid { return $itemid; } +sub sign_params { + my ($cdom,$cnum,$crstool,$url,$idx,$keynum,$post,$loncaparev,$paramsref,$inforef) = @_; + return unless (ref($paramsref) eq 'HASH'); + my ($sigmethod,$type,$callback); + if (ref($inforef) eq 'HASH') { + if (exists($inforef->{'method'})) { + $sigmethod = $inforef->{'method'}; + } + if (exists($inforef->{'cb'})) { + $callback = $inforef->{'cb'}; + } + if (exists($inforef->{'type'})) { + $type = $inforef->{'type'}; + } + } + my ($cachename,$hashid,$key,$secret,%ltitoolsenc); + if ($crstool) { + $cachename = 'crsltitoolsenc'; + $hashid = $cdom.'_'.$cnum; + } else { + $cachename = 'ltitoolsenc'; + $hashid = $cdom; + } + my ($encresult,$enccached)=&Apache::lonnet::is_cached_new($cachename,$hashid); + if (defined($enccached)) { + if (ref($encresult) eq 'HASH') { + %ltitoolsenc = %{$encresult}; + } + } else { + if ($crstool) { + my $reply = &dump_with_regexp(join(":",($cdom,$cnum,'nohist_toolsenc','','')),$loncaparev); + %ltitoolsenc = %{&Apache::lonnet::unserialize($reply)}; + } else { + my $reply = &get_dom("getdom:$cdom:encconfig:ltitools"); + my $ltitoolsencref = &Apache::lonnet::thaw_unescape($reply); + if (ref($ltitoolsencref) eq 'HASH') { + %ltitoolsenc = %{$ltitoolsencref}; + } + } + my $cachetime = 24*60*60; + &Apache::lonnet::do_cache_new($cachename,$hashid,\%ltitoolsenc,$cachetime); + } + if (!keys(%ltitoolsenc)) { + return; + } elsif (exists($ltitoolsenc{$idx})) { + if (ref($ltitoolsenc{$idx}) eq 'HASH') { + if (exists($ltitoolsenc{$idx}{'key'})) { + $key = $ltitoolsenc{$idx}{'key'}; + } + if (exists($ltitoolsenc{$idx}{'secret'})) { + $secret = $ltitoolsenc{$idx}{'secret'}; + my $privhost; + if ($keynum =~ /^\d+$/) { + if ($crstool) { + my $primary = &Apache::lonnet::domain($cdom,'primary'); + my @ids = &Apache::lonnet::current_machine_ids(); + unless (grep(/^\Q$primary\E$/,@ids)) { + $privhost = $primary; + my ($result,$plainsecret) = &decrypt_secret($privhost,$secret,$keynum,'ltitools'); + if ($result eq 'ok') { + $secret = $plainsecret; + } else { + undef($secret); + } + } + } + unless ($privhost) { + my $privkey = &get_dom("getdom:$cdom:private:$keynum:ltitools:key"); + if (($privkey ne '') && ($secret ne '')) { + my $cipher = new Crypt::CBC($privkey); + $secret = $cipher->decrypt_hex($secret); + } else { + undef($secret); + } + } + } + } + } + } + return if (($key eq '') || ($secret eq '')); + if ($sigmethod eq '') { + $sigmethod = 'HMAC-SHA1'; + } + if ($type eq '') { + $type = 'request token'; + } + if ($callback eq '') { + $callback = 'about:blank', + } + srand( time() ^ ($$ + ($$ << 15)) ); # Seed rand. + my $nonce = Digest::SHA::sha1_hex(sprintf("%06x%06x",rand(0xfffff0),rand(0xfffff0))); + my $request = Net::OAuth->request($type)->new( + consumer_key => $key, + consumer_secret => $secret, + request_url => $url, + request_method => 'POST', + signature_method => $sigmethod, + timestamp => time, + nonce => $nonce, + callback => $callback, + extra_params => $paramsref, + version => '1.0', + ); + $request->sign(); + if ($post) { + return $request->to_post_body(); + } else { + return $request->to_hash(); + } +} + +sub decrypt_secret { + my ($privhost,$secret,$keynum,$type) = @_; + return; +} + 1; __END__