1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410
|
# BEGIN BPS TAGGED BLOCK {{{
#
# COPYRIGHT:
#
# This software is Copyright (c) 1996-2025 Best Practical Solutions, LLC
# <sales@bestpractical.com>
#
# (Except where explicitly superseded by other copyright notices)
#
#
# LICENSE:
#
# This work is made available to you under the terms of Version 2 of
# the GNU General Public License. A copy of that license should have
# been provided with this software, but in any event can be snarfed
# from www.gnu.org.
#
# This work is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
# 02110-1301 or visit their web page on the internet at
# http://www.gnu.org/licenses/old-licenses/gpl-2.0.html.
#
#
# CONTRIBUTION SUBMISSION POLICY:
#
# (The following paragraph is not intended to limit the rights granted
# to you to modify and distribute this software under the terms of
# the GNU General Public License and is only of importance to you if
# you choose to contribute your changes and enhancements to the
# community by submitting them to Best Practical Solutions, LLC.)
#
# By intentionally submitting any modifications, corrections or
# derivatives to this work, or any other work intended for use with
# Request Tracker, to Best Practical Solutions, LLC, you confirm that
# you are the copyright holder for those contributions and you grant
# Best Practical Solutions, LLC a nonexclusive, worldwide, irrevocable,
# royalty-free, perpetual, license to use, copy, create derivative
# works based on those contributions, and sublicense and distribute
# those contributions and any derivatives thereof.
#
# END BPS TAGGED BLOCK }}}
use strict;
use warnings;
use 5.10.1;
package RT::AuthToken;
use base 'RT::Record';
require RT::User;
require RT::Util;
use Digest::SHA 'sha512_hex';
=head1 NAME
RT::AuthToken - Represents an authentication token for a user
=cut
=head1 METHODS
=head2 Create PARAMHASH
Create takes a hash of values and creates a row in the database. Available
keys are:
=over 4
=item Owner
The user ID for whom this token will authenticate. If it's not the AuthToken
object's CurrentUser, then the AdminUsers permission is required.
=item Description
A human-readable description of what this token will be used for.
=item Expires
An optional date for when this token should expire.
=back
Returns a tuple of (status, msg) on failure and (id, msg, authstring) on
success. Note that this is the only time the authstring will be directly
readable (as it is stored in the database hashed like a password, so use
this opportunity to capture it.
=cut
sub Create {
my $self = shift;
my %args = (
Owner => undef,
Description => '',
@_,
);
return (0, $self->loc("Permission Denied"))
unless $self->CurrentUserHasRight('ManageAuthTokens');
return (0, $self->loc("Owner required"))
unless $args{Owner};
return (0, $self->loc("Permission Denied"))
unless $args{Owner} == $self->CurrentUser->Id
|| $self->CurrentUserHasRight('AdminUsers');
my $token = $self->_GenerateToken;
# delete Expires arg if it is empty
delete $args{Expires} unless $args{Expires};
my ( $id, $msg ) = $self->SUPER::Create(
Token => $self->_CryptToken($token),
map { $_ => $args{$_} } grep {exists $args{$_}}
qw(Owner Description Expires),
);
unless ($id) {
return (0, $self->loc("Authentication token create failed: [_1]", $msg));
}
my $authstring = $self->_BuildAuthString($self->Owner, $token);
return ($id, $self->loc('Authentication token created'), $authstring);
}
=head2 CurrentUserCanSee
Returns true if the current user can see the AuthToken
=cut
sub CurrentUserCanSee {
my $self = shift;
return 0 unless $self->CurrentUserHasRight('ManageAuthTokens');
return 0 unless $self->__Value('Owner') == $self->CurrentUser->Id
|| $self->CurrentUserHasRight('AdminUsers');
return 1;
}
=head2 SetOwner
Not permitted
=cut
sub SetOwner {
my $self = shift;
return (0, $self->loc("Permission Denied"));
}
=head2 SetToken
Not permitted
=cut
sub SetToken {
my $self = shift;
return (0, $self->loc("Permission Denied"));
}
=head2 Delete
Checks ACL
=cut
sub Delete {
my $self = shift;
return (0, $self->loc("Permission Denied")) unless $self->CurrentUserCanSee;
my ($ok, $msg) = $self->SUPER::Delete(@_);
return ($ok, $self->loc("Authentication token revoked.")) if $ok;
return ($ok, $msg);
}
=head2 UpdateLastUsed
Sets the "last used" time, without touching "last updated"
=cut
sub UpdateLastUsed {
my $self = shift;
my $now = RT::Date->new( $self->CurrentUser );
$now->SetToNow;
return $self->__Set(
Field => 'LastUsed',
Value => $now->ISO,
);
}
=head2 ParseAuthString AUTHSTRING
Class method that takes as input an authstring and provides a tuple
of (user id, token) on success, or the empty list on failure.
=cut
sub ParseAuthString {
my $class = shift;
my $input = shift;
my ($version) = $input =~ s/^([0-9]+)-//
or return;
if ($version == 1) {
my ($user_id, $token) = $input =~ /^([0-9]+)-([0-9a-f]{32})$/i
or return;
return ($user_id, $token);
}
return;
}
=head2 IsToken
Analogous to L<RT::User/IsPassword>, without all of the legacy password
forms.
=cut
sub IsToken {
my $self = shift;
my $value = shift;
# check if token has expired
if ( my $expires = $self->__Value('Expires') ) {
my $expiresObj = RT::Date->new( $self->CurrentUser );
my $nowObj = RT::Date->new( $self->CurrentUser );
$expiresObj->Set( Format => 'sql', Value => $expires );
$nowObj->SetToNow();
if ( $expiresObj->Unix < $nowObj->Unix ) {
$RT::Logger->debug("Auth Token has expired.");
return 0;
}
}
my $stored = $self->__Value('Token');
# If it's a new-style (>= RT 4.0) password, it starts with a '!'
my (undef, $method, @rest) = split /!/, $stored;
if ($method eq "bcrypt") {
if (RT::Util->can('constant_time_eq')) {
return 0 unless RT::Util::constant_time_eq(
$self->_CryptToken_bcrypt($value, @rest),
$stored,
);
} else {
return 0 unless $self->_CryptToken_bcrypt($value, @rest) eq $stored;
}
# Upgrade to a larger number of rounds if necessary
return 1 unless $rest[0] < RT->Config->Get('BcryptCost');
}
else {
$RT::Logger->warn("Unknown hash method $method");
return 0;
}
# We got here by validating successfully, but with a legacy
# password form. Update to the most recent form.
$self->_Set(Field => 'Token', Value => $self->_CryptToken($value));
return 1;
}
=head2 LastUsedObj
L</LastUsed> as an L<RT::Date> object.
=cut
sub LastUsedObj {
my $self = shift;
my $date = RT::Date->new($self->CurrentUser);
$date->Set(Format => 'sql', Value => $self->LastUsed);
return $date;
}
=head2 ExpiresObj
L</Expires> as an L<RT::Date> object.
=cut
sub ExpiresObj {
my $self = shift;
my $date = RT::Date->new($self->CurrentUser);
$date->Set(Format => 'sql', Value => $self->Expires)
if $self->Expires;
return $date;
}
=head1 PRIVATE METHODS
Documented for internal use only, do not call these from outside
RT::AuthToken itself.
=head2 _Set
Checks if the current user can I<ManageAuthTokens> before calling
C<SUPER::_Set>.
=cut
sub _Set {
my $self = shift;
my %args = (
Field => undef,
Value => undef,
@_
);
return (0, $self->loc("Permission Denied"))
unless $self->CurrentUserCanSee;
return $self->SUPER::_Set(@_);
}
=head2 _Value
Checks L</CurrentUserCanSee> before calling C<SUPER::_Value>.
=cut
sub _Value {
my $self = shift;
return unless $self->CurrentUserCanSee;
return $self->SUPER::_Value(@_);
}
=head2 _GenerateToken
Generates an unpredictable auth token
=cut
sub _GenerateToken {
my $class = shift;
require Time::HiRes;
my $input = join '',
Time::HiRes::time(), # subsecond-precision time
{}, # unpredictable memory address
rand(); # RNG
my $digest = sha512_hex($input);
return substr($digest, 0, 32);
}
=head2 _BuildAuthString
Takes a user id and token and provides an authstring for use in place of
a (username, password) combo.
=cut
sub _BuildAuthString {
my $self = shift;
my $version = 1;
my $userid = shift;
my $token = shift;
return $version . '-' . $userid . '-' . $token;
}
sub _CryptToken_bcrypt {
my $self = shift;
return $self->CurrentUser->UserObj->_GeneratePassword_bcrypt(@_);
}
sub _CryptToken {
my $self = shift;
return $self->_CryptToken_bcrypt(@_);
}
sub Table { "AuthTokens" }
sub _CoreAccessible {
{
id => { read => 1, type => 'int(11)', default => '' },
Owner => { read => 1, type => 'int(11)', default => '0' },
Token => { read => 1, sql_type => 12, length => 256, is_blob => 0, is_numeric => 0, type => 'varchar(256)', default => ''},
Description => { read => 1, type => 'varchar(255)', default => '', write => 1 },
LastUsed => { read => 1, type => 'datetime', default => '', write => 1 },
Creator => { read => 1, type => 'int(11)', default => '0', auto => 1 },
Created => { read => 1, type => 'datetime', default => '', auto => 1 },
LastUpdatedBy => { read => 1, type => 'int(11)', default => '0', auto => 1 },
LastUpdated => { read => 1, type => 'datetime', default => '', auto => 1 },
Expires => { read => 1, type => 'datetime', default => '', auto => 1 },
}
}
RT::Base->_ImportOverlays();
1;
|