예제 #1
0
    def POST(self, jrequest, jresponse):
        assert isinstance(jrequest, JsonRpcRequest)
        assert isinstance(jresponse, JsonRpcResponse)
        jresponse.setId()
        try:
            email = jrequest.getValue("email")[0]
            raw_password = jrequest.getValue("password")[0]
        except Exception:
            raise InvalidParams(
                "email and password are required for method=login.")
        try:
            email_user = EmailUser.getByEmail(email)
        except Exception:
            raise EntityNotFound("EmailUser entity is not found",
                                 {"email": email})
        assert isinstance(email_user, EmailUser)
        email_user.matchPassword(
            raw_password)  # raises PasswordMismatch if password not matches
        #        email_user.saveToSession()

        assert email_user.odenkiId is not None
        odenki_user = OdenkiUser.getByOdenkiId(email_user.odenkiId)
        odenki_user.saveToSession()
        EmailUser.deleteFromSession()

        jresponse.setResultValue("EmailUser", email_user)
        jresponse.setResultValue("OdenkiUser", odenki_user)
예제 #2
0
 def POST(self, jrequest, jresponse):
     jresponse.setId()
     odenki_id = int(jrequest.getValue("odenkiId")[0])
     try:
         odenki_user = OdenkiUser.getByOdenkiId(odenki_id)
     except EntityNotFound, e:
         odenki_user = OdenkiUser.createNew()
예제 #3
0
 def POST(self, jrequest, jresponse):
     jresponse.setId()
     odenki_id = int(jrequest.getValue("odenkiId")[0])
     try:
         odenki_user = OdenkiUser.getByOdenkiId(odenki_id)
     except EntityNotFound, e:
         odenki_user = OdenkiUser.createNew()
예제 #4
0
    def GET(self, jrequest, jresponse):
        assert isinstance(jrequest, JsonRpcRequest)
        assert isinstance(jresponse, JsonRpcResponse)
        jresponse.setId()
        jresponse.setRedirectTarget("/html/error.html")
        #jresponse.setRedirectTarget("/html/auth/index.html")
        current_user = users.get_current_user()
        if current_user is None:
            #            jresponse.setRedirectTarget("/html/auth/index.html")
            return
        #debug("type of current_user.user_id() is %s " % type(current_user.user_id()))

        # prepare GmailUser
        try:
            gmail_user = GmailUser.getByGmailId(current_user.user_id())
            gmail_user.nickname = current_user.nickname()
            gmail_user.gmail = current_user.email()
            gmail_user.put_async()
        except EntityNotFound:
            gmail_user = GmailUser()
            gmail_user.gmailId = current_user.user_id()
            gmail_user.gmail = current_user.email()
            gmail_user.nickname = current_user.nickname()
            gmail_user.put()
        assert isinstance(gmail_user, GmailUser)

        # prepare OdenkiUser
        try:
            odenki_user = OdenkiUser.loadFromSession()
        except EntityNotFound:
            odenki_user = None

        if odenki_user is None:
            if gmail_user.odenkiId is None:
                odenki_user = OdenkiUser.createNew()
                odenki_user.saveToSession()
                gmail_user.setOdenkiId(odenki_user.odenkiId)
                gmail_user.put_async()
            else:
                odenki_user = OdenkiUser.getByOdenkiId(gmail_user.odenkiId)
                odenki_user.saveToSession()
        else:
            if gmail_user.odenkiId is None:
                gmail_user.setOdenkiId(odenki_user.odenkiId)
                gmail_user.put_async()
                odenki_user.saveToSession()
            else:
                if gmail_user.odenkiId != odenki_user.odenkiId:
                    raise InconsistentAuthentiation({
                        gmail_user.__class__.__name__:
                        gmail_user,
                        odenki_user.__class__.__name__:
                        odenki_user
                    })
                odenki_user.saveToSession()

        jresponse.setResultValue("OdenkiUser", odenki_user)
        jresponse.setResultValue("GmailUser", gmail_user)
        jresponse.setRedirectTarget("/html/settings.html")
예제 #5
0
    def GET(self, jrequest, jresponse):
        """load EmailUser instance by nonce.
        Nonce will be invalidated and email will be updated."""
        assert isinstance(jrequest, JsonRpcRequest)
        assert isinstance(jresponse, JsonRpcResponse)
        jresponse.setId()
        #EmailUser.deleteFromSession()
        nonce = unicode(jrequest.getPathInfo(4))
        email_user = EmailUser.getByNonce(nonce)
        assert isinstance(email_user, EmailUser)
        assert email_user.nonceEmail is not None
        email_user.email = email_user.nonceEmail
        email_user.nonceEmail = None
        email_user.hashedPassword = email_user.nonceHashedPassword
        email_user.nonceHashedPassword = None
        email_user.nonce = None
        email_user.put_async()
        #email_user.saveToSession()

        # prepare OdenkiUser
        try:
            odenki_user = OdenkiUser.loadFromSession()
        except EntityNotFound:
            odenki_user = None

        # reconcile EmailUser and OdenkiUser
        if odenki_user is None:
            if email_user.odenkiId is None:
                odenki_user = OdenkiUser.createNew()
                assert isinstance(odenki_user, OdenkiUser)
                odenki_user.saveToSession()
                email_user.setOdenkiId(odenki_user.odenkiId)
                email_user.put_async()
            else:
                odenki_user = OdenkiUser.getByOdenkiId(email_user.odenkiId)
                odenki_user.saveToSession()
        else:
            if email_user.odenkiId is None:
                odenki_user.saveToSession()
                email_user.setOdenkiId(odenki_user.odenkiId)
                email_user.put_async()
            else:
                if email_user.odenkiId != odenki_user.odenkiId:
                    raise InconsistentAuthentiation({
                        email_user.__class__.__name__:
                        email_user,
                        odenki_user.__class__.__name__:
                        odenki_user
                    })
                odenki_user.saveToSession()

        jresponse.setResultValue(odenki_user.__class__.__name__, odenki_user)
        jresponse.setResultValue(email_user.__class__.__name__, email_user)
        #jresponse.setResultValue("nonce", nonce)
        jresponse.setRedirectTarget("http://%s/html/settings.html" %
                                    jrequest.request.host)
예제 #6
0
    def GET(self, jrequest, jresponse):
        assert isinstance(jrequest, JsonRpcRequest)
        assert isinstance(jresponse, JsonRpcResponse)
        jresponse.setId()
        jresponse.setRedirectTarget("/html/error.html")
        #jresponse.setRedirectTarget("/html/auth/index.html")
        current_user = users.get_current_user()
        if current_user is None:
#            jresponse.setRedirectTarget("/html/auth/index.html")
            return
        #debug("type of current_user.user_id() is %s " % type(current_user.user_id()))

        # prepare GmailUser
        try:
            gmail_user = GmailUser.getByGmailId(current_user.user_id())
            gmail_user.nickname = current_user.nickname()
            gmail_user.gmail = current_user.email()
            gmail_user.put_async()
        except EntityNotFound:
            gmail_user = GmailUser()
            gmail_user.gmailId = current_user.user_id()
            gmail_user.gmail = current_user.email()
            gmail_user.nickname = current_user.nickname()
            gmail_user.put()
        assert isinstance(gmail_user, GmailUser)
        
        # prepare OdenkiUser
        try:
            odenki_user = OdenkiUser.loadFromSession()
        except EntityNotFound:
            odenki_user = None
        
        if odenki_user is None:
            if gmail_user.odenkiId is None:
                odenki_user = OdenkiUser.createNew()
                odenki_user.saveToSession()
                gmail_user.setOdenkiId(odenki_user.odenkiId)
                gmail_user.put_async()
            else:
                odenki_user = OdenkiUser.getByOdenkiId(gmail_user.odenkiId)
                odenki_user.saveToSession()
        else:
            if gmail_user.odenkiId is None:
                gmail_user.setOdenkiId(odenki_user.odenkiId)
                gmail_user.put_async()
                odenki_user.saveToSession()
            else:
                if gmail_user.odenkiId != odenki_user.odenkiId:
                    raise InconsistentAuthentiation({gmail_user.__class__.__name__: gmail_user,
                                                     odenki_user.__class__.__name__:odenki_user})
                odenki_user.saveToSession()
        
        jresponse.setResultValue("OdenkiUser", odenki_user)
        jresponse.setResultValue("GmailUser", gmail_user)
        jresponse.setRedirectTarget("/html/settings.html")
예제 #7
0
def fillOdenkiUserByTwitterUser():
    try:
        odenki_user = OdenkiUser.loadFromSession()
        assert isinstance(odenki_user, OdenkiUser)
        return
    except EntityNotFound:
        twitter_user = TwitterUser.loadFromSession()
        assert isinstance(twitter_user, TwitterUser)
        odenki_user = OdenkiUser.getByOdenkiId(twitter_user.odenkiId)
        assert isinstance(odenki_user, OdenkiUser)
        odenki_user.saveToSession()
예제 #8
0
def fillOdenkiUserByEmailUser():
    try:
        odenki_user = OdenkiUser.loadFromSession()
        assert isinstance(odenki_user, OdenkiUser)
        return
    except EntityNotFound:
        email_user = EmailUser.loadFromSession()
        assert isinstance(email_user, EmailUser)
        odenki_user = OdenkiUser.getByOdenkiId(email_user.odenkiId)
        assert isinstance(odenki_user, OdenkiUser)
        odenki_user.saveToSession()
예제 #9
0
def fillOdenkiUserByEmailUser():
    try:
        odenki_user = OdenkiUser.loadFromSession()
        assert isinstance(odenki_user, OdenkiUser)
        return
    except EntityNotFound:
        email_user = EmailUser.loadFromSession()
        assert isinstance(email_user, EmailUser)
        odenki_user = OdenkiUser.getByOdenkiId(email_user.odenkiId)
        assert isinstance(odenki_user, OdenkiUser)
        odenki_user.saveToSession()
예제 #10
0
def fillOdenkiUserByTwitterUser():
    try:
        odenki_user = OdenkiUser.loadFromSession()
        assert isinstance(odenki_user, OdenkiUser)
        return
    except EntityNotFound:
        twitter_user = TwitterUser.loadFromSession()
        assert isinstance(twitter_user, TwitterUser)
        odenki_user = OdenkiUser.getByOdenkiId(twitter_user.odenkiId)
        assert isinstance(odenki_user, OdenkiUser)
        odenki_user.saveToSession()
예제 #11
0
 def login(self, jrequest, jresponse):
     assert isinstance(jrequest, JsonRpcRequest)
     assert isinstance(jresponse, JsonRpcResponse)
     jresponse.setId()
     import gaesessions
     session = gaesessions.get_current_session()
     assert isinstance(session, gaesessions.Session)
     odenki_id = jrequest.request.get("odenkiId")
     odenki_id = int(odenki_id)
     odenki_user = OdenkiUser.getByOdenkiId(odenki_id)
     assert isinstance(odenki_user, OdenkiUser)
     odenki_user.saveToSession()
     jresponse.addResult(odenki_user)
예제 #12
0
 def GET(self, jrequest, jresponse):
     """load EmailUser instance by nonce.
     Nonce will be invalidated and email will be updated."""
     assert isinstance(jrequest, JsonRpcRequest)
     assert isinstance(jresponse, JsonRpcResponse)
     jresponse.setId()
     #EmailUser.deleteFromSession()
     nonce = unicode(jrequest.getPathInfo(4))
     email_user = EmailUser.getByNonce(nonce)
     assert isinstance(email_user, EmailUser)
     assert email_user.nonceEmail is not None
     email_user.email = email_user.nonceEmail
     email_user.nonceEmail = None
     email_user.hashedPassword = email_user.nonceHashedPassword
     email_user.nonceHashedPassword = None
     email_user.nonce = None
     email_user.put_async()
     #email_user.saveToSession()
     
     # prepare OdenkiUser
     try:
         odenki_user = OdenkiUser.loadFromSession()
     except EntityNotFound:
         odenki_user = None
         
     # reconcile EmailUser and OdenkiUser
     if odenki_user is None:
         if email_user.odenkiId is None:
             odenki_user = OdenkiUser.createNew()
             assert isinstance(odenki_user, OdenkiUser)
             odenki_user.saveToSession()
             email_user.setOdenkiId(odenki_user.odenkiId)
             email_user.put_async()
         else:
             odenki_user = OdenkiUser.getByOdenkiId(email_user.odenkiId)
             odenki_user.saveToSession()
     else:
         if email_user.odenkiId is None:
             odenki_user.saveToSession()
             email_user.setOdenkiId(odenki_user.odenkiId)
             email_user.put_async()
         else:
             if email_user.odenkiId != odenki_user.odenkiId:
                 raise InconsistentAuthentiation({email_user.__class__.__name__: email_user,
                                                  odenki_user.__class__.__name__:odenki_user})
             odenki_user.saveToSession()
     
     jresponse.setResultValue(odenki_user.__class__.__name__, odenki_user)
     jresponse.setResultValue(email_user.__class__.__name__, email_user)
     #jresponse.setResultValue("nonce", nonce)
     jresponse.setRedirectTarget("http://%s/html/settings.html" % jrequest.request.host)
예제 #13
0
    def login(self, jrequest, jresponse):
        assert isinstance(jrequest, JsonRpcRequest)
        assert isinstance(jresponse, JsonRpcResponse)
        jresponse.setId()
        import gaesessions

        session = gaesessions.get_current_session()
        assert isinstance(session, gaesessions.Session)
        odenki_id = jrequest.request.get("odenkiId")
        odenki_id = int(odenki_id)
        odenki_user = OdenkiUser.getByOdenkiId(odenki_id)
        assert isinstance(odenki_user, OdenkiUser)
        odenki_user.saveToSession()
        jresponse.addResult(odenki_user)
예제 #14
0
    def GET(self, jrequest, jresponse):
        assert isinstance(jrequest, JsonRpcRequest)
        assert isinstance(jresponse, JsonRpcResponse)
        jresponse.setId()
        jresponse.setRedirectTarget("/api/Twitter")
        debug(jrequest.request)
        implicit_flow = ImplicitFlow()
        implicit_flow.setAccessToken(jrequest.request)
        implicit_flow.saveToSession()
        try:
            twitter_user = TwitterUser.loadFromSession(self)
            if twitter_user.twitterId != implicit_flow.twitterId:
                raise MixedAuthentication({"twitter_user.twitterId": twitter_user.twitterId, "implicit_flow.twitterId": implicit_flow.twitterId})
        except EntityNotFound:
            try:
                twitter_user = TwitterUser.getByTwitterId(implicit_flow.twitterId)
            except EntityNotFound:
                twitter_user = TwitterUser()
                twitter_user.twitterId = implicit_flow.twitterId

        assert isinstance(twitter_user, TwitterUser)
        assert twitter_user.twitterId == implicit_flow.twitterId
        twitter_user.screenName = implicit_flow.screenName
        twitter_user.accessToken = implicit_flow.accessToken
        
        if twitter_user.odenkiId is None:
            try:
                odenki_user = OdenkiUser.loadFromSession()
                assert isinstance(odenki_user, OdenkiUser)
                twitter_user.odenkiId = odenki_user.odenkiId
            except EntityNotFound:
                odenki_user = OdenkiUser.getNew()
                assert isinstance(odenki_user, OdenkiUser)
                twitter_user.odenkiId = odenki_user.odenkiId
        else:
            try:
                odenki_user = OdenkiUser.loadFromSession()
                if twitter_user.odenkiId != odenki_user.odenkiId:
                    raise MixedAuthentication({"twitter_user.odenkiId": twitter_user.odenkiId, "odenki_user.odenkiId": odenki_user.odenkiId}) 
            except EntityNotFound:
                odenki_user = OdenkiUser.getByOdenkiId(twitter_user.odenkiId)
                odenki_user.saveToSession()
                
        assert twitter_user.odenkiId == odenki_user.odenkiId
        twitter_user.put()
        twitter_user.saveToSession()
예제 #15
0
    def POST(self, jrequest, jresponse):
        assert isinstance(jrequest, JsonRpcRequest)
        assert isinstance(jresponse, JsonRpcResponse)
        jresponse.setId()
        try:
            email = jrequest.getValue("email")[0]
            raw_password = jrequest.getValue("password")[0]
        except Exception:
            raise InvalidParams("email and password are required for method=login.")
        try:    
            email_user = EmailUser.getByEmail(email)
        except Exception:
            raise EntityNotFound("EmailUser entity is not found", {"email": email})
        assert isinstance(email_user, EmailUser)
        email_user.matchPassword(raw_password) # raises PasswordMismatch if password not matches
#        email_user.saveToSession()
        
        assert email_user.odenkiId is not None
        odenki_user = OdenkiUser.getByOdenkiId(email_user.odenkiId)
        odenki_user.saveToSession()
        EmailUser.deleteFromSession()
        
        jresponse.setResultValue("EmailUser", email_user)
        jresponse.setResultValue("OdenkiUser", odenki_user)
예제 #16
0
    def GET(self, jrequest, jresponse):
        assert isinstance(jrequest, JsonRpcRequest)
        assert isinstance(jresponse, JsonRpcResponse)
        jresponse.setId()
        jresponse.setRedirectTarget("/html/auth/error.html")
        try:
            oauth_token = jrequest.getValue("oauth_token")[0]
            oauth_verifier = jrequest.getValue("oauth_verifier")[0]
        except:
            raise OAuthError("OAuthCallback was called with neither oauth_token nor oauth_verifier.")
        
        session = gaesessions.get_current_session()
        try:
            request_token = session[REQUEST_TOKEN_SESSION_KEY]
            request_token_secret = session[REQUEST_TOKEN_SECRET_SESSION_KEY]
        except KeyError:
            raise OAuthError("Request token have not been obtained.")
        
        if oauth_token != request_token:
            raise OAuthError("OAuthCallback gets token which is not identical to retaining request token.")

        token = oauth2.Token(request_token, request_token_secret)
        token.set_verifier(oauth_verifier)
        consumer = oauth2.Consumer(TWITTER_CONSUMER_KEY, TWITTER_CONSUMER_SECRET)
        client = oauth2.Client(consumer, token)
        resp, content = client.request(ACCESS_TOKEN_URL, "POST")
        access_token_dict = dict(parse_qsl(content))
        try:
            access_token = access_token_dict["oauth_token"]
            access_token_secret = access_token_dict["oauth_token_secret"]
            user_id = access_token_dict["user_id"]
            screen_name = access_token_dict["screen_name"]
        except KeyError:
            raise OAuthError({"request_token": request_token,
                              "ACCESS_TOKEN_URL": ACCESS_TOKEN_URL
                              },
                             message="OAuthCallback failed to exchange verified request token to access token.")
    
        # prepare TwittrUser
        try: 
            twitter_user = TwitterUser.getByTwitterId(int(user_id))
            twitter_user.setAccessToken(access_token, access_token_secret)
            twitter_user.screenName = unicode(screen_name)
            twitter_user.verifyCredentials11()
            twitter_user.put_async()
        except EntityNotFound:
            twitter_user = TwitterUser.create(int(user_id))
            twitter_user.setAccessToken(access_token, access_token_secret)
            twitter_user.screenName = unicode(screen_name)
            twitter_user.verifyCredentials11()
            twitter_user.put()
        assert isinstance(twitter_user, TwitterUser)
        
        # prepare OdenkiUser
        try:
            odenki_user = OdenkiUser.loadFromSession()
        except EntityNotFound: 
            odenki_user = None
        
        # reconcile TwitterUser and OdenkiUser
        if odenki_user is None:
            if twitter_user.odenkiId is None:
                odenki_user = OdenkiUser.createNew()
                assert isinstance(odenki_user, OdenkiUser)
                odenki_user.saveToSession()
                twitter_user.setOdenkiId(odenki_user.odenkiId)
                twitter_user.put_async()
            else:
                odenki_user = OdenkiUser.getByOdenkiId(twitter_user.odenkiId)
                odenki_user.saveToSession()
        else:
            if twitter_user.odenkiId is None:
                odenki_user.saveToSession()
                twitter_user.setOdenkiId(odenki_user.odenkiId)
                twitter_user.put_async()
            else:
                if twitter_user.odenkiId != odenki_user.odenkiId:
                    raise InconsistentAuthentiation({twitter_user.__class__.__name__: twitter_user,
                                                     odenki_user.__class__.__name__:odenki_user})
                odenki_user.saveToSession()
        
        jresponse.setResultValue("OdenkiUser", odenki_user)
        jresponse.setResultValue("TwitterUser", twitter_user)
        jresponse.setRedirectTarget("/html/settings.html")
예제 #17
0
    def GET(self, jrequest, jresponse):
        assert isinstance(jrequest, JsonRpcRequest)
        assert isinstance(jresponse, JsonRpcResponse)
        jresponse.setId()
        jresponse.setRedirectTarget("/html/auth/error.html")
        try:
            oauth_token = jrequest.getValue("oauth_token")[0]
            oauth_verifier = jrequest.getValue("oauth_verifier")[0]
        except:
            raise OAuthError(
                "OAuthCallback was called with neither oauth_token nor oauth_verifier."
            )

        session = gaesessions.get_current_session()
        try:
            request_token = session[REQUEST_TOKEN_SESSION_KEY]
            request_token_secret = session[REQUEST_TOKEN_SECRET_SESSION_KEY]
        except KeyError:
            raise OAuthError("Request token have not been obtained.")

        if oauth_token != request_token:
            raise OAuthError(
                "OAuthCallback gets token which is not identical to retaining request token."
            )

        token = oauth2.Token(request_token, request_token_secret)
        token.set_verifier(oauth_verifier)
        consumer = oauth2.Consumer(TWITTER_CONSUMER_KEY,
                                   TWITTER_CONSUMER_SECRET)
        client = oauth2.Client(consumer, token)
        resp, content = client.request(ACCESS_TOKEN_URL, "POST")
        access_token_dict = dict(parse_qsl(content))
        try:
            access_token = access_token_dict["oauth_token"]
            access_token_secret = access_token_dict["oauth_token_secret"]
            user_id = access_token_dict["user_id"]
            screen_name = access_token_dict["screen_name"]
        except KeyError:
            raise OAuthError(
                {
                    "request_token": request_token,
                    "ACCESS_TOKEN_URL": ACCESS_TOKEN_URL
                },
                message=
                "OAuthCallback failed to exchange verified request token to access token."
            )

        # prepare TwittrUser
        try:
            twitter_user = TwitterUser.getByTwitterId(int(user_id))
            twitter_user.setAccessToken(access_token, access_token_secret)
            twitter_user.screenName = unicode(screen_name)
            twitter_user.verifyCredentials11()
            twitter_user.put_async()
        except EntityNotFound:
            twitter_user = TwitterUser.create(int(user_id))
            twitter_user.setAccessToken(access_token, access_token_secret)
            twitter_user.screenName = unicode(screen_name)
            twitter_user.verifyCredentials11()
            twitter_user.put()
        assert isinstance(twitter_user, TwitterUser)

        # prepare OdenkiUser
        try:
            odenki_user = OdenkiUser.loadFromSession()
        except EntityNotFound:
            odenki_user = None

        # reconcile TwitterUser and OdenkiUser
        if odenki_user is None:
            if twitter_user.odenkiId is None:
                odenki_user = OdenkiUser.createNew()
                assert isinstance(odenki_user, OdenkiUser)
                odenki_user.saveToSession()
                twitter_user.setOdenkiId(odenki_user.odenkiId)
                twitter_user.put_async()
            else:
                odenki_user = OdenkiUser.getByOdenkiId(twitter_user.odenkiId)
                odenki_user.saveToSession()
        else:
            if twitter_user.odenkiId is None:
                odenki_user.saveToSession()
                twitter_user.setOdenkiId(odenki_user.odenkiId)
                twitter_user.put_async()
            else:
                if twitter_user.odenkiId != odenki_user.odenkiId:
                    raise InconsistentAuthentiation({
                        twitter_user.__class__.__name__:
                        twitter_user,
                        odenki_user.__class__.__name__:
                        odenki_user
                    })
                odenki_user.saveToSession()

        jresponse.setResultValue("OdenkiUser", odenki_user)
        jresponse.setResultValue("TwitterUser", twitter_user)
        jresponse.setRedirectTarget("/html/settings.html")