Example #1
0
    def build(service_name):
        service = False
        host = get_app_url()
        if service_name == 'facebook':
            service = oauth.remote_app('tvonline.in.ua',
                                       base_url='https://graph.facebook.com/',
                                       request_token_url=None, access_token_url='/oauth/access_token',
                                       authorize_url='https://www.facebook.com/dialog/oauth',
                                       consumer_key='1047252288640078',
                                       consumer_secret='8a1bdcf07a5fc98975775f36f947397a',
                                       request_token_params={'scope': 'email'})
        elif service_name == 'vkontakte':
            service = 'https://oauth.vk.com/authorize?client_id={client_id}&display=page&scope={scope}&redirect_uri=' \
                      '{redirect_url}&v=5.0&response_type=code'.format(
                       client_id='5145835', scope='email',
                       redirect_url='{host}/auth/vkontakte_login/authorized'.format(host=host))

        elif service_name == 'google':
            service = oauth.remote_app('google',
                                       base_url='https://www.google.com/accounts/',
                                       authorize_url='https://accounts.google.com/o/oauth2/auth',
                                       request_token_url=None,
                                       request_token_params={'scope': 'https://www.googleapis.com/auth/plus.login',
                                                             'response_type': 'code'},
                                       access_token_url='https://accounts.google.com/o/oauth2/token',
                                       access_token_method='GET',
                                       access_token_params={'grant_type': 'authorization_code'},
                                       consumer_key='382845359434-uv6dnoqibqb9pf0s1nvvfhdgrgv1gshl.'
                                                    'apps.googleusercontent.com',
                                       consumer_secret='1r7us2GYy-QS42BOnxnxSfTl')

        return service
Example #2
0
 def get_token(self, code):
     url = ''
     host = get_app_url()
     if self.service == 'vkontakte':
         url = 'https://oauth.vk.com/access_token?client_id={client_id}&' \
               'client_secret={client_secret}&redirect_uri={redirect_url}&code={code}'.format(
                    client_id='5145835', client_secret='910H8XhkfJs0qayNRKRM',
                    redirect_url='{host}/auth/vkontakte_login/authorized'.format(host=host), code=code)
     return json.load(urllib.urlopen(url))
Example #3
0
def google_login():
    try:
        flow = client.flow_from_clientsecrets(os.getcwd() + '/app-root/runtime/repo/client_secret.json',
                                              scope=['https://www.googleapis.com/auth/userinfo.email',
                                                     'https://www.googleapis.com/auth/userinfo.profile'],
                                              redirect_uri='{host}/auth/google_login'.format(host=get_app_url()))
    except IOError as e:
        flow = client.flow_from_clientsecrets('client_secret.json',
                                              scope=['https://www.googleapis.com/auth/userinfo.email',
                                                     'https://www.googleapis.com/auth/userinfo.profile'],
                                              redirect_uri='{host}/auth/google_login'.format(host=get_app_url()))
    flow.params['access_type'] = 'online'
    if 'code' not in request.args:
        auth_uri = flow.step1_get_authorize_url()
        return redirect(auth_uri)
    else:
        auth_code = request.args.get('code')
        credentials = flow.step2_exchange(auth_code)
        user_data = json.load(
            urllib.urlopen('https://www.googleapis.com/oauth2/v1/userinfo?alt=json&access_token={token}'.format(
                token=credentials.access_token)))
        session['credentials'] = credentials.to_json()
    if not user_data.get('email'):
        return redirect(url_for('error.error', error='Помилка при логуванні. '
                                                     'Можливо ви не підтвердили вашу електронну адресу в vkontakte '
                                                     'або заборонили доступ до вашої електронної адреси в '
                                                     'налаштуваннях. Спробуйте залогуватися через іншу систему'))
    user = db_query(User, email=user_data.get('email')).first()
    if user is None:
        new_user = User(email=user_data.get('email'), registered_via='google',
                        username=user_data.get('email'), shown_name=user_data.get('name'),
                        confirmed_at=datetime.datetime.utcnow(), active=True, google_id=user_data['id'])
        g.db.add(new_user)
        login_user(new_user)
        g.db.commit()
    else:
        if not user.google_id:
            user.google_id = user_data['id']
        login_user(user)
    g.db.commit()
    session['login_via'] = 'google'
    return redirect(url_for('index.index'))