SCRAM/Server.pm

filter: Statementstmt 92.5% 87 / 94 Branchbran 80.7% 21 / 26 Conditioncond 66.6% 2 / 3 MC/DCmcdc 50.0% 1 / 2 Subroutinesub 88.2% 15 / 17 Podpod 100% 3 / 3 totaltotal 88.9% 129 / 145 CC 15 SCAR 13.4
CC
15
Cov
89%
final_msg
7.2
first_msg
6.1
authorization_id
4.0
CRAP 3.8
↑ Summary SCRAM/Role/Common.pm »
1 3 ▶use 5.008;
Subroutine BEGIN: called CC=1 SCAR=0.0
2 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 package Authen::SCRAM::Server;
6 # ABSTRACT: RFC 5802 SCRAM Server
7  
8 3 our $VERSION = '0.011';
9  
10 3 ▶use Moo 1.001000;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 3 ▶use Authen::SASL::SASLprep qw/saslprep/;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 3 ▶use Carp qw/croak/;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 3 ▶use Crypt::URandom qw/urandom/;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 3 ▶use Encode qw/encode_utf8/;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 3 ▶use MIME::Base64 qw/decode_base64/;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 3 ▶use PBKDF2::Tiny 0.003 qw/derive digest_fcn hmac/;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 3 ▶use Types::Standard qw/Str Num CodeRef Bool/;
Subroutine BEGIN: called CC=1 SCAR=0.0
19  
20 3 ▶use namespace::clean;
Subroutine BEGIN: called CC=1 SCAR=0.0
21  
22 3 with 'Authen::SCRAM::Role::Common';
23  
24 #--------------------------------------------------------------------------#
25 # public attributes
26 #--------------------------------------------------------------------------#
27  
28 #pod =attr credential_cb (required)
29 #pod
30 #pod This attribute must contain a code reference that takes a username (as a
31 #pod character string normalized by SASLprep) and returns the four user-credential
32 #pod parameters required by SCRAM: C<salt>, C<StoredKey>, C<ServerKey>, and
33 #pod C<iteration count>. The C<salt>, C<StoredKey> and C<ServerKey> must be
34 #pod provided as octets (i.e. B<NOT> base64 encoded).
35 #pod
36 #pod If the username is unknown, it should return an empty list.
37 #pod
38 #pod ($salt, $stored_key, $server_key, $iterations) =
39 #pod $server->credential_cb->( $username );
40 #pod
41 #pod See L<RFC 5802: SCRAM Algorithm Overview|http://tools.ietf.org/html/rfc5802#section-3>
42 #pod for details.
43 #pod
44 #pod =cut
45  
46 3 has credential_cb => (
47 is => 'ro',
48 isa => CodeRef,
49 required => 1,
50 );
51  
52 #pod =attr auth_proxy_cb
53 #pod
54 #pod If provided, this attribute must contain a code reference that takes an
55 #pod B<authentication> username and a B<authorization> username (both as character
56 #pod strings), and return a true value if the authentication username is permitted
57 #pod to act as the authorization username:
58 #pod
59 #pod $bool = $server->auth_proxy_cb->(
60 #pod $authentication_user, $authorization_user
61 #pod );
62 #pod
63 #pod It will only be all called if the authentication username has successfully
64 #pod authenticated. Both usernames will have been normalized via C<SASLprep> with
65 #pod any transport encoding removed before being passed to this function.
66 #pod
67 #pod =cut
68  
69 has auth_proxy_cb => (
70 is => 'ro',
71 isa => CodeRef,
72 0 ▶ default => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
73 0 ▶ return sub { 1 }
Subroutine __ANON__: not called CC=1 SCAR=6.9
74 0 },
75 3 );
76  
77 #--------------------------------------------------------------------------#
78 # provided by Authen::SCRAM::Role::Common
79 #--------------------------------------------------------------------------#
80  
81 3 with 'Authen::SCRAM::Role::Common';
82  
83 #pod =attr digest
84 #pod
85 #pod Name of a digest function available via L<PBKDF2::Tiny>. Valid values are
86 #pod SHA-1, SHA-224, SHA-256, SHA-384, or SHA-512. Defaults to SHA-1.
87 #pod
88 #pod =attr nonce_size
89 #pod
90 #pod Size of the client-generated nonce, in bits. Defaults to 192.
91 #pod The server-nonce will be appended, so the final nonce size will
92 #pod be substantially larger.
93 #pod
94 #pod =attr skip_saslprep
95 #pod
96 #pod A boolean that defaults to false. If set to true, usernames and passwords will
97 #pod not be normalized through SASLprep. This is a deviation from the RFC5802 spec
98 #pod and is not recommended.
99 #pod
100 #pod =cut
101  
102 #--------------------------------------------------------------------------#
103 # private attributes
104 #--------------------------------------------------------------------------#
105  
106 3 has _proof_ok => (
107 is => 'ro',
108 isa => Bool,
109 writer => '_set_proof_ok',
110 );
111  
112 #--------------------------------------------------------------------------#
113 # public methods
114 #--------------------------------------------------------------------------#
115  
116 #pod =method first_msg
117 #pod
118 #pod $server_first_msg = $server->first_msg( $client_first_msg );
119 #pod
120 #pod This takes the C<client-first-message> received from the client and returns the
121 #pod C<server-first-message> string to be sent to the client to continue a SCRAM
122 #pod session. Calling this again will reset the internal state and initiate a new
123 #pod session. This will throw an exception should an error occur.
124 #pod
125 #pod =cut
126  
127 17 ▶sub first_msg {
Subroutine first_msg: called CC=6 SCAR=18.1
Pod documented
128 17 my ( $self, $msg ) = @_;
129 17 $self->_clear_session;
130  
131 17 my ( $cbind, $authz, $c_1_bare, $mext, @params ) = $msg =~ $self->_client_first_re;
132  
133 17 ▶ if ( !defined $cbind ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $cbind) 9 8 17
134 9 croak "SCRAM client-first-message could not be parsed";
135 }
136 8 ▶ if ( $cbind eq 'p' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cbind eq "p") 0 8 8
137 0 croak
138 "SCRAM client-first-message required channel binding, but we do not support it";
139 }
140 8 ▶ if ( defined $mext ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $mext) 0 8 8
141 0 croak
142 "SCRAM client-first-message required mandatory extension '$mext', but we do not support it";
143 }
144  
145 8 ▶ push @params, $authz if defined $authz;
Branch: 1 branch
BranchTrueFalseTotal
if defined $authz 3 5 8
146 8 $self->_parse_to_session(@params);
147 8 $self->_extend_nonce;
148  
149 8 my $name = $self->_get_session('n');
150 8 my ( $salt, $stored_key, $server_key, $iters ) = $self->credential_cb->($name);
151  
152 8 ▶ if ( !defined $salt ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $salt) 1 7 8
153 1 croak "SCRAM client-first-message had unknown user '$name'";
154 }
155  
156 $self->_set_session(
157 7 s => $self->_base64($salt),
158 i => $iters,
159 _c1b => $c_1_bare,
160 _stored_key => $stored_key,
161 _server_key => $server_key
162 );
163  
164 7 my $reply = $self->_join_reply(qw/r s i/);
165 7 $self->_set_session( _s1 => $reply );
166  
167 7 return $reply;
168 }
169  
170 #pod =method final_msg
171 #pod
172 #pod $server_final_msg = $server->final_msg( $client_final_msg );
173 #pod
174 #pod This takes the C<client-final-message> received from the client and returns the
175 #pod C<server-final-message> string containing the verification signature to be sent
176 #pod to the client.
177 #pod
178 #pod If an authorization identity was provided by the client, it will confirm that
179 #pod the authenticating username is authorized to act as the authorization id using
180 #pod the L</auth_proxy_cb> attribute.
181 #pod
182 #pod If the client credentials do not match or the authentication name is not
183 #pod authorized to act as the authorization name, then an exception will be thrown.
184 #pod
185 #pod =cut
186  
187 7 ▶sub final_msg {
Subroutine final_msg: called CC=7 SCAR=19.7
Pod documented
188 7 my ( $self, $msg ) = @_;
189  
190 7 my ( $c2wop, @params ) = $msg =~ $self->_client_final_re;
191 7 $self->_set_session( _c2wop => $c2wop );
192  
193 7 ▶ if ( !defined $c2wop ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $c2wop) 0 7 7
194 0 croak "SCRAM client-first-message could not be parsed";
195 }
196  
197 # confirm nonce
198 7 my $original_nonce = $self->_get_session("r");
199 7 $self->_parse_to_session(@params);
200 7 my $joint_nonce = $self->_get_session("r");
201 7 ▶ unless ( $joint_nonce eq $original_nonce ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($joint_nonce eq $original_nonce) 0 7 7
202 0 croak "SCRAM client-final-message nonce invalid";
203 }
204  
205 # confirm channel bindings
206 7 my $cbind =
207 $self->_base64( encode_utf8( $self->_construct_gs2( $self->_get_session("a") ) ) );
208 7 ▶ if ( $cbind ne $self->_get_session("c") ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cbind ne $self->_get_session("c")) 0 7 7
209 0 croak "SCRAM client-final-message channel binding didn't match";
210 }
211  
212 # confirm proof
213  
214 7 my $client_sig = $self->_client_sig;
215 7 my $proof = decode_base64( $self->_get_session("p") );
216 7 my $client_key = $proof ^ $client_sig;
217 7 my $computed_key = $self->_digest_fcn->($client_key);
218 7 my $name = $self->_get_session("n");
219  
220 7 ▶ if ( !$self->_const_eq_fcn->( $computed_key, $self->_get_session("_stored_key") ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->_const_eq_fcn->($computed_key, $self->_get_session("_stored_key"))) 1 6 7
221 1 croak "SCRAM authentication for user '$name' failed";
222 }
223  
224 6 ▶ if ( my $authz = $self->_get_session("a") ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $authz = $self->_get_session("a")) 3 3 6
225 3 ▶ $self->auth_proxy_cb->( $name, $authz )
Branch: 1 branch
BranchTrueFalseTotal
$self->auth_proxy_cb->($name, $authz) or croak("SCRAM authentication failed; '${name}' not authorized to act as '${authz}'") 2 1 3
226 or croak("SCRAM authentication failed; '$name' not authorized to act as '$authz'");
227 }
228  
229 5 $self->_set_session( _proof_ok => 1 );
230  
231 5 my $server_sig =
232 $self->_hmac_fcn->( $self->_get_session('_server_key'), $self->_auth_msg );
233  
234 5 $self->_set_session( v => $self->_base64($server_sig) );
235  
236 5 $self->_join_reply('v');
237 }
238  
239 #pod =method authorization_id
240 #pod
241 #pod $username = $client->authorization_id();
242 #pod
243 #pod This takes no arguments and returns the authorization identity resulting from
244 #pod the SCRAM exchange. This is the client-supplied authorization identity (if one
245 #pod was provided and validated) or else the successfully authenticated identity.
246 #pod
247 #pod =cut
248  
249 8 ▶sub authorization_id {
Subroutine authorization_id: called CC=4 SCAR=13.9
Pod documented
250 8 my ($self) = @_;
251 8 ▶ return '' unless $self->_get_session("_proof_ok");
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_get_session("_proof_ok") 3 5 8
252 5 my $authz = $self->_get_session("a");
253 5 ▶ return ( defined($authz) && length($authz) ) ? $authz : $self->_get_session("n");
Branch: 1 branch
BranchTrueFalseTotal
defined $authz && length $authz ? : 2 3 5
Condition66%2/3
defined $authz and length $authz
!ll&&!rl&&r
302
MC/DC50%1/2
defined $authz and length $authz
defined $authz length $authz
Truth table66%2/3
A and B
A= defined $authzB= length $authz
ABresult
0X0
100
111
254 }
255  
256 3 1;
257  
258  
259 # vim: ts=4 sts=4 sw=4 et:
260  
261 __END__
↑ Summary SCRAM/Role/Common.pm »