Skip to content

Commit

Permalink
Merge 3544229 into 91ef9fa
Browse files Browse the repository at this point in the history
  • Loading branch information
tchellomello committed Jun 11, 2018
2 parents 91ef9fa + 3544229 commit b6e76be
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 7 deletions.
21 changes: 18 additions & 3 deletions ring_doorbell/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@
from ring_doorbell.const import (
API_VERSION, API_URI, CACHE_ATTRS, CACHE_FILE,
DEVICES_ENDPOINT, HEADERS, NEW_SESSION_ENDPOINT, MSG_GENERIC_FAIL,
POST_DATA, PERSIST_TOKEN_ENDPOINT, PERSIST_TOKEN_DATA, RETRY_TOKEN)
POST_DATA, PERSIST_TOKEN_ENDPOINT, PERSIST_TOKEN_DATA, RETRY_TOKEN,
OAUTH_ENDPOINT, OAUTH_DATA)

from ring_doorbell.doorbot import RingDoorBell
from ring_doorbell.chime import RingChime
Expand All @@ -40,7 +41,6 @@ def __init__(self, username, password, debug=False, persist_token=False,
self.username = username
self.password = password
self.session = requests.Session()
self.session.auth = (self.username, self.password)

self.cache = CACHE_ATTRS
self.cache['account'] = self.username
Expand Down Expand Up @@ -85,12 +85,27 @@ def _process_cached_session(self):
# first time executing, so we have to create a cache file
self._authenticate()

def _get_oauth_token(self):
"""Return Oauth Bearer token."""
oauth_data = OAUTH_DATA.copy()
oauth_data['username'] = self.username
oauth_data['password'] = self.password

req = self.session.post(OAUTH_ENDPOINT,
data=oauth_data,
headers=HEADERS)
oauth_token = None
if req.status_code == 200:
oauth_token = req.json().get('access_token')
return oauth_token

def _authenticate(self, attempts=RETRY_TOKEN, session=None):
"""Authenticate user against Ring API."""
url = API_URI + NEW_SESSION_ENDPOINT

loop = 0
while loop <= attempts:
HEADERS['Authorization'] = \
'Bearer {}'.format(self._get_oauth_token())
loop += 1
try:
if session is None:
Expand Down
18 changes: 14 additions & 4 deletions ring_doorbell/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@
"""Constants."""
import os
from uuid import uuid4 as uuid

HEADERS = {'Content-Type': 'application/x-www-form-urlencoded; charset: UTF-8',
'User-Agent': 'Dalvik/1.6.0 (Linux; Android 4.4.4; Build/KTU84Q)',
'Accept-Encoding': 'gzip, deflate'}
HEADERS = {
'Content-Type': 'application/x-www-form-urlencoded; charset: UTF-8',
'User-Agent': 'Dalvik/1.6.0 (Linux; Android 4.4.4; Build/KTU84Q)',
'Accept-Encoding': 'gzip, deflate'
}

# number of attempts to refresh token
RETRY_TOKEN = 3
Expand All @@ -25,6 +26,7 @@
NOT_FOUND = -1

# API endpoints
OAUTH_ENDPOINT = 'https://oauth.ring.com/oauth/token'
API_VERSION = '9'
API_URI = 'https://api.ring.com'
CHIMES_ENDPOINT = '/clients_api/chimes/{0}'
Expand Down Expand Up @@ -74,6 +76,14 @@
MSG_ALLOWED_VALUES = 'Only the following values are allowed: {0}.'

# structure acquired from reverse engineering to create auth token
OAUTH_DATA = {
"client_id": "ring_official_android",
"grant_type": "password",
"scope": "client",
"username": None,
"password": None,
}

POST_DATA = {
'api_version': API_VERSION,
'device[hardware_id]': str(uuid()),
Expand Down

0 comments on commit b6e76be

Please sign in to comment.