Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Changes to login via sherriff id verification #510

Merged
merged 2 commits into from
Dec 14, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 47 additions & 2 deletions robin_stocks/robinhood/authentication.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,11 @@ def login(username=None, password=None, expiresIn=86400, scope='internal', by_sm
'scope': scope,
'username': username,
'challenge_type': challenge_type,
'device_token': device_token
'device_token': device_token,
'try_passkeys': False,
'token_request_path':'/login',
'create_read_only_secondary_token':True,
'request_id': '848bd19e-02bc-45d9-99b5-01bce5a79ea7'
}

if mfa_code:
Expand Down Expand Up @@ -181,6 +185,10 @@ def login(username=None, password=None, expiresIn=86400, scope='internal', by_sm
update_session(
'X-ROBINHOOD-CHALLENGE-RESPONSE-ID', challenge_id)
data = request_post(url, payload)
elif 'verification_workflow' in data:
workflow_id = data['verification_workflow']['id']
_validate_sherrif_id(device_token=device_token, workflow_id=workflow_id, mfa_code=mfa_code)
data = request_post(url, payload)
# Update Session data with authorization or raise exception with the information present in data.
if 'access_token' in data:
token = '{0} {1}'.format(data['token_type'], data['access_token'])
Expand All @@ -193,12 +201,49 @@ def login(username=None, password=None, expiresIn=86400, scope='internal', by_sm
'access_token': data['access_token'],
'refresh_token': data['refresh_token'],
'device_token': payload['device_token']}, f)

else:
raise Exception(data['detail'])
if 'detail' in data:
raise Exception(data['detail'])
raise Exception(f"Received an error response {data}")
else:
raise Exception('Error: Trouble connecting to robinhood API. Check internet connection.')
return(data)

def _validate_sherrif_id(device_token:str, workflow_id:str,mfa_code:str):
url = "https://api.robinhood.com/pathfinder/user_machine/"
payload = {
'device_id': device_token,
'flow': 'suv',
'input':{'workflow_id': workflow_id}
}
data = request_post(url=url, payload=payload,json=True )

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

NIT extra space at end of request_post params

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed, I had to disable autoformatting on my end as it was introducing lot of changes on PR.

if "id" in data:
inquiries_url = f"https://api.robinhood.com/pathfinder/inquiries/{data['id']}/user_view/"
res = request_get(inquiries_url)
challenge_id=res['type_context']["context"]["sheriff_challenge"]["id"]
challenge_url = f"https://api.robinhood.com/challenge/{challenge_id}/respond/"
challenge_payload = {
'response': mfa_code
}
challenge_response = request_post(url=challenge_url, payload=challenge_payload,json=True )
if challenge_response["status"] == "validated":
inquiries_payload = {"sequence":0,"user_input":{"status":"continue"}}
inquiries_response = request_post(url=inquiries_url, payload=inquiries_payload,json=True )
if inquiries_response["type_context"]["result"] == "workflow_status_approved":
return
else:
raise Exception("workflow status not approved")
else:
raise Exception("Challenge not validated")
raise Exception("Id not returned in user-machine call")

def _get_sherrif_challenge(token_id:str):
Copy link

@tamablevirus tamablevirus Dec 6, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is _get_sheriff_challenge used anywhere?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

its a new function used only in one place

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hes referring to line 241 function, not the validation handler, of which he is correct, the function starting at line 241 is not called anywhere and then the logic calls data value without it being in the args nor subscripted to anything. Just trying to help, the rest of the script seems pretty good


if "id" in data:
return data["id"]
raise Exception("Id not returned in user-machine call")


@login_required
def logout():
Expand Down