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