| 1 | $VERSION = "0.4"; | ||
| 2 | package News::Web::CookieAuth; | ||
| 3 | our $VERSION = "0.4"; | ||
| 4 | |||
| 5 | # -*- Perl -*- # Fri Oct 10 11:29:51 CDT 2003 | ||
| 6 | ############################################################################# | ||
| 7 | # Written by Tim Skirvin <tskirvin@killfile.org>. Copyright 2003, Tim | ||
| 8 | # Skirvin. Redistribution terms are below. | ||
| 9 | ############################################################################# | ||
| 10 | |||
| 11 | =head1 NAME | ||
| 12 | |||
| 13 | News::Web::CookieAuth - authentication for News::Web via cookies | ||
| 14 | |||
| 15 | =head1 SYNOPSIS | ||
| 16 | |||
| 17 | use News::Web::CookieAuth; | ||
| 18 | use CGI; | ||
| 19 | |||
| 20 | my $cookie = CGI->cookie('nntpauthinfo') || ""; | ||
| 21 | my $authinfo = News::Web::CookieAuth->new($cookie); | ||
| 22 | |||
| 23 | See 'setcookie.cgi' for a fairly comprehensive tutorial. | ||
| 24 | |||
| 25 | =head1 DESCRIPTION | ||
| 26 | |||
| 27 | This documentation is far from complete. However, the module itself is | ||
| 28 | just meant to be glue; if you're using it, you can probably spend some | ||
| 29 | time and work out how it works. | ||
| 30 | |||
| 31 | The cookie itself contains these fields: | ||
| 32 | |||
| 33 | nntpuser The NNTP user to connect to the server as | ||
| 34 | nntppass The NNTP password, in clear text (these things are | ||
| 35 | generally passed in cleartext anyway, and so shouldn't | ||
| 36 | be considered secure in the first place | ||
| 37 | realname The user's real name (half of the From: heaer) | ||
| 38 | emailadd The user's email address (half of the From: heaer) | ||
| 39 | signature The user's signature file | ||
| 40 | version Not currently used, but useful if we decide to use | ||
| 41 | different versions of the cookie. | ||
| 42 | |||
| 43 | =head1 USAGE | ||
| 44 | |||
| 45 | =cut | ||
| 46 | |||
| 47 | ############################################################################### | ||
| 48 | ### main() #################################################################### | ||
| 49 | ############################################################################### | ||
| 50 | |||
| 51 | use strict; | ||
| 52 | use CGI; | ||
| 53 | |||
| 54 | use vars qw( %HTMLFIELDS ); | ||
| 55 | %HTMLFIELDS = ( | ||
| 56 | 'nntpuser' => [ "NNTP Username", 'text', 30 ], | ||
| 57 | 'nntppass' => [ "NNTP Password", 'password', 30 ], | ||
| 58 | 'emailadd' => [ "Email Address", 'text', 30 ], | ||
| 59 | 'realname' => [ "Real Name", 'text', 30 ], | ||
| 60 | 'signature' => [ "Signature", 'textarea', 4, 80 ], | ||
| 61 | 'version' => [ '', 'hidden'], # Hidden field | ||
| 62 | # Possible other fields for the future - killfile, newsrc, select, | ||
| 63 | # scorefile | ||
| 64 | ); | ||
| 65 | |||
| 66 | =head2 Functions | ||
| 67 | |||
| 68 | =over 4 | ||
| 69 | |||
| 70 | =item new ( [COOKIE] ) | ||
| 71 | |||
| 72 | Creates the News::Web::CookieAuth object, based on the string C<COOKIE>. | ||
| 73 | If not passed, then you can later add it with read_cookie(). | ||
| 74 | |||
| 75 | =cut | ||
| 76 | |||
| 77 | sub new { | ||
| 78 | my ($proto, $cookie, %other) = @_; | ||
| 79 | my $class = ref($proto) || $proto; | ||
| 80 | my $self = { 'cookie' => $cookie }; | ||
| 81 | bless $self, $class; | ||
| 82 | $self->read_cookie(); | ||
| 83 | } | ||
| 84 | |||
| 85 | =item version ( [DATA] ) | ||
| 86 | |||
| 87 | =item nntpuser ( [DATA] ) | ||
| 88 | |||
| 89 | =item nntppass ( [DATA] ) | ||
| 90 | |||
| 91 | =item realname ( [DATA] ) | ||
| 92 | |||
| 93 | =item emailadd ( [DATA] ) | ||
| 94 | |||
| 95 | =item signature ( [DATA] ) | ||
| 96 | |||
| 97 | Reads or manipulates the given field. If C<DATA> is passed, sets the | ||
| 98 | field as appropriate; either way, it returns the value of the field. | ||
| 99 | |||
| 100 | =cut | ||
| 101 | |||
| 102 | sub version { defined $_[1] ? $_[0]->{version} = $_[1] | ||
| 103 | : $_[0]->{version} || 0 } | ||
| 104 | sub nntpuser { defined $_[1] ? $_[0]->{nntpuser} = $_[1] | ||
| 105 | : $_[0]->{nntpuser} || 0 } | ||
| 106 | sub nntppass { defined $_[1] ? $_[0]->{nntppass} = $_[1] | ||
| 107 | : $_[0]->{nntppass} || 0 } | ||
| 108 | sub realname { defined $_[1] ? $_[0]->{realname} = $_[1] | ||
| 109 | : $_[0]->{realname} || 0 } | ||
| 110 | sub emailadd { defined $_[1] ? $_[0]->{emailadd} = $_[1] | ||
| 111 | : $_[0]->{emailadd} || 0 } | ||
| 112 | sub signature { defined $_[1] ? $_[0]->{signature} = $_[1] | ||
| 113 | : $_[0]->{signature} || 0 } | ||
| 114 | |||
| 115 | =item cookie () | ||
| 116 | |||
| 117 | Returns or manipulates the cookie itself, as above. Not as good an idea | ||
| 118 | to do this, since without read_cookie() the other values are not | ||
| 119 | manipulated. | ||
| 120 | |||
| 121 | =cut | ||
| 122 | |||
| 123 | sub cookie { defined $_[1] ? $_[0]->{cookie} = $_[1] | ||
| 124 | : $_[0]->{cookie} || 0 } | ||
| 125 | |||
| 126 | =item set () | ||
| 127 | |||
| 128 | =cut | ||
| 129 | |||
| 130 | sub set { | ||
| 131 | my ($self, $field, $value) = @_; | ||
| 132 | delete $self->{lc $field} unless $value; | ||
| 133 | $self->{lc $field} = $value; | ||
| 134 | } | ||
| 135 | |||
| 136 | =item value ( FIELD ) | ||
| 137 | |||
| 138 | Reads the specific C<FIELD>. | ||
| 139 | |||
| 140 | =cut | ||
| 141 | |||
| 142 | sub value { my ($self, $field) = @_; $self->{lc $field} || ""; } | ||
| 143 | |||
| 144 | =item make_cookie () | ||
| 145 | |||
| 146 | =cut | ||
| 147 | |||
| 148 | sub make_cookie { | ||
| 149 | my ($self, $cookie) = @_; $cookie ||= $self->cookie; | ||
| 150 | join("::", _escape($self->nntpuser, $self->nntppass, | ||
| 151 | $self->realname, $self->emailadd, | ||
| 152 | $self->signature, $self->version)); | ||
| 153 | } | ||
| 154 | |||
| 155 | =item read_cookie ( [COOKIE] ) | ||
| 156 | |||
| 157 | Reads the information from C<COOKIE> (defaults to the value of cookie()) | ||
| 158 | into the object. | ||
| 159 | |||
| 160 | =cut | ||
| 161 | |||
| 162 | sub read_cookie { | ||
| 163 | my ($self, $cookie) = @_; $cookie ||= $self->cookie; | ||
| 164 | my ($nntpuser, $nntppass, $realname, $emailadd, $signature, $version) | ||
| 165 | = _unescape(split("::", $cookie)); | ||
| 166 | |||
| 167 | $self->nntpuser($nntpuser); $self->nntppass($nntppass); | ||
| 168 | $self->realname($realname); $self->emailadd($emailadd); | ||
| 169 | $self->signature($signature); $self->version($version); | ||
| 170 | |||
| 171 | $self; | ||
| 172 | } | ||
| 173 | |||
| 174 | =item fields () | ||
| 175 | |||
| 176 | Returns a hash of the fields stored in the object. | ||
| 177 | |||
| 178 | Returns either either a hashref (when invoked in scalar context) or the | ||
| 179 | hash itself. | ||
| 180 | |||
| 181 | =cut | ||
| 182 | |||
| 183 | sub fields { | ||
| 184 | my ($self) = @_; | ||
| 185 | my %return; | ||
| 186 | foreach (keys %HTMLFIELDS) { | ||
| 187 | $return{$_} = @{$HTMLFIELDS{$_}}[0] | ||
| 188 | } | ||
| 189 | wantarray ? %return : \%return; | ||
| 190 | } | ||
| 191 | |||
| 192 | =item html () | ||
| 193 | |||
| 194 | =cut | ||
| 195 | |||
| 196 | sub html { | ||
| 197 | my ($self, $field, $default) = @_; | ||
| 198 | return "" unless ($field); | ||
| 199 | my ($desc, $type, @args) = @{$HTMLFIELDS{$field}}; | ||
| 200 | if ($type eq 'text') { | ||
| 201 | CGI->textfield($field, $default || "", @args); | ||
| 202 | } elsif ($type eq 'password') { | ||
| 203 | CGI->password_field($field, $default || "", @args); | ||
| 204 | } elsif ($type eq 'textarea') { | ||
| 205 | CGI->textarea(-name=>$field, -default=>$default || "", | ||
| 206 | -rows=>$args[0] || 2, -cols=> $args[1] || 80, | ||
| 207 | -wrap=>$args[2] || 'hard'); | ||
| 208 | } | ||
| 209 | } | ||
| 210 | |||
| 211 | ############################################################################### | ||
| 212 | ### Internal Functions ######################################################## | ||
| 213 | ############################################################################### | ||
| 214 | |||
| 215 | ### _escape ( @_ ) | ||
| 216 | # This is not enough escaping and I know it. Grr. | ||
| 217 | sub _escape { | ||
| 218 | map { $_ =~ s/\\/\\\\/g } @_; | ||
| 219 | map { $_ =~ s/::/:\\:/g } @_; | ||
| 220 | @_; | ||
| 221 | } | ||
| 222 | |||
| 223 | ### _unescape ( @_ ) | ||
| 224 | sub _unescape { | ||
| 225 | map { $_ =~ s/:\\:/::/g } @_; | ||
| 226 | map { $_ =~ s/\\\\/\\/g } @_; | ||
| 227 | @_; | ||
| 228 | } | ||
| 229 | |||
| 230 | =head1 REQUIREMENTS | ||
| 231 | |||
| 232 | B<CGI.pm> | ||
| 233 | |||
| 234 | =head1 SEE ALSO | ||
| 235 | |||
| 236 | B<CGI.pm>, B<News::Web> | ||
| 237 | |||
| 238 | =head1 NOTES | ||
| 239 | |||
| 240 | This is hardly the ideal form of authentication; however, it's a lot | ||
| 241 | simpler to implement generally than a database-backed solution would be. | ||
| 242 | This doesn't change the fact that I'd rather use the database-backed | ||
| 243 | solution in the future, though, which is why this module is fairly general | ||
| 244 | and could be re-implemented as some other class... | ||
| 245 | |||
| 246 | =head1 AUTHOR | ||
| 247 | |||
| 248 | Tim Skirvin <tskirvin@killfile.org> | ||
| 249 | |||
| 250 | =head1 COPYRIGHT | ||
| 251 | |||
| 252 | Copyright 2003 by Tim Skirvin <tskirvin@killfile.org>. This code may be | ||
| 253 | distributed under the same terms as Perl itself. | ||
| 254 | |||
| 255 | =cut | ||
| 256 | |||
| 257 | 1; | ||
| 258 | |||
| 259 | ############################################################################### | ||
| 260 | ### Version History ########################################################### | ||
| 261 | ############################################################################### | ||
| 262 | # v0.3b Fri Oct 10 16:12:45 CDT 2003 | ||
| 263 | ### First commented version. | ||
| 264 | # v0.4 Thu Apr 22 14:11:58 CDT 2004 | ||
| 265 | ### Code clean-up. |