lib/Yote/YapiServer/Site.pm

filter: Statementstmt 96.6% 117 / 121 Branchbran 91.3% 42 / 46 Conditioncond 100% 12 / 12 MC/DCmcdc 100% 8 / 8 Subroutinesub 94.1% 16 / 17 Podpod 66.6% 6 / 9 totaltotal 94.3% 201 / 213 CC 29 SCAR 17.8
CC
29
Cov
96%
createUser
12.0
login
7.0
init
5.0
CRAP 5.9
« lib/Yote/YapiServer/YapiDef.pm ↑ Summary Build »
1 package Yote::YapiServer::Site;
2  
3 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use base 'Yote::YapiServer::BaseObj';
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 1 ▶use Digest::MD5;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use Time::Piece;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 1 ▶use Yote::YapiServer::User;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use Yote::YapiServer::Session;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 1 ▶use Yote::YapiServer::App::Base;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 # Database column definitions
15 1 our %cols = (
16 created => 'TIMESTAMP DEFAULT CURRENT_TIMESTAMP',
17 apps => '*HASH<64>_*HASH<16>_*', # app_name => { version => App object }
18 users => '*HASH<125>_*Yote::YapiServer::User', # handle => User
19 users_by_email => '*HASH<125>_*Yote::YapiServer::User', # email => User
20 sessions => '*HASH<128>_*Yote::YapiServer::Session', # token => Session
21 );
22  
23 # Installed applications - add your apps here
24 1 our %INSTALLED_APPS = (
25 example => 'Yote::YapiServer::App::Example',
26 );
27  
28 # Rate limiting configuration
29 1 our %RATE_LIMITS = (
30 createUser => { per_ip => 5, window => 3600 }, # 5 per hour
31 login => { per_ip => 10, window => 300 }, # 10 per 5 min
32 default => { per_session => 100, window => 60 }, # 100 per min
33 );
34  
35 #----------------------------------------------------------------------
36 # Initialization
37 #----------------------------------------------------------------------
38  
39 sub installed_apps {
40 2 ▶ return \%INSTALLED_APPS;
Subroutine installed_apps: called CC=1 SCAR=0.0
Pod undocumented
41 }
42  
43 sub init {
44 1 ▶ my ($self) = @_;
Subroutine init: called CC=5 SCAR=16.2
Pod documented
45 1 my $store = $self->store;
46 1 my $installed = $self->installed_apps;
47 1 my $apps = $self->get_apps;
48  
49 1 for my $app_name (keys %$installed) {
50 1 my $app_class = $installed->{$app_name};
51 1 eval "require $app_class";
52 1 ▶ die "Failed to load $app_class: $@" if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 1 1
53  
54 1 my $version = $app_class->version;
55  
56 # get or create the version hash for this app name
57 1 my $version_hash = $apps->{$app_name};
58 1 ▶ unless ($version_hash) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($version_hash) 1 0 1
59 1 $version_hash = $store->new_hash('*HASH<16>_*');
60 1 $apps->{$app_name} = $version_hash;
61 }
62  
63 # create the app object if this version doesn't exist yet
64 1 ▶ unless ($version_hash->{$version}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($version_hash->{$version}) 1 0 1
65 1 my $app = $store->new_obj($app_class,
66 app_name => $app_name,
67 app_version => $version,
68 );
69 1 $version_hash->{$version} = $app;
70 }
71 }
72  
73 1 $store->save;
74 1 return $self;
75 }
76  
77 #----------------------------------------------------------------------
78 # App access
79 #----------------------------------------------------------------------
80  
81 sub get_app {
82 3 ▶ my ($self, $app_name, $version) = @_;
Subroutine get_app: called CC=4 SCAR=14.0
Pod documented
83 3 my $version_hash = $self->get_apps->{$app_name};
84 3 ▶ return unless $version_hash;
Branch: 1 branch
BranchTrueFalseTotal
unless $version_hash 1 2 3
85  
86 2 ▶ if (defined $version) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $version) 1 1 2
87 1 return $version_hash->{$version};
88 }
89  
90 # default to latest (highest) version
91 1 my @versions = sort { $b <=> $a } keys %$version_hash;
92 1 ▶ return $version_hash->{$versions[0]} if @versions;
Branch: 1 branch
BranchTrueFalseTotal
if @versions 1 0 1
93 0 return;
94 }
95  
96 sub list_apps {
97 1 ▶ my ($self) = @_;
Subroutine list_apps: called CC=1 SCAR=0.0
Pod undocumented
98 1 return [ sort keys %{$self->installed_apps} ];
99 }
100  
101 #----------------------------------------------------------------------
102 # User management
103 #----------------------------------------------------------------------
104  
105 sub createUser {
106 9 ▶ my ($self, $args, $context) = @_;
Subroutine createUser: called CC=12 SCAR=24.8
Pod documented
107 9 my $store = $self->store;
108  
109 9 my $handle = $args->{handle};
110 9 my $email = $args->{email};
111 9 my $password = $args->{password};
112  
113 # Validation
114 9 ▶ return 0, "handle required" unless $handle;
Branch: 1 branch
BranchTrueFalseTotal
unless $handle 1 8 9
115 8 ▶ return 0, "password required" unless $password;
Branch: 1 branch
BranchTrueFalseTotal
unless $password 1 7 8
116 7 ▶ return 0, "password must be at least 8 characters"
Branch: 1 branch
BranchTrueFalseTotal
if length $password < 8 1 6 7
117 if length($password) < 8;
118 6 ▶ return 0, "handle must be alphanumeric"
Branch: 1 branch
BranchTrueFalseTotal
unless $handle =~ /^[a-zA-Z0-9_]+$/ 1 5 6
119 unless $handle =~ /^[a-zA-Z0-9_]+$/;
120 5 ▶ return 0, "invalid email format"
Branch: 1 branch
BranchTrueFalseTotal
if $email and not $email =~ /^[^\@]+\@[^\@]+\.[^\@]+$/ 1 4 5
Condition100%3/3
$email and not $email =~ /^[^\@]+\@[^\@]+\.[^\@]+$/
!ll&&!rl&&r
221
MC/DC100%2/2
$email and not $email =~ /^[^\@]+\@[^\@]+\.[^\@]+$/
$email not $email =~ /^[^\@]+\@[^\@]+\.[^\@]+$/
Truth table100%3/3
A and not(B)
A= $emailB= not $email =~ /^[^\@]+\@[^\@]+\.[^\@]+$/
ABresult
0X0
100
111
121 if $email && $email !~ /^[^@]+@[^@]+\.[^@]+$/;
122  
123 4 eval {
124 # Check handle uniqueness
125 4 ▶ if ($store->fetch_path('users', lc($handle))) {
Branch: 1 branch
BranchTrueFalseTotal
if ($store->fetch_path("users", lc $handle)) 1 3 4
126 1 die "handle already taken\n";
127 }
128  
129 # Check email uniqueness
130 3 ▶ if ($email && $store->fetch_path('users_by_email', lc($email))) {
Branch: 1 branch
BranchTrueFalseTotal
if ($email and $store->fetch_path("users_by_email", lc $email)) 1 2 3
Condition100%3/3
$email and $store->fetch_path("users_by_email", lc $email)
!ll&&!rl&&r
111
MC/DC100%2/2
$email and $store->fetch_path("users_by_email", lc $email)
$email $store->fetch_path("users_by_email", lc $email)
Truth table100%3/3
A and B
A= $emailB= $store->fetch_path("users_by_email", lc $email)
ABresult
0X0
100
111
131 1 die "email already registered\n";
132 }
133  
134 # Create user
135 2 my $user = $store->new_obj('Yote::YapiServer::User',
136 handle => $handle,
137 email => $email,
138 );
139 2 $user->set_password($password);
140  
141 # Index user
142 2 $store->set_path('users', lc($handle), $user);
143 2 ▶ if ($email) {
Branch: 1 branch
BranchTrueFalseTotal
if ($email) 1 1 2
144 1 $store->set_path('users_by_email', lc($email), $user);
145 }
146  
147 2 $store->save;
148 };
149  
150 4 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 2 2 4
151 2 chomp(my $error = $@);
152 2 return 0, $error;
153 }
154  
155 # Auto-login after registration
156 2 return $self->login({
157 handle => $handle,
158 password => $password,
159 }, $context);
160 }
161  
162 sub login {
163 9 ▶ my ($self, $args, $context) = @_;
Subroutine login: called CC=7 SCAR=19.5
Pod documented
164 9 my $store = $self->store;
165  
166 9 ▶ my $handle_or_email = $args->{handle} // $args->{email};
Condition100%3/3
$args->{"handle"} // $args->{"email"}
l!l&&r!l&&!r
711
MC/DC100%2/2
$args->{"handle"} // $args->{"email"}
$args->{"handle"} $args->{"email"}
Truth table100%3/3
A // B
A= $args->{"handle"}B= $args->{"email"}
ABresult
1X1
011
000
167 9 my $password = $args->{password};
168  
169 9 ▶ return 0, "handle/email required" unless $handle_or_email;
Branch: 1 branch
BranchTrueFalseTotal
unless $handle_or_email 1 8 9
170 8 ▶ return 0, "password required" unless $password;
Branch: 1 branch
BranchTrueFalseTotal
unless $password 1 7 8
171  
172 # Look up user
173 7 ▶ my $user = $store->fetch_path('users', lc($handle_or_email))
Condition100%3/3
$store->fetch_path("users", lc $handle_or_email) // $store->fetch_path("users_by_email", lc $handle_or_email)
l!l&&r!l&&!r
511
MC/DC100%2/2
$store->fetch_path("users", lc $handle_or_email) // $store->fetch_path("users_by_email", lc $handle_or_email)
$store->fetch_path("users", lc $handle_or_email) $store->fetch_path("users_by_email", lc $handle_or_email)
Truth table100%3/3
A // B
A= $store->fetch_path("users", lc $handle_or_email)B= $store->fetch_path("users_by_email", lc $handle_or_email)
ABresult
1X1
011
000
174 // $store->fetch_path('users_by_email', lc($handle_or_email));
175  
176 7 ▶ return 0, "invalid credentials" unless $user;
Branch: 1 branch
BranchTrueFalseTotal
unless $user 1 6 7
177 6 ▶ return 0, "invalid credentials"
Branch: 1 branch
BranchTrueFalseTotal
unless $user->verify_password($password) 1 5 6
178 unless $user->verify_password($password);
179  
180 # Create session
181 5 my $token = Yote::YapiServer::Session->generate_token();
182 5 my $expires = Yote::YapiServer::Session->calculate_expiry();
183  
184 my $session = $store->new_obj('Yote::YapiServer::Session',
185 token => $token,
186 user => $user,
187 expires => $expires,
188 ip_address => $context->{ip_address},
189 5 );
190 5 $session->touch;
191  
192 # Store session by token
193 5 $store->set_path('sessions', $token, $session);
194  
195 # Update user last login
196 5 $user->set_last_login(localtime->strftime("%Y-%m-%d %H:%M:%S"));
197  
198 5 $store->save;
199  
200 # Expose user to session
201 5 $session->expose_object($user);
202  
203 5 return 1, $user, { token => $token };
204 }
205  
206 sub logout {
207 2 ▶ my ($self, $args, $context) = @_;
Subroutine logout: called CC=2 SCAR=6.9
Pod documented
208 2 my $store = $self->store;
209 2 my $session = $context->{session};
210  
211 2 ▶ return 0, "not logged in" unless $session;
Branch: 1 branch
BranchTrueFalseTotal
unless $session 1 1 2
212  
213 1 my $token = $session->get_token;
214 1 $store->del_path('sessions', $token);
215 1 $store->save;
216  
217 1 return 1;
218 }
219  
220 sub validateToken {
221 6 ▶ my ($self, $token) = @_;
Subroutine validateToken: called CC=4 SCAR=13.9
Pod documented
222 6 ▶ return unless $token;
Branch: 1 branch
BranchTrueFalseTotal
unless $token 2 4 6
223  
224 4 my $store = $self->store;
225 4 my $session = $store->fetch_path('sessions', $token);
226  
227 4 ▶ return unless $session;
Branch: 1 branch
BranchTrueFalseTotal
unless $session 1 3 4
228 3 ▶ return if $session->is_expired;
Branch: 1 branch
BranchTrueFalseTotal
if $session->is_expired 1 2 3
229  
230 2 $session->touch;
231 2 return $session;
232 }
233  
234 #----------------------------------------------------------------------
235 # Session cleanup
236 #----------------------------------------------------------------------
237  
238 sub cleanup_expired_sessions {
239 0 ▶ my ($self) = @_;
Subroutine cleanup_expired_sessions: not called CC=1 SCAR=6.9
Pod undocumented
240 0 my $store = $self->store;
241  
242 # This would need implementation based on how sessions are indexed
243 # For now, sessions are checked on access via is_expired()
244  
245 0 return 1;
246 }
247  
248 1 1;
249  
250 __END__
« lib/Yote/YapiServer/YapiDef.pm ↑ Summary Build »