def get(self, provider=None): oauth2_conf = self.get_provider_conf(provider) redirect_uri = self.uri_for('oauth2.callback', provider=provider, _full=True) service = oauth2.get_service(provider)(redirect_uri=redirect_uri, **oauth2_conf) come_back_to = self.request.get('come_back_to', '') auth_url = service.get_authorization_url(come_back_to=come_back_to) return self.redirect(auth_url)
def get(self, provider=None): code = self.request.get('code', None) if not code: return self.redirect_to('oauth2.begin', provider=provider) #TODO: Add logic to manage declined permissions state = self.request.get('state', None) come_back_to = '' if state and '|' in state: come_back_to = state.split('|')[-1] oauth2_conf = self.get_provider_conf(provider) redirect_uri = self.uri_for('oauth2.callback', provider=provider, _full=True) service = oauth2.get_service(provider)(redirect_uri=redirect_uri, **oauth2_conf) token = service.get_access_token(code) user_info = service.get_user_info(token) add_auth_id, user = self.get_user(provider, user_info) if not user: # creates a new one user_info['type'] = ['u'] ok, user = self.create_user(provider, user_info) if ok: user.confirmed = datetime.datetime.now() user.put_async() user = self.get_user_dict(user) self.auth.set_session(user) if come_back_to: return self.redirect(come_back_to) return self.redirect_to('home') else: raise AuthError('Username already taken.') else: if add_auth_id: auth_ids = user.auth_ids new_auth_id = ':'.join([provider, user_info['email']]) auth_ids.append(new_auth_id) user.auth_ids = auth_ids user.put_async() unique_key = '%s.%s:%s' % (user.__class__.__name__, 'auth_id', new_auth_id) unique = user.unique_model.create(unique_key) if not user.active: self.abort(401, 'Inactive User.') user = self.get_user_dict(user) self.auth.set_session(user) if come_back_to: return self.redirect(come_back_to) return self.redirect_to('home')
def get(self, provider=None): code = self.request.get('code', None) if not code: return self.redirect_to('oauth2.begin', provider=provider) state = self.request.get('state', None) come_back_to = '' if state: come_back_to = state.split('|')[-1] oauth2_conf = self.get_provider_conf(provider) redirect_uri = self.uri_for('oauth2.callback', provider=provider, _full=True) service = oauth2.get_service(provider)(redirect_uri=redirect_uri, **oauth2_conf) token = service.get_access_token(code) user_info = service.get_user_info(token) user_model = self.auth.store.user_model user = user_model.get_by_auth_id(':'.join([provider, user_info['email']])) user = self.auth.store.user_to_dict(user) if not user: # creates a new one user_info['type'] = ['u'] ok, user = user_model.create_user(':'.join([provider, user_info['email']]), **user_info) if ok: user.confirmed = datetime.datetime.now() user.put_async() user = user_model.get_by_auth_id(':'.join( [provider, user_info['email']])) logger.debug(user) user = self.auth.store.user_to_dict(user) self.auth.set_session(user) if come_back_to: return self.redirect(come_back_to) return self.redirect_to('home') else: raise AuthError('Username already taken.') else: self.auth.set_session(user) if come_back_to: return self.redirect(come_back_to) return self.redirect_to('home')