| 1 | |||
| 2 | package Net::OpenID::Common; | ||
| 3 | BEGIN { | ||
| 4 | 6 | ▶ | $Net::OpenID::Common::VERSION = '1.030099_004'; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 5 | } | ||
| 6 | |||
| 7 | =head1 NAME | ||
| 8 | |||
| 9 | Net::OpenID::Common - Libraries shared between Net::OpenID::Consumer and Net::OpenID::Server | ||
| 10 | |||
| 11 | =head1 VERSION | ||
| 12 | |||
| 13 | version 1.030099_004 | ||
| 14 | |||
| 15 | =head1 DESCRIPTION | ||
| 16 | |||
| 17 | The Consumer and Server implementations share a few libraries which live with this module. This module is here largely to hold the version number and this documentation, though it also incorporates some utility functions inherited from previous versions of L<Net::OpenID::Consumer>. | ||
| 18 | |||
| 19 | =head1 COPYRIGHT | ||
| 20 | |||
| 21 | This package is Copyright (c) 2005 Brad Fitzpatrick, and (c) 2008 Martin Atkins. All rights reserved. | ||
| 22 | |||
| 23 | You may distribute under the terms of either the GNU General Public License or the Artistic License, as specified in the Perl README file. If you need more liberal licensing terms, please contact the maintainer. | ||
| 24 | |||
| 25 | =head1 MAINTAINER | ||
| 26 | |||
| 27 | Maintained by Martin Atkins <mart@degeneration.co.uk> | ||
| 28 | |||
| 29 | =cut | ||
| 30 | |||
| 31 | # This package should totally be called Net::OpenID::util, but | ||
| 32 | # it was historically named wrong so we're just leaving it | ||
| 33 | # like this to avoid confusion. | ||
| 34 | package OpenID::util; | ||
| 35 | BEGIN { | ||
| 36 | 6 | ▶ | $OpenID::util::VERSION = '1.030099_004'; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 37 | } | ||
| 38 | |||
| 39 | 6 | ▶ | use Crypt::DH::GMP; |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 40 | use Math::BigInt; | ||
| 41 | use Time::Local (); | ||
| 42 | use MIME::Base64 (); | ||
| 43 | use URI::Escape (); | ||
| 44 | |||
| 45 | use constant VERSION_1_NAMESPACE => "http://openid.net/signon/1.1"; | ||
| 46 | use constant VERSION_2_NAMESPACE => "http://specs.openid.net/auth/2.0"; | ||
| 47 | |||
| 48 | # I guess this is a bit daft since constants are subs anyway, | ||
| 49 | # but whatever. | ||
| 50 | sub version_1_namespace { | ||
| 51 | return VERSION_1_NAMESPACE; | ||
| 52 | } | ||
| 53 | sub version_2_namespace { | ||
| 54 | return VERSION_2_NAMESPACE; | ||
| 55 | } | ||
| 56 | sub version_1_xrds_service_url { | ||
| 57 | return VERSION_1_NAMESPACE; | ||
| 58 | } | ||
| 59 | sub version_2_xrds_service_url { | ||
| 60 | return "http://specs.openid.net/auth/2.0/signon"; | ||
| 61 | } | ||
| 62 | sub version_2_xrds_directed_service_url { | ||
| 63 | return "http://specs.openid.net/auth/2.0/server"; | ||
| 64 | } | ||
| 65 | sub version_2_identifier_select_url { | ||
| 66 | return "http://specs.openid.net/auth/2.0/identifier_select"; | ||
| 67 | } | ||
| 68 | |||
| 69 | sub parse_keyvalue { | ||
| 70 | my $reply = shift; | ||
| 71 | my %ret; | ||
| 72 | $reply =~ s/\r//g; | ||
| 73 | foreach (split /\n/, $reply) { | ||
| 74 | next unless /^(\S+?):(.*)/; | ||
| 75 | $ret{$1} = $2; | ||
| 76 | } | ||
| 77 | return %ret; | ||
| 78 | } | ||
| 79 | |||
| 80 | sub eurl | ||
| 81 | { | ||
| 82 | my $a = $_[0]; | ||
| 83 | $a =~ s/([^a-zA-Z0-9_\,\-.\/\\\: ])/uc sprintf("%%%02x",ord($1))/eg; | ||
| 84 | $a =~ tr/ /+/; | ||
| 85 | return $a; | ||
| 86 | } | ||
| 87 | |||
| 88 | sub push_url_arg { | ||
| 89 | my $uref = shift; | ||
| 90 | $$uref =~ s/[&?]$//; | ||
| 91 | my $got_qmark = ($$uref =~ /\?/); | ||
| 92 | |||
| 93 | while (@_) { | ||
| 94 | my $key = shift; | ||
| 95 | my $value = shift; | ||
| 96 | $$uref .= $got_qmark ? "&" : ($got_qmark = 1, "?"); | ||
| 97 | $$uref .= URI::Escape::uri_escape($key) . "=" . URI::Escape::uri_escape($value); | ||
| 98 | } | ||
| 99 | } | ||
| 100 | |||
| 101 | sub push_openid2_url_arg { | ||
| 102 | my $uref = shift; | ||
| 103 | my %args = @_; | ||
| 104 | push_url_arg($uref, | ||
| 105 | 'openid.ns' => VERSION_2_NAMESPACE, | ||
| 106 | map { | ||
| 107 | 'openid.'.$_ => $args{$_} | ||
| 108 | } keys %args, | ||
| 109 | ); | ||
| 110 | } | ||
| 111 | |||
| 112 | sub time_to_w3c { | ||
| 113 | my $time = shift || time(); | ||
| 114 | my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = gmtime($time); | ||
| 115 | $mon++; | ||
| 116 | $year += 1900; | ||
| 117 | |||
| 118 | return sprintf("%04d-%02d-%02dT%02d:%02d:%02dZ", | ||
| 119 | $year, $mon, $mday, | ||
| 120 | $hour, $min, $sec); | ||
| 121 | } | ||
| 122 | |||
| 123 | sub w3c_to_time { | ||
| 124 | my $hms = shift; | ||
| 125 | return 0 unless | ||
| 126 | $hms =~ /^(\d{4,4})-(\d\d)-(\d\d)T(\d\d):(\d\d):(\d\d)Z$/; | ||
| 127 | |||
| 128 | my $time; | ||
| 129 | eval { | ||
| 130 | $time = Time::Local::timegm($6, $5, $4, $3, $2 - 1, $1); | ||
| 131 | }; | ||
| 132 | return 0 if $@; | ||
| 133 | return $time; | ||
| 134 | } | ||
| 135 | |||
| 136 | sub int2bytes { | ||
| 137 | my ($int) = @_; | ||
| 138 | |||
| 139 | my $bigint = Math::BigInt->new($int); | ||
| 140 | |||
| 141 | die "Can't deal with negative numbers" if $bigint->is_negative; | ||
| 142 | |||
| 143 | my $bits = $bigint->as_bin; | ||
| 144 | die unless $bits =~ s/^0b//; | ||
| 145 | |||
| 146 | # prepend zeros to round to byte boundary, or to unset high bit | ||
| 147 | my $prepend = (8 - length($bits) % 8) || ($bits =~ /^1/ ? 8 : 0); | ||
| 148 | $bits = ("0" x $prepend) . $bits if $prepend; | ||
| 149 | |||
| 150 | return pack("B*", $bits); | ||
| 151 | } | ||
| 152 | |||
| 153 | sub int2arg { | ||
| 154 | return b64(int2bytes($_[0])); | ||
| 155 | } | ||
| 156 | |||
| 157 | sub b64 { | ||
| 158 | my $val = MIME::Base64::encode_base64($_[0]); | ||
| 159 | $val =~ s/\s+//g; | ||
| 160 | return $val; | ||
| 161 | } | ||
| 162 | |||
| 163 | sub d64 { | ||
| 164 | return MIME::Base64::decode_base64($_[0]); | ||
| 165 | } | ||
| 166 | |||
| 167 | sub bytes2int { | ||
| 168 | return Math::BigInt->new("0b" . unpack("B*", $_[0]))->bstr; | ||
| 169 | } | ||
| 170 | |||
| 171 | sub arg2int { | ||
| 172 | my ($arg) = @_; | ||
| 173 | return undef unless defined $arg and $arg ne ""; | ||
| 174 | # don't acccept base-64 encoded numbers over 700 bytes. which means | ||
| 175 | # those over 4200 bits. | ||
| 176 | return 0 if length($arg) > 700; | ||
| 177 | return bytes2int(MIME::Base64::decode_base64($arg)); | ||
| 178 | } | ||
| 179 | |||
| 180 | sub timing_indep_eq { | ||
| 181 | no warnings 'uninitialized'; | ||
| 182 | my ($x, $y)=@_; | ||
| 183 | warnings::warn('uninitialized','Use of uninitialized value in timing_indep_eq') | ||
| 184 | if (warnings::enabled('uninitialized') && !(defined($x) && defined($y))); | ||
| 185 | |||
| 186 | return '' if length($x)!=length($y); | ||
| 187 | |||
| 188 | my $n=length($x); | ||
| 189 | |||
| 190 | my $result=0; | ||
| 191 | for (my $i=0; $i<$n; $i++) { | ||
| 192 | $result |= ord(substr($x, $i, 1)) ^ ord(substr($y, $i, 1)); | ||
| 193 | } | ||
| 194 | |||
| 195 | return !$result; | ||
| 196 | } | ||
| 197 | |||
| 198 | sub get_dh { | ||
| 199 | my ($p, $g) = @_; | ||
| 200 | |||
| 201 | $p ||= "155172898181473697471232257763715539915724801966915404479707795314057629378541917580651227423698188993727816152646631438561595825688188889951272158842675419950341258706556549803580104870537681476726513255747040765857479291291572334510643245094715007229621094194349783925984760375594985848253359305585439638443"; | ||
| 202 | $g ||= "2"; | ||
| 203 | |||
| 204 | return if $p <= 10 or $g <= 1; | ||
| 205 | |||
| 206 | my $dh = Crypt::DH::GMP->new(p => $p, g => $g); | ||
| 207 | $dh->generate_keys; | ||
| 208 | |||
| 209 | return $dh; | ||
| 210 | } | ||
| 211 | |||
| 212 | # HTML parsing | ||
| 213 | sub _extract_head_markup_only { | ||
| 214 | my $htmlref = shift; | ||
| 215 | |||
| 216 | # kill all CDATA sections | ||
| 217 | $$htmlref =~ s/<!\[CDATA\[.*?\]\]>//sg; | ||
| 218 | |||
| 219 | # kill all comments | ||
| 220 | $$htmlref =~ s/<!--.*?-->//sg; | ||
| 221 | # ***FIX?*** Strictly speaking, SGML comments must have matched | ||
| 222 | # pairs of '--'s but almost nobody checks for this or even knows | ||
| 223 | |||
| 224 | # trim everything past the body. this is in case the user doesn't | ||
| 225 | # have a head document and somebody was able to inject their own | ||
| 226 | # head. -- brad choate | ||
| 227 | $$htmlref =~ s/<body\b.*//is; | ||
| 228 | } | ||
| 229 | |||
| 230 | 1; | ||