summaryrefslogtreecommitdiff
path: root/lib/Pass/OTP/URI.pm
blob: 25f6002ac9edc8fc5ddf6eaa7450c4e35cb5765a (plain)
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
package Pass::OTP::URI;

=encoding utf8

=head1 NAME

Pass::OTP::URI - Parse otpauth:// URI

=head1 SYNOPSIS

    use Pass::OTP::URI qw(parse);

    my $uri = "otpauth://totp/ACME:john.doe@email.com?secret=HXDMVJECJJWSRB3HWIZR4IFUGFTMXBOZ&issuer=ACME&digits=6";
    my %options = parse($uri);

=cut

use utf8;
use strict;
use warnings;

require Exporter;
our @ISA       = qw(Exporter);
our @EXPORT_OK = qw(parse);

=head1 FUNCTIONS

=over 4

=item parse($uri)

=cut

sub parse {
    my ($uri) = @_;

    my %options = (
        base32 => 1,
    );
    ($options{type}, $options{label}, my $params) = $uri =~ m#^otpauth://([th]otp)/((?:[^:?]+(?::|%3A))?[^:?]+)\?(.*)#;

    foreach my $param (split(/&/, $params)) {
        my ($option, $value) = split(/=/, $param);
        $options{$option} = $value;
    }

    return (%options);
}

=back

=head1 SEE ALSO

L<Pass::OTP>

L<https://github.com/google/google-authenticator/wiki/Key-Uri-Format>

=head1 COPYRIGHT AND LICENSE

Copyright (C) 2020 Jan Baier

This program is free software; you can redistribute it and/or modify it
under the terms of either: the GNU General Public License as published
by the Free Software Foundation; or the Artistic License.

See L<http://dev.perl.org/licenses/> for more information.

=cut

1;