Esempio n. 1
0
    def get(self, mode=""):
        google_user = users.get_current_user()

        if google_user is None:
            self.redirect(users.create_login_url("/login"))

        # last_access_timeを記録
        user = User.gql("WHERE user_id=:1 and service=:2", google_user.user_id(), "google").get()
        if user is None:
            user = User(
                user_id=google_user.user_id(),
                name=google_user.nickname(),
                mail=google_user.email(),
                service="google",
                access_token=None,
                google_user=google_user,
                post_key="control",
                last_access_time=datetime.datetime.now(),
            )
            user.put()

        session = Session(self.request, self.response)
        session.new(user)
        if mode == "mlogin":
            self.redirect("/mhome?xoauth_requestor_id=" + user.user_id)
            return
        self.redirect("/")
Esempio n. 2
0
    def get(self, mode=""):
        google_user = users.get_current_user()

        if google_user is None:
            self.redirect(users.create_login_url('/login'))

        # last_access_timeを記録
        user = User.gql("WHERE user_id=:1 and service=:2",
                        google_user.user_id(), "google").get()
        if user is None:
            user = User(user_id=google_user.user_id(),
                        name=google_user.nickname(),
                        mail=google_user.email(),
                        service='google',
                        access_token=None,
                        google_user=google_user,
                        post_key='control',
                        last_access_time=datetime.datetime.now())
            user.put()

        session = Session(self.request, self.response)
        session.new(user)
        if mode == 'mlogin':
            self.redirect('/mhome?xoauth_requestor_id=' + user.user_id)
            return
        self.redirect('/')
Esempio n. 3
0
def register():
    rq = request.json
    if not rq or not 'username' in rq or not 'password' in rq or not "display_name" in rq or not "email" in rq:
        abort(400)

    if db.user.find_one({"username": rq["username"]}) is not None:
        abort(409)

    try:
        user = User()
        user.username = rq['username']
        user.password = hashlib.md5(rq['password'].encode('utf-8')).hexdigest()
        user.display_name = rq['display_name']
        user.email = rq['email']
        db.user.insert_one(user.__dict__)
    except:
        abort(400)

    return "ok"
Esempio n. 4
0
    def get(self, action="", account="", param=""):
        verification_code = self.request.get("code")
        args = dict(client_id=settings.FACEBOOK_APP_ID,
                    redirect_uri=self.request.path_url)
        if verification_code:
            args["client_secret"] = settings.FACEBOOK_APP_SECRET
            args["code"] = verification_code
            response = cgi.parse_qs(
                urllib.urlopen(
                    "https://graph.facebook.com/oauth/access_token?" +
                    urllib.urlencode(args)).read())
            access_token = response["access_token"][-1]

            profile_res = unicode(
                urllib.urlopen("https://graph.facebook.com/me?" +
                               urllib.urlencode(dict(
                                   access_token=access_token))).read(),
                'utf-8')
            profile = simplejson.loads(profile_res)

            user = None
            is_not_login = False
            try:
                self.check_login()
                user = self.session.get_user()
            except utils.NotLoginError:
                is_not_login = True
                user = User.gql("WHERE user_id=:1 and service=:2",
                                str(profile["id"]), "facebook").get()
                if user is None:
                    user = User(user_id=str(profile["id"]),
                                name=str(profile["name"]),
                                mail=str(profile["email"]),
                                service='facebook',
                                access_token=access_token,
                                post_key='control',
                                last_access_time=datetime.datetime.now())
                    user.put()
                session = Session(self.request, self.response)
                session.new(user)

            account = Account(user_ref=user,
                              service="facebook",
                              account_name=str(profile["id"]),
                              display_name=str(profile["name"]),
                              account_info=profile_res,
                              scope=FACEBOOK_SCOPE,
                              access_token=access_token)
            account.put()

            #既に同じアカウントが登録されていたら削除します
            saved_accounts = Account.gql(
                "WHERE service = :1 and user_ref = :2 and account_name = :3",
                "facebook", user.key(), account.account_name)
            for saved_account in saved_accounts:
                if saved_account.key() != account.key():
                    saved_account.delete()

            if is_not_login:
                if action == 'mlogin':
                    self.redirect('/mhome?xoauth_requestor_id=' + user.user_id)
                    return
                self.redirect('/')
                return

            #ウィンドウを閉じます
            tmpl = os.path.join(os.path.dirname(__file__),
                                "../view/oauth_callback.html")
            return self.response.out.write(
                template.render(tmpl, {'account': account}))
        else:
            args["scope"] = FACEBOOK_SCOPE
            if action == 'oauth':
                args["display"] = 'popup'
            self.redirect("https://www.facebook.com/dialog/oauth?" +
                          #"https://graph.facebook.com/oauth/authorize?" +
                          urllib.urlencode(args))
            return
Esempio n. 5
0
        
        # Fetch the token from Cassandra and build our Consumer object.
        if request is None or 'oauth_consumer_key' not in request:
            logging.warn("Request is missing oauth_consumer_key.")
            raise NotLoginError()
        
        try:
            # Verify the two-legged request.
            server = oauth2.Server()
            server.add_signature_method(oauth2.SignatureMethod_HMAC_SHA1())
            server.verify_request(request, _get_consumer(request["oauth_consumer_key"]), None)
        except Exception, e:
            logging.warn("Could not verify signature (%s)." % e)
            raise NotLoginError()
        
        user = User.gql("WHERE user_id=:1", user_id).get()
        if not user:
            logging.warn("Specified user is not found. (%s)" % user_id)
            raise NotLoginError()
        session = TempSession(self.request, self.response)
        session.new(user)
        self.session = session
        
        return True

def need_login(fn):
    def check_login(_self, *args, **kw):
        _self.check_login()
        return fn(_self, *args, **kw)
    return check_login
Esempio n. 6
0
        if request is None or 'oauth_consumer_key' not in request:
            logging.warn("Request is missing oauth_consumer_key.")
            raise NotLoginError()

        try:
            # Verify the two-legged request.
            server = oauth2.Server()
            server.add_signature_method(oauth2.SignatureMethod_HMAC_SHA1())
            server.verify_request(request,
                                  _get_consumer(request["oauth_consumer_key"]),
                                  None)
        except Exception, e:
            logging.warn("Could not verify signature (%s)." % e)
            raise NotLoginError()

        user = User.gql("WHERE user_id=:1", user_id).get()
        if not user:
            logging.warn("Specified user is not found. (%s)" % user_id)
            raise NotLoginError()
        session = TempSession(self.request, self.response)
        session.new(user)
        self.session = session

        return True


def need_login(fn):
    def check_login(_self, *args, **kw):
        _self.check_login()
        return fn(_self, *args, **kw)
Esempio n. 7
0
 def get(self, action="", account="", param=""):
     verification_code = self.request.get("code")
     args = dict(client_id=settings.FACEBOOK_APP_ID, redirect_uri=self.request.path_url)
     if verification_code:
         args["client_secret"] = settings.FACEBOOK_APP_SECRET
         args["code"] = verification_code
         response = cgi.parse_qs(urllib.urlopen(
             "https://graph.facebook.com/oauth/access_token?" +
             urllib.urlencode(args)).read())
         access_token = response["access_token"][-1]
         
         profile_res = unicode(urllib.urlopen(
             "https://graph.facebook.com/me?" +
             urllib.urlencode(dict(access_token=access_token))).read(),'utf-8')
         profile = simplejson.loads(profile_res)
         
         user = None
         is_not_login = False
         try:
             self.check_login()
             user = self.session.get_user()
         except utils.NotLoginError:
             is_not_login = True
             user = User.gql("WHERE user_id=:1 and service=:2", str(profile["id"]), "facebook").get()
             if user is None:
                 user = User(
                     user_id=str(profile["id"]),
                     name=str(profile["name"]),
                     mail=str(profile["email"]),
                     service='facebook',
                     access_token=access_token,
                     post_key='control',
                     last_access_time=datetime.datetime.now()
                 )
                 user.put()
             session = Session(self.request, self.response)
             session.new(user)
         
         account = Account(
             user_ref=user,
             service="facebook",
             account_name=str(profile["id"]),
             display_name=str(profile["name"]),
             account_info=profile_res,
             scope=FACEBOOK_SCOPE,
             access_token=access_token
             )
         account.put()
         
         #既に同じアカウントが登録されていたら削除します
         saved_accounts = Account.gql(
             "WHERE service = :1 and user_ref = :2 and account_name = :3",
             "facebook",
             user.key(),
             account.account_name)
         for saved_account in saved_accounts:
             if saved_account.key() != account.key():
                 saved_account.delete()
         
         if is_not_login:
             if action == 'mlogin':
                 self.redirect('/mhome?xoauth_requestor_id='+user.user_id)
                 return
             self.redirect('/')
             return
         
         #ウィンドウを閉じます
         tmpl = os.path.join(os.path.dirname(__file__), "../view/oauth_callback.html")
         return self.response.out.write(template.render(tmpl, {'account':account}))
     else:
         args["scope"] = FACEBOOK_SCOPE
         if action == 'oauth':
             args["display"] = 'popup'
         self.redirect(
             "https://www.facebook.com/dialog/oauth?" +
             #"https://graph.facebook.com/oauth/authorize?" +
             urllib.urlencode(args))
         return