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
|
package TestDataHandler;
use strict;
use warnings;
use parent 'OAuth::Lite2::Server::DataHandler';
use String::Random;
use OAuth::Lite2::Server::Error;
use OAuth::Lite2::Model::AuthInfo;
use OAuth::Lite2::Model::AccessToken;
use OAuth::Lite2::Model::ServerState;
my %ID_POD = (
auth_info => 0,
access_token => 0,
user => 0,
server_state => 0,
ext_account => 0,
);
my %AUTH_INFO;
my %ACCESS_TOKEN;
my %DEVICE_CODE;
my %CLIENTS;
my %USERS;
my %SERVER_STATE;
my %EXT_ACCOUNT;
sub clear {
my $class = shift;
%AUTH_INFO = ();
%ACCESS_TOKEN = ();
%DEVICE_CODE = ();
%CLIENTS = ();
%USERS = ();
%EXT_ACCOUNT = ();
}
sub gen_next_auth_info_id {
my $class = shift;
$ID_POD{auth_info}++;
}
sub gen_next_user_id {
my $class = shift;
$ID_POD{user}++;
}
sub gen_next_access_token_id {
my $class = shift;
$ID_POD{access_token}++;
}
sub gen_next_server_state_id {
my $class = shift;
$ID_POD{server_state}++;
}
sub add_client {
my ($class, %args) = @_;
$CLIENTS{ $args{id} } = {
secret => $args{secret},
user_id => $args{user_id} || 0,
group_id => $args{group_id} || undef,
};
}
sub add_user {
my ($class, %args) = @_;
$USERS{ $args{username} } = {
password => $args{password},
};
}
sub add_ext_account {
my ($class, %args) = @_;
$EXT_ACCOUNT{ $args{assertion} } = {
id => $args{id},
client_id => $args{client_id},
type => $args{type},
iss => $args{iss},
aud => $args{aud},
};
}
sub init {
my $self = shift;
}
sub get_user_id {
my ($self, $username, $password) = @_;
return unless ($username && exists $USERS{$username});
return unless ($password && $USERS{$username}{password} eq $password);
return $username;
}
sub get_client_user_id {
my ($self, $client_id) = @_;
return unless ($client_id && exists $CLIENTS{$client_id});
return $CLIENTS{$client_id}{user_id};
}
# TODO needed?
sub get_client_by_id {
my ($self, $client_id) = @_;
return unless ($client_id && exists $CLIENTS{$client_id});
return $CLIENTS{$client_id};
}
# called in following flows:
# - refresh
sub get_auth_info_by_refresh_token {
my ($self, $refresh_token) = @_;
for my $id (keys %AUTH_INFO) {
my $auth_info = $AUTH_INFO{$id};
return $auth_info if $auth_info->{refresh_token} eq $refresh_token;
}
return;
}
sub get_auth_info_by_id {
my ($self, $auth_id) = @_;
return $AUTH_INFO{$auth_id};
}
# called in following flows:
# - device_token
sub get_auth_info_by_code {
my ($self, $device_code) = @_;
for my $id (keys %AUTH_INFO) {
my $auth_info = $AUTH_INFO{$id};
return $auth_info if ($auth_info->code && $auth_info->code eq $device_code);
}
return;
}
sub create_or_update_auth_info {
my ($self, %params) = @_;
my $client_id = $params{client_id};
my $user_id = $params{user_id};
my $scope = $params{scope};
my $code = $params{code};
my $redirect_uri = $params{redirect_uri};
my $server_state = $params{server_state};
my $id = ref($self)->gen_next_auth_info_id();
my $refresh_token = sprintf q{refresh_token_%d}, $id;
my $auth_info = OAuth::Lite2::Model::AuthInfo->new({
id => $id,
client_id => $client_id,
user_id => $user_id,
scope => $scope,
refresh_token => $refresh_token,
});
$auth_info->code($code) if $code;
$auth_info->redirect_uri($redirect_uri) if $redirect_uri;
$auth_info->server_state($server_state) if $server_state;
$AUTH_INFO{$id} = $auth_info;
return $auth_info;
}
# called in following flows:
# - refresh
sub create_or_update_access_token {
my ($self, %params) = @_;
my $auth_info = $params{auth_info};
my $auth_id = $auth_info->id;
my $id = ref($self)->gen_next_access_token_id();
my $token = sprintf q{access_token_%d}, $id;
my %attrs = (
auth_id => $auth_id,
token => $token,
expires_in => $params{expires_in} || 3600,
created_on => time(),
);
my $access_token = OAuth::Lite2::Model::AccessToken->new(\%attrs);
$ACCESS_TOKEN{$auth_id} = $access_token;
return $access_token;
}
sub get_access_token {
my ($self, $token) = @_;
for my $auth_id ( keys %ACCESS_TOKEN ) {
my $t = $ACCESS_TOKEN{ $auth_id };
if ($t->token eq $token) {
return $t;
}
}
return;
}
sub validate_client {
my ($self, $client_id, $client_secret, $type) = @_;
return 0 unless exists $CLIENTS{ $client_id };
my $client = $CLIENTS{ $client_id };
return 1 if ( $type eq q{server_state} && $client );
return 0 unless $client->{secret} eq $client_secret;
if ($client_id eq 'aaa') {
if ($type eq 'basic-credentials') {
return 1;
} else {
return 0;
}
} else {
return 1;
}
}
sub validate_client_by_id {
my ($self, $client_id) = @_;
return ($client_id ne 'malformed');
}
sub validate_user_by_id {
my ($self, $user_id) = @_;
return ($user_id ne 666);
}
sub get_group_id_by_client_id {
my ($self, $client_id) = @_;
return $CLIENTS{$client_id}{group_id};
}
sub validate_grouping_scope {
my ($self, $client_id, $scope) = @_;
my @scopes;
@scopes = split /\s/, $scope if ( $scope );
return (grep {$_ eq q{grouping_scope}} @scopes);
}
sub create_server_state {
my ($self, %params) = @_;
my $id = ref($self)->gen_next_server_state_id();
my %attrs = (
client_id => $params{client_id},
server_state => "server_state_".$id,
expires_in => $params{expires_in} || 3600,
created_on => time(),
);
my $state = OAuth::Lite2::Model::ServerState->new(\%attrs);
$SERVER_STATE{$state->server_state} = $state;
return $state;
}
sub get_user_id_by_external_assertion{
my ($self, %params) = @_;
return unless ($params{assertion} && exists $EXT_ACCOUNT{$params{assertion}});
return unless ($params{client_id} && $EXT_ACCOUNT{$params{assertion}}{client_id} eq $params{client_id});
if ($EXT_ACCOUNT{$params{assertion}}{type}) {
return unless ($EXT_ACCOUNT{$params{assertion}}{type} eq $params{type});
}
if ($EXT_ACCOUNT{$params{assertion}}{iss}) {
return unless ($EXT_ACCOUNT{$params{assertion}}{iss} eq $params{iss});
}
if ($EXT_ACCOUNT{$params{assertion}}{aud}) {
return unless ($EXT_ACCOUNT{$params{assertion}}{aud} eq $params{aud});
}
return $EXT_ACCOUNT{$params{assertion}}{id};
}
1;
|