def main(argv=sys.argv): if len(argv) != 2: usage(argv) config_uri = argv[1] setup_logging(config_uri) settings = get_appsettings(config_uri) engine = engine_from_config(settings, 'sqlalchemy.') DBSession.configure(bind=engine) Base.metadata.create_all(engine)
def social(cls, *args, **kwargs): # Grab out passed in values from end_point callback profile = kwargs.get('profile') provider = profile.get('accounts')[0] credentials = kwargs.get('credentials') identifier = sha1(provider.get('userid') + SALT).hexdigest() # Check if we already have a user with that identity? try: user = DBSession.query(cls).filter(cls.identifier==identifier).one() user.update_social_tokens(profile, credentials) return user except NoResultFound: pass # Get the username depending on the provider if provider.get('domain') == 'facebook.com': username = profile.get('preferredUsername', None) elif provider.get('domain') == 'twitter.com': username = profile.get('displayName', None) # Ensure the username is unique tries = 0 while tries < MAX_TRIES: if not username: username = User.random_username(_range=7) if username: try: DBSession.query(cls).filter(cls.username==username).one() username = username + User.random_username(_range=3, prefix='_') except NoResultFound: break tries += 1 else: raise HTTPForbidden # Create the user, update the identifier, and socal tokens user = cls(username=username) user.identifier = identifier user.update_social_tokens(profile, credentials) return user
def authenticate(cls, login, password): from sqlalchemy import or_ try: user = DBSession.query(cls).filter(or_(cls.username==login, cls.email==login)).one() password = User.generate_password(password, str(user.signup_date)) if password == user.password: return user else: return None except NoResultFound: return None
def update_social_tokens(self, profile, credentials): provider = profile.get('accounts')[0] if provider.get('domain') == 'facebook.com': self.facebook_id = profile.get('preferredUsername') email = profile.get('verifiedEmail') if not DBSession.query(User).filter_by(email=email).count(): self.email = profile.get('verifiedEmail') self.facebook_auth_token = credentials.get('oauthAccessToken') elif provider.get('domain') == 'twitter.com': self.twitter_id = profile.get('displayName') self.twitter_auth_token = credentials.get('oauthAccessToken') self.twitter_auth_secret = credentials.get('oauthAccessTokenSecret')