| 1 | package Finance::Robinhood::Error::Challenge; | ||
| 2 | |||
| 3 | =encoding utf-8 | ||
| 4 | |||
| 5 | =for stopwords watchlist watchlists untradable urls | ||
| 6 | |||
| 7 | =head1 NAME | ||
| 8 | |||
| 9 | Finance::Robinhood::Error::Challenge - Device Authentication | ||
| 10 | |||
| 11 | =head1 SYNOPSIS | ||
| 12 | |||
| 13 | $challenge->respond( 284750 ); | ||
| 14 | $challenge || die; # false value is retured when the challenge response has not been validated | ||
| 15 | |||
| 16 | =head1 DESCRIPTION | ||
| 17 | |||
| 18 | As a security measure, Robinhood has implemented user-involved client | ||
| 19 | validation. When met with a challenge, the user must respond with a six digit | ||
| 20 | string of numbers sent to them via SMS or email within a certain length of | ||
| 21 | time. Failing to respond or failing to respond with the correct numbers | ||
| 22 | blacklists the device from accessing the user's account. | ||
| 23 | |||
| 24 | Challenge objects evaluate to untrue values if the response was invalid or if | ||
| 25 | the challenge has not been responded to yet. Obviously, they evaluate to true | ||
| 26 | values if the challenge was met successfully. | ||
| 27 | |||
| 28 | =head1 METHODS | ||
| 29 | |||
| 30 | =cut | ||
| 31 | |||
| 32 | our $VERSION = '0.92_003'; | ||
| 33 | use Mojo::Base-base, -signatures; | ||
| 34 | use Mojo::URL; | ||
| 35 | use Time::Moment; | ||
| 36 | use overload 'bool' => sub ($s, @) { $s->status eq 'validated' ? 1 : 0 }, | ||
| 37 | fallback => 1; | ||
| 38 | # | ||
| 39 | has _rh => undef => weak => 1; | ||
| 40 | |||
| 41 | =head2 C<id( )> | ||
| 42 | |||
| 43 | UUID of the challenge request. | ||
| 44 | |||
| 45 | =head2 C<user( )> | ||
| 46 | |||
| 47 | UUID of the currently or attempting to log in. | ||
| 48 | |||
| 49 | =head2 C<type( )> | ||
| 50 | |||
| 51 | Current primary means of sending validation key. | ||
| 52 | |||
| 53 | =head2 C<alternate_type( )> | ||
| 54 | |||
| 55 | Alternate means of sending validation key. | ||
| 56 | |||
| 57 | =head2 C<remaining_retries( )> | ||
| 58 | |||
| 59 | The number of times the system will attempt to deliver the validation key. | ||
| 60 | |||
| 61 | =head2 C<remaining_attempts( )> | ||
| 62 | |||
| 63 | The number of times the user has to verify before the device ID is blocked. | ||
| 64 | |||
| 65 | =head2 C<status( )> | ||
| 66 | |||
| 67 | return 'OK' if $challenge->status eq 'validated'; | ||
| 68 | |||
| 69 | Returns whether the challenge has been C<issued>, C<validated>, etc. | ||
| 70 | |||
| 71 | =cut | ||
| 72 | |||
| 73 | has ['id', 'user', | ||
| 74 | 'type', 'alternate_type', | ||
| 75 | 'remaining_retries', 'remaining_attempts', | ||
| 76 | 'status' | ||
| 77 | ]; | ||
| 78 | |||
| 79 | =head2 C<expires_at( )> | ||
| 80 | |||
| 81 | $challenge->expires_at; | ||
| 82 | |||
| 83 | Returns a Time::Moment object. This value should be 5 minutes in the future | ||
| 84 | which is how long you have to C<respond( ... )> to the challenge. | ||
| 85 | |||
| 86 | =cut | ||
| 87 | |||
| 88 | sub expires_at($s) { | ||
| 89 | Time::Moment->from_string($s->{expires_at}); | ||
| 90 | } | ||
| 91 | |||
| 92 | =head2 C<email( )> | ||
| 93 | |||
| 94 | $challenge->email; | ||
| 95 | |||
| 96 | Request that the system sends the authorization key via email to the address on | ||
| 97 | file. | ||
| 98 | |||
| 99 | =cut | ||
| 100 | |||
| 101 | sub email ($s) { | ||
| 102 | my $res | ||
| 103 | = $s->_rh->_post( | ||
| 104 | sprintf('https://api.robinhood.com/challenge/%s/replace/', $s->id), | ||
| 105 | challenge_type => 'email'); | ||
| 106 | $_[0] = $res->is_success | ||
| 107 | ? Finance::Robinhood::Error::Challenge->new(_rh => $s->_rh, | ||
| 108 | %{$res->json->{challenge}} | ||
| 109 | ) | ||
| 110 | : Finance::Robinhood::Error->new( | ||
| 111 | $res->is_server_error ? (details => $res->message) : $res->json); | ||
| 112 | } | ||
| 113 | |||
| 114 | =head2 C<sms( )> | ||
| 115 | |||
| 116 | $challenge->sms; | ||
| 117 | |||
| 118 | Request that the system sends the authorization key via SMS to the phone number | ||
| 119 | on file. | ||
| 120 | |||
| 121 | =cut | ||
| 122 | |||
| 123 | sub sms ($s) { | ||
| 124 | my $res | ||
| 125 | = $s->_rh->_post( | ||
| 126 | sprintf('https://api.robinhood.com/challenge/%s/replace/', $s->id), | ||
| 127 | challenge_type => 'sms'); | ||
| 128 | $_[0] = $res->is_success | ||
| 129 | ? Finance::Robinhood::Error::Challenge->new(_rh => $s->_rh, | ||
| 130 | %{$res->json->{challenge}} | ||
| 131 | ) | ||
| 132 | : Finance::Robinhood::Error->new( | ||
| 133 | $res->is_server_error ? (details => $res->message) : $res->json); | ||
| 134 | } | ||
| 135 | |||
| 136 | =head2 C<respond( ... )> | ||
| 137 | |||
| 138 | $challenge->respond( 003298 ); | ||
| 139 | |||
| 140 | Respond to the challenge with the string sent to you via SMS or email. | ||
| 141 | |||
| 142 | =cut | ||
| 143 | |||
| 144 | sub respond ($s, $response) { | ||
| 145 | my $res | ||
| 146 | = $s->_rh->_post( | ||
| 147 | sprintf('https://api.robinhood.com/challenge/%s/respond/', $s->id), | ||
| 148 | response => $response); | ||
| 149 | $_[0] = $res->is_success | ||
| 150 | ? Finance::Robinhood::Error::Challenge->new(_rh => $s->_rh, | ||
| 151 | %{$res->json}) | ||
| 152 | : Finance::Robinhood::Error->new( | ||
| 153 | $res->is_server_error ? (details => $res->message) : $res->json); | ||
| 154 | } | ||
| 155 | |||
| 156 | =head1 LEGAL | ||
| 157 | |||
| 158 | This is a simple wrapper around the API used in the official apps. The author | ||
| 159 | provides no investment, legal, or tax advice and is not responsible for any | ||
| 160 | damages incurred while using this software. This software is not affiliated | ||
| 161 | with Robinhood Financial LLC in any way. | ||
| 162 | |||
| 163 | For Robinhood's terms and disclosures, please see their website at | ||
| 164 | https://robinhood.com/legal/ | ||
| 165 | |||
| 166 | =head1 LICENSE | ||
| 167 | |||
| 168 | Copyright (C) Sanko Robinson. | ||
| 169 | |||
| 170 | This library is free software; you can redistribute it and/or modify it under | ||
| 171 | the terms found in the Artistic License 2. Other copyrights, terms, and | ||
| 172 | conditions may apply to data transmitted through this module. Please refer to | ||
| 173 | the L<LEGAL> section. | ||
| 174 | |||
| 175 | =head1 AUTHOR | ||
| 176 | |||
| 177 | Sanko Robinson E<lt>sanko@cpan.orgE<gt> | ||
| 178 | |||
| 179 | =cut | ||
| 180 | |||
| 181 | 1; |