Example #1
0
def set_lang_cookie_and_return_dict(cls, force=""):
    """
    set language returning a dict and set cookie
    """
    if force != "":
        lang_cookie = force
    elif cls.request.get("hl") == "":
        # ask for cookie
        lang_cookie = utils.read_cookie(cls, "hl")
        arrLang = str(get_languages(cls)[0]).lower()
        if not lang_cookie:
            if arrLang.find("es") >= 0:
                lang_cookie = "es"
            else:
                lang_cookie = "en"
    else:
        # set cookie to param 'hl' value
        lang_cookie = cls.request.get("hl")
        # Two weeks for hl cookie
    utils.write_cookie(cls, "hl", str(lang_cookie), "/", 1209600)
    lang = {
        'en': languages.en,
        'es': languages.es,
        'it': languages.it,
        'cn': languages.cn,
    }[lang_cookie]
    return lang
Example #2
0
def set_locale(cls, force=None):
    """
    retrieve locale from a prioritized list of sources and then set locale and save it
    cls: self object
    force: a locale to force set (ie 'en_US')
    return: locale
    """
    # 1. force locale if provided
    locale = force
    if locale not in AVAILABLE_LOCALES:
        # 2. retrieve locale from url query string
        locale = cls.request.get("hl", None)
        if locale not in AVAILABLE_LOCALES:
            # 3. retrieve locale from cookie
            locale = utils.read_cookie(cls, "hl")
            if locale not in AVAILABLE_LOCALES:
                # 4. retrieve locale from accept language header
                locale = get_locale_from_accept_header(cls)
                if locale not in AVAILABLE_LOCALES:
                    # 5. detect locale from IP address location
                    locale = get_locale_from_territory(get_territory_from_ip(cls))
                    if locale not in AVAILABLE_LOCALES:
                        # 6. use default locale
                        locale = AVAILABLE_LOCALES[0]
    # convert unicode locale to string for headers
    locale = str(locale)
    i18n.get_i18n().set_locale(locale)
    # save locale in cookie with 26 weeks expiration (in seconds)
    utils.write_cookie(cls, "hl", locale, "/", 15724800)
    return locale
Example #3
0
    def get(self):
        # pull the github token out of the social user db
        user_info = models.User.get_by_id(long(self.user_id))
        social_user = models.SocialUser.get_by_user_and_provider(
            user_info.key, 'github')

        # what do we do if we don't have a token or association?  auth 'em!
        if not social_user:
            scope = 'gist'
            # drop a short lived cookie so we know where to come back to when we're done auth'ing
            utils.write_cookie(self, 'oauth_return_url', 'apps', '/', 15)
            github_helper = github.GithubAuth(scope)
            self.redirect(github_helper.get_authorize_url())
            return
        else:
            apps = models.App.get_by_user(user_info.key)

            if not apps:
                # no apps, no problem, make one
                params = {}
                return self.redirect_to('apps-new', **params)
            else:
                # setup channel to do page refresh in case they sync
                channel_token = user_info.key.urlsafe()
                refresh_channel = channel.create_channel(channel_token)
                params = {
                    'apps': apps,
                    'refresh_channel': refresh_channel,
                    'channel_token': channel_token
                }
                return self.render_template('app/app_list.html', **params)
Example #4
0
    def get(self):
        # pull the github token out of the social user db
        user_info = models.User.get_by_id(long(self.user_id))
        social_user = models.SocialUser.get_by_user_and_provider(user_info.key, 'github')

        # what do we do if we don't have a token or association?  auth 'em!
        if not social_user:
            scope = 'gist'
            # drop a short lived cookie so we know where to come back to when we're done auth'ing
            utils.write_cookie(self, 'oauth_return_url', 'apps', '/', 15)
            github_helper = github.GithubAuth(scope)
            self.redirect( github_helper.get_authorize_url() )
            return
        else:
            apps = models.App.get_by_user(user_info.key)

            if not apps:
            	# no apps, no problem, make one
            	params = {}
            	return self.redirect_to('apps-new', **params)
            else:
            	# setup channel to do page refresh in case they sync
	            channel_token = user_info.key.urlsafe()
	            refresh_channel = channel.create_channel(channel_token)
	            params = {'apps': apps, 'refresh_channel': refresh_channel, 'channel_token': channel_token}
	            return self.render_template('app/app_list.html', **params)
Example #5
0
    def get(self):
        # lookup user's auth info
        user_info = User.get_by_id(long(self.user_id))

        # if we came in from a stream create, redirect back to it
        next = utils.read_cookie(self, "next")
        if next > "":
            utils.write_cookie(self, "next", "", '/', expires=7200)
            return self.redirect(next)

        # params build out
        params = {}

        return self.render_template('user/dashboard.html', **params)
Example #6
0
    def get(self):
        # pull the github token out of the social user db
        user_info = models.User.get_by_id(long(self.user_id))
        social_user = models.SocialUser.get_by_user_and_provider(user_info.key, 'github')

        # what do we do if we don't have a token or association?  auth 'em!
        if not social_user:
            scope = 'gist'
            # drop a short lived cookie so we know where to come back to when we're done auth'ing
            utils.write_cookie(self, 'oauth_return_url', 'apps-create', '/', 15)
            github_helper = github.GithubAuth(scope)
            self.redirect( github_helper.get_authorize_url() )
            return
        else:
            params = {}
            return self.render_template('app/app_create.html', **params)
Example #7
0
    def get(self):
        # pull the github token out of the social user db
        user_info = models.User.get_by_id(long(self.user_id))
        social_user = models.SocialUser.get_by_user_and_provider(
            user_info.key, 'github')

        # what do we do if we don't have a token or association?  auth 'em!
        if not social_user:
            scope = 'gist'
            # drop a short lived cookie so we know where to come back to when we're done auth'ing
            utils.write_cookie(self, 'oauth_return_url', 'apps-create', '/',
                               15)
            github_helper = github.GithubAuth(scope)
            self.redirect(github_helper.get_authorize_url())
            return
        else:
            params = {}
            return self.render_template('app/app_create.html', **params)
Example #8
0
    def get(self, provider_name):
        if not config.enable_federated_login:
            message = _('Federated login is disabled.')
            self.add_message(message, 'warning')
            return self.redirect_to('login')

        # callback handler for twitter oauth
        if provider_name == "twitter":
            oauth_token = self.request.get('oauth_token')
            oauth_verifier = self.request.get('oauth_verifier')
            twitter_helper = twitter.TwitterAuth(self)
            user_data = twitter_helper.auth_complete(oauth_token,
                                                     oauth_verifier)
            screen_name = user_data['screen_name']

            if self.user:
                # user is already logged in so we set a new association with twitter
                user_info = models.User.get_by_id(long(self.user_id))
                if models.SocialUser.check_unique(user_info.key, 'twitter',
                                                  str(user_data['id'])):
                    social_user = models.SocialUser(
                        user=user_info.key,
                        provider='twitter',
                        uid=str(user_data['id']),
                        extra_data=user_data,
                        screen_name=screen_name,
                    )
                    social_user.put()

                    message = _('Twitter association added.')
                    self.add_message(message, 'success')
                else:
                    message = _('This Twitter account is already in use.')
                    self.add_message(message, 'error')
                self.redirect_to('edit-profile')
            else:
                # user is not logged in, but is trying to log in via twitter
                social_user = models.SocialUser.get_by_provider_and_uid(
                    'twitter', str(user_data['id']))
                if social_user:
                    # Social user exists. Need authenticate related site account
                    user = social_user.user.get()
                    self.auth.set_session(self.auth.store.user_to_dict(user),
                                          remember=True)
                    logVisit = models.LogVisit(
                        user=user.key,
                        uastring=self.request.user_agent,
                        ip=self.request.remote_addr,
                        timestamp=utils.get_date_time())
                    logVisit.put()
                    self.redirect_to('home')
                else:
                    # Social user does not exists. Need show login and registration forms!
                    twitter_helper.save_association_data(user_data)
                    message = _(
                        'This Twitter account is not associated with a StackGeek account. '
                        'Please sign in or create a StackGeek account before continuing.'
                    )
                    self.add_message(message, 'warning')
                    self.redirect_to('login')

        # callback handler for github oauth
        elif provider_name == "github":
            # get our request code back from the social login handler above
            code = self.request.get('code')

            # create our github auth object (again)
            scope = 'gist'
            github_helper = github.GithubAuth(scope)

            # retrieve the access token using the code and auth object
            try:
                access_token = github_helper.get_access_token(code)
                user_data = github_helper.get_user_info(access_token)
            except:
                message = _(
                    'An error was encountered while exchanging tokens with Github.'
                )
                self.add_message(message, 'error')
                self.redirect_to('edit-profile')
                return

            if self.user:
                # user is already logged in so we set a new association with github
                user_info = models.User.get_by_id(long(self.user_id))
                if models.SocialUser.check_unique(user_info.key, 'github',
                                                  str(user_data['login'])):
                    social_user = models.SocialUser(user=user_info.key,
                                                    provider='github',
                                                    uid=str(
                                                        user_data['login']),
                                                    access_token=access_token,
                                                    extra_data=user_data)
                    social_user.put()

                    message = _(
                        'The StackGeek application has been added to your Github account.'
                    )
                    self.add_message(message, 'success')
                else:
                    message = _(
                        'The currently logged in Github account is already in use with another account.'
                    )
                    self.add_message(message, 'error')
                    self.redirect_to('edit-profile')
                    return

                # check to see if we are headed anywhere else besides the profile page
                next_page = utils.read_cookie(self, 'oauth_return_url')
                utils.write_cookie(self, 'oauth_return_url', '', '/', 15)

                # try out what we found or redirect to profile if it's a bad value
                if next_page:
                    try:
                        self.redirect_to(next_page)
                    except:
                        self.redirect_to('edit-profile')
                else:
                    self.redirect_to('edit-profile')
            else:
                # user is not logged in, but is trying to log in via github
                social_user = models.SocialUser.get_by_provider_and_uid(
                    'github', str(user_data['login']))
                if social_user:
                    # Social user exists. Need authenticate related site account
                    user = social_user.user.get()
                    self.auth.set_session(self.auth.store.user_to_dict(user),
                                          remember=True)
                    logVisit = models.LogVisit(
                        user=user.key,
                        uastring=self.request.user_agent,
                        ip=self.request.remote_addr,
                        timestamp=utils.get_date_time())
                    logVisit.put()
                    self.redirect_to('home')
                else:
                    # Social user does not exists. Need show login and registration forms!
                    message = _(
                        'This Github account is not associated with a StackGeek account. '
                        'Please sign in or create a StackGeek account before continuing.'
                    )
                    self.add_message(message, 'warning')
                    self.redirect_to('login')

        # google, myopenid, yahoo OpenID Providers
        elif provider_name in models.SocialUser.open_id_providers():
            provider_display_name = models.SocialUser.PROVIDERS_INFO[
                provider_name]['label']
            # get info passed from OpenId Provider
            from google.appengine.api import users
            current_user = users.get_current_user()
            if current_user:
                if current_user.federated_identity():
                    uid = current_user.federated_identity()
                else:
                    uid = current_user.user_id()
                email = current_user.email()
            else:
                message = _(
                    'No user authentication information received from %s. '
                    'Please ensure you are logging in from an authorized OpenID Provider (OP).'
                    % provider_display_name)
                self.add_message(message, 'error')
                return self.redirect_to('login')
            if self.user:
                # add social account to user
                user_info = models.User.get_by_id(long(self.user_id))
                if models.SocialUser.check_unique(user_info.key, provider_name,
                                                  uid):
                    social_user = models.SocialUser(user=user_info.key,
                                                    provider=provider_name,
                                                    uid=uid)
                    social_user.put()

                    message = _('%s association successfully added.' %
                                provider_display_name)
                    self.add_message(message, 'success')
                else:
                    message = _('This %s account is already in use.' %
                                provider_display_name)
                    self.add_message(message, 'error')
                self.redirect_to('edit-profile')
            else:
                # login with OpenId Provider
                social_user = models.SocialUser.get_by_provider_and_uid(
                    provider_name, uid)
                if social_user:
                    # Social user found. Authenticate the user
                    user = social_user.user.get()
                    self.auth.set_session(self.auth.store.user_to_dict(user),
                                          remember=True)
                    logVisit = models.LogVisit(
                        user=user.key,
                        uastring=self.request.user_agent,
                        ip=self.request.remote_addr,
                        timestamp=utils.get_date_time())
                    logVisit.put()
                    self.redirect_to('home')
                else:
                    message = _(
                        'This OpenID based account is not associated with a StackGeek account. '
                        'Please sign in or create a StackGeek account before continuing.'
                    )
                    self.add_message(message, 'warning')
                    self.redirect_to('login')
        else:
            message = _('This authentication method is not yet implemented!')
            self.add_message(message, 'warning')
            self.redirect_to('login')
Example #9
0
    def get(self, provider_name):
        if not config.enable_federated_login:
            message = _('Federated login is disabled.')
            self.add_message(message, 'warning')
            return self.redirect_to('login')

        # callback handler for twitter oauth
        if provider_name == "twitter":
            oauth_token = self.request.get('oauth_token')
            oauth_verifier = self.request.get('oauth_verifier')
            twitter_helper = twitter.TwitterAuth(self)
            user_data = twitter_helper.auth_complete(oauth_token, oauth_verifier)
            screen_name = user_data['screen_name']

            if self.user:
                # user is already logged in so we set a new association with twitter
                user_info = models.User.get_by_id(long(self.user_id))
                if models.SocialUser.check_unique(user_info.key, 'twitter', str(user_data['id'])):
                    social_user = models.SocialUser(
                        user = user_info.key,
                        provider = 'twitter',
                        uid = str(user_data['id']),
                        extra_data = user_data,
                        screen_name = screen_name,
                    )
                    social_user.put()

                    message = _('Twitter association added.')
                    self.add_message(message, 'success')
                else:
                    message = _('This Twitter account is already in use.')
                    self.add_message(message, 'error')
                self.redirect_to('edit-profile')
            else:
                # user is not logged in, but is trying to log in via twitter
                social_user = models.SocialUser.get_by_provider_and_uid('twitter', str(user_data['id']))
                if social_user:
                    # Social user exists. Need authenticate related site account
                    user = social_user.user.get()
                    self.auth.set_session(self.auth.store.user_to_dict(user), remember=True)
                    logVisit = models.LogVisit(
                        user = user.key,
                        uastring = self.request.user_agent,
                        ip = self.request.remote_addr,
                        timestamp = utils.get_date_time()
                    )
                    logVisit.put()
                    self.redirect_to('home')
                else:
                    # Social user does not exists. Need show login and registration forms!
                    twitter_helper.save_association_data(user_data)
                    message = _('This Twitter account is not associated with a StackGeek account. '
                                'Please sign in or create a StackGeek account before continuing.')
                    self.add_message(message, 'warning')
                    self.redirect_to('login')

        # callback handler for github oauth
        elif provider_name == "github":
            # get our request code back from the social login handler above
            code = self.request.get('code')

            # create our github auth object (again)
            scope = 'gist'
            github_helper = github.GithubAuth(scope)

            # retrieve the access token using the code and auth object
            try:
                access_token = github_helper.get_access_token(code)
                user_data = github_helper.get_user_info(access_token)
            except:
                message = _('An error was encountered while exchanging tokens with Github.')
                self.add_message(message, 'error')
                self.redirect_to('edit-profile')
                return
            
            if self.user:
                # user is already logged in so we set a new association with github
                user_info = models.User.get_by_id(long(self.user_id))
                if models.SocialUser.check_unique(user_info.key, 'github', str(user_data['login'])):
                    social_user = models.SocialUser(
                        user = user_info.key,
                        provider = 'github',
                        uid = str(user_data['login']),
                        access_token = access_token,
                        extra_data = user_data
                    )
                    social_user.put()

                    message = _('The StackGeek application has been added to your Github account.')
                    self.add_message(message, 'success')
                else:
                    message = _('The currently logged in Github account is already in use with another account.')
                    self.add_message(message, 'error')
                    self.redirect_to('edit-profile')
                    return

                # check to see if we are headed anywhere else besides the profile page
                next_page = utils.read_cookie(self, 'oauth_return_url')
                utils.write_cookie(self, 'oauth_return_url', '', '/', 15)

                # try out what we found or redirect to profile if it's a bad value
                if next_page:
                    try:
                        self.redirect_to(next_page)
                    except:
                        self.redirect_to('edit-profile')
                else:
                    self.redirect_to('edit-profile')
            else:
                # user is not logged in, but is trying to log in via github
                social_user = models.SocialUser.get_by_provider_and_uid('github', str(user_data['login']))
                if social_user:
                    # Social user exists. Need authenticate related site account
                    user = social_user.user.get()
                    self.auth.set_session(self.auth.store.user_to_dict(user), remember=True)
                    logVisit = models.LogVisit(
                        user = user.key,
                        uastring = self.request.user_agent,
                        ip = self.request.remote_addr,
                        timestamp = utils.get_date_time()
                    )
                    logVisit.put()
                    self.redirect_to('home')
                else:
                    # Social user does not exists. Need show login and registration forms!
                    message = _('This Github account is not associated with a StackGeek account. '
                                'Please sign in or create a StackGeek account before continuing.')
                    self.add_message(message, 'warning')
                    self.redirect_to('login')

        # google, myopenid, yahoo OpenID Providers
        elif provider_name in models.SocialUser.open_id_providers():
            provider_display_name = models.SocialUser.PROVIDERS_INFO[provider_name]['label']
            # get info passed from OpenId Provider
            from google.appengine.api import users
            current_user = users.get_current_user()
            if current_user:
                if current_user.federated_identity():
                    uid = current_user.federated_identity()
                else:
                    uid = current_user.user_id()
                email = current_user.email()
            else:
                message = _('No user authentication information received from %s. '
                            'Please ensure you are logging in from an authorized OpenID Provider (OP).'
                            % provider_display_name)
                self.add_message(message, 'error')
                return self.redirect_to('login')
            if self.user:
                # add social account to user
                user_info = models.User.get_by_id(long(self.user_id))
                if models.SocialUser.check_unique(user_info.key, provider_name, uid):
                    social_user = models.SocialUser(
                        user = user_info.key,
                        provider = provider_name,
                        uid = uid
                    )
                    social_user.put()

                    message = _('%s association successfully added.' % provider_display_name)
                    self.add_message(message, 'success')
                else:
                    message = _('This %s account is already in use.' % provider_display_name)
                    self.add_message(message, 'error')
                self.redirect_to('edit-profile')
            else:
                # login with OpenId Provider
                social_user = models.SocialUser.get_by_provider_and_uid(provider_name, uid)
                if social_user:
                    # Social user found. Authenticate the user
                    user = social_user.user.get()
                    self.auth.set_session(self.auth.store.user_to_dict(user), remember=True)
                    logVisit = models.LogVisit(
                        user = user.key,
                        uastring = self.request.user_agent,
                        ip = self.request.remote_addr,
                        timestamp = utils.get_date_time()
                    )
                    logVisit.put()
                    self.redirect_to('home')
                else:
                    message = _('This OpenID based account is not associated with a StackGeek account. '
                                'Please sign in or create a StackGeek account before continuing.')
                    self.add_message(message, 'warning')
                    self.redirect_to('login')
        else:
            message = _('This authentication method is not yet implemented!')
            self.add_message(message, 'warning')
            self.redirect_to('login')