SASL/Perl/OAUTHBEARER.pm untested

filter: Statementstmt 0.0% 0 / 37 Branchbran 0.0% 0 / 6 Conditioncond 0.0% 0 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 0.0% 0 / 9 Podpod 0.0% 0 / 5 totaltotal 0.0% 0 / 62 CC 4 SCAR 20.8
CC
4
Cov
0%
client_start
20.0
_order
2.0
_secflags
2.0
CRAP 8.0
1 # Copyright (c) 2025 Aditya Garg <gargaditya08@live.com>
2 # Copyright (c) 2025 Julian Swagemakers <julian@swagemakers.org>
3 # All rights reserved.
4 # This program is free software; you can redistribute it and/or
5 # modify it under the same terms as Perl itself.
6  
7 package Authen::SASL::Perl::OAUTHBEARER 2.2100;
8  
9 use strict;
10 use warnings;
11 use vars qw(@ISA);
12 use JSON::PP;
13  
14 @ISA = qw(Authen::SASL::Perl);
15  
16 my %secflags = (
17 noanonymous => 1,
18 );
19  
20 sub _order { 1 }
21  
22 sub _secflags {
23 shift;
24 scalar grep { $secflags{$_} } @_;
25 }
26  
27 sub mechanism { 'OAUTHBEARER' }
28  
29 sub client_start {
30 my $self = shift;
31 $self->{stage} = 0;
32  
33 # This will generate the oauthbearer string used for authentication.
34 #
35 # "n,a=" {User} ",^Ahost=" {Host} "^Aport=" {Port} "^Aauth=Bearer " {Access Token} "^A^A
36 #
37 # The first part `n,a=" {User} ",` is the gs2 header described in RFC5801.
38 # * gs2-cb-flag `n` -> client does not support CB
39 # * gs2-authzid `a=" {User} "`
40 #
41 # The second part are key value pairs containing host, port and auth as
42 # described in RFC7628. Host and port are optional and can be omitted.
43 #
44 # https://datatracker.ietf.org/doc/html/rfc5801
45 # https://datatracker.ietf.org/doc/html/rfc7628
46 my $username = $self->_call('user');
47 my $host = $self->_call('host');
48 my $port = $self->_call('port');
49 my $token = $self->_call('pass'); # OAuth 2.0 access token
50 my $auth_string;
51 if (defined $host && defined $port) {
52 $auth_string = "n,a=$username,\001host=$host\001port=$port\001auth=Bearer $token\001\001";
53 } elsif (defined $host) {
54 $auth_string = "n,a=$username,\001host=$host\001auth=Bearer $token\001\001";
55 } elsif (defined $port) {
56 $auth_string = "n,a=$username,\001port=$port\001auth=Bearer $token\001\001";
57 } else {
58 $auth_string = "n,a=$username,\001auth=Bearer $token\001\001";
59 }
60  
61 return $auth_string;
62 }
63  
64 sub client_step {
65 my ($self, $challenge) = @_;
66 my $json = JSON::PP->new;
67 my $payload = $json->decode( $challenge );
68 $self->set_error( $payload );
69 # Send dummy request on authentication failure according to rfc7628.
70 # https://datatracker.ietf.org/doc/html/rfc7628#section-3.2.3
71 return "\001";
72 }
73  
74 1;
75  
76 __END__