File Coverage

blib/lib/App/MatrixTool/Command/client/login.pm
Criterion Covered Total %
statement 18 36 50.0
branch 0 2 0.0
condition n/a
subroutine 6 8 75.0
pod 0 1 0.0
total 24 47 51.0


line stmt bran cond sub pod time code
1             # You may distribute under the terms of either the GNU General Public License
2             # or the Artistic License (the same terms as Perl itself)
3             #
4             # (C) Paul Evans, 2016 -- leonerd@leonerd.org.uk
5              
6             package App::MatrixTool::Command::client::login;
7              
8 1     1   752 use strict;
  1         1  
  1         27  
9 1     1   3 use warnings;
  1         1  
  1         23  
10 1     1   3 use base qw( App::MatrixTool::Command::client );
  1         1  
  1         87  
11              
12             our $VERSION = '0.08';
13              
14 1     1   4 use constant DESCRIPTION => "Obtain a client authentication token";
  1         1  
  1         103  
15 1     1   5 use constant ARGUMENTS => ( "user", "password?" );
  1         1  
  1         55  
16 1     1   4 use constant OPTIONS => ();
  1         1  
  1         242  
17              
18             =head1 NAME
19              
20             matrixtool client login - Obtain a client authentication token
21              
22             =head1 SYNOPSIS
23              
24             $ matrixtool client login @me:example.com
25              
26             =head1 DESCRIPTION
27              
28             This command performs the C steps of the Matrix client API, obtaining
29             an access token for the given user ID. This requires a password - either
30             provided on the commandline as a second argument, or requested interactively
31             on the terminal.
32              
33             Once the access token is obtained it is stored in the token cache, which lives
34             in F<$HOME/.matrix/client-tokens>. This cache will be consulted by other
35             C sub-commands to automatically provide the access token if
36             required.
37              
38             Note that the interactive terminal method requires the optional dependency of
39             L to be installed, so that local echo can temporarily be disabled
40             while the user types in the password.
41              
42             =cut
43              
44             sub run
45             {
46 0     0 0   my $self = shift;
47 0           my ( $opts, $user, $password ) = @_;
48              
49 0           $self->{user_id} = $user;
50              
51 0 0         if( !defined $password ) {
52 0           require IO::Termios;
53 0           my $stdin = IO::Termios->new( \*STDIN );
54              
55 0           STDOUT->autoflush(1);
56 0           print "Password: ";
57              
58 0           $stdin->setflag_echo( 0 );
59 0           $password = <$stdin>; chomp $password; print "\n";
  0            
  0            
60 0           $stdin->setflag_echo( 1 );
61             }
62              
63             $self->do_json( POST => "/_matrix/client/r0/login",
64             content => {
65             type => "m.login.password",
66             user => $user,
67             password => $password,
68             }
69             )->then( sub {
70 0     0     my ( $body ) = @_;
71              
72             $self->client_token_store->put(
73             server => $self->{server},
74             id => $user,
75             data => $body->{access_token},
76 0           );
77              
78 0           $self->output_ok( "Obtained access token" );
79              
80 0           Future->done();
81 0           });
82             }
83              
84             =head1 AUTHOR
85              
86             Paul Evans
87              
88             =cut
89              
90             0x55AA;