| 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::XOAUTH2 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 { 'XOAUTH2' } | ||
| 28 | |||
| 29 | sub client_start { | ||
| 30 | my $self = shift; | ||
| 31 | $self->{stage} = 0; | ||
| 32 | |||
| 33 | # "user=" {User} "^Aauth=Bearer " {Access Token} "^A^A" | ||
| 34 | # https://developers.google.com/gmail/imap/xoauth2-protocol#initial_client_response | ||
| 35 | my $username = $self->_call('user'); | ||
| 36 | my $token = $self->_call('pass'); # OAuth 2.0 access token | ||
| 37 | my $auth_string = "user=$username\001auth=Bearer $token\001\001"; | ||
| 38 | return $auth_string | ||
| 39 | } | ||
| 40 | |||
| 41 | sub client_step { | ||
| 42 | my ($self, $challenge) = @_; | ||
| 43 | my $json = JSON::PP->new; | ||
| 44 | my $payload = $json->decode( $challenge ); | ||
| 45 | $self->set_error( $payload ); | ||
| 46 | # Send dummy request on authentication failure according to rfc7628. | ||
| 47 | # https://datatracker.ietf.org/doc/html/rfc7628#section-3.2.3 | ||
| 48 | return "\001"; | ||
| 49 | } | ||
| 50 | |||
| 51 | 1; | ||
| 52 | |||
| 53 | __END__ |