Firefox上的Ember Simple Auth:身份验证会引发错误

时间:2015-01-30 00:20:47

标签: firefox authentication ember.js ember-simple-auth

我正在扩展Ember Simple Auth的基本身份验证类,以允许通过Google进行身份验证。到目前为止,它适用于Safari 8和Chrome 41(均在Yosemite上),没有任何错误。但是,在Firefox 35上,它会抛出在其他浏览器上不会出现的错误。这是我的Google身份验证器类:

App.GoogleAuthenticator = SimpleAuth.Authenticators.Base.extend({
    // constants for Google API
    GAPI_CLIENT_ID: 'the client id',
    GAPI_SCOPE: ['email'],
    GAPI_TOKEN_VERIFICATION_ENDPOINT: 'https://www.googleapis.com/oauth2/v2/tokeninfo',

    // method for scheduleing a single token refresh
    // time in milliseconds
    scheduleSingleTokenRefresh: function(time) {
        var self = this;
        return new Ember.RSVP.Promise(function(resolve, reject) {
            Ember.run.later(self, function() {
                gapi.auth.authorize({
                    client_id: self.GAPI_CLIENT_ID,
                    scope: self.GAPI_SCOPE,
                    immediate: true
                }, function(data) {
                    if (data && !data.error) {
                        resolve(data);
                    } else {
                        reject((data || {}).error);
                    }
                });
            }, time);
        });
    },
    // WIP: recursive method that reschedules another token refresh after the previous scheduled one was fulfilled
    // usage: scheduleTokenRefreshes(time until token should refresh for the first time, time between subsequent refreshes)
    // usage: scheduleTokenRefreshes(time between refreshes)
    scheduleTokenRefreshes: function(time1, time2) {
        var self = this;
        // if there is a time2, schedule a single refresh, wait for it to be fulfilled, then call myself to schedule again
        if (!Ember.isEmpty(time2)) {
            self.scheduleSingleTokenRefresh(time1)
            .then(function() {
                self.scheduleTokenRefreshes(time2);
            });
        // if there isn't a time2, simply schedule a single refresh, then call myself to schedule again
        } else {
            self.scheduleSingleTokenRefresh(time1)
            .then(function() {
                self.scheduleTokenRefreshes(time1);
            });
        }
    },

    // method that restores the session on reload
    restore: function(data) {
        var self = this;
        return new Ember.RSVP.Promise(function(resolve, reject) {
            console.log(data);
            if (Ember.isEmpty(data.access_token)) {
                reject();
                return;
            }
            // schedule a refresh 15 minutes before it expires or immediately if it expires in < 15
            var timeNow = Math.floor(Date.now() / 1000);
            var expiresAt = +data.expires_at;
            var timeDifference = expiresAt - timeNow;
            var schedulingDelay = Math.floor(timeDifference - 15 * 60);
            schedulingDelay = schedulingDelay < 0 ? 0 : schedulingDelay;
            self.scheduleTokenRefreshes(schedulingDelay * 1000, 45 * 60);
            resolve(data);
        });
    },
    // method that authenticates
    authenticate: function() {
        var self = this;
        return new Ember.RSVP.Promise(function(resolve, reject) {
            gapi.auth.authorize({
                client_id: self.GAPI_CLIENT_ID,
                scope: self.GAPI_SCOPE
            }, function(data) {
                if (data && !data.error) {
                    // schedule a refresh in 45 minutes
                    var schedulingDelay = 45 * 60;
                    self.scheduleTokenRefreshes(schedulingDelay * 1000);
                    resolve(data);
                } else {
                    reject((data || {}).error);
                }
            });
        });
    },
    // method that logs the user out and revokes the token
    invalidate: function(data) {
        var self = this;
        return new Ember.RSVP.Promise(function(resolve, reject) {
            // send a GET request to revoke the token
            Ember.$.ajax({
                type: 'GET',
                url: 'https://accounts.google.com/o/oauth2/revoke?token=' + self.get('session.access_token'),
                contentType: 'application/json',
                dataType: 'jsonp'
            })
            .done(function(successData) {
                resolve(successData);
            })
            .fail(function(error) {
                reject(error);
            });
        });
    }
});

当在Google端成功登录后弹出窗口关闭时,Firefox的控制台上会出现此错误:

Error: Assertion Failed: Error: Permission denied to access property 'toJSON' ember.js:13749
"__exports__.default<.persist@http://127.0.0.1/~jonchan/test/bower_components/ember-simple-auth/simple-auth.js:1524:1
__exports__.default<.updateStore@http://127.0.0.1/~jonchan/test/bower_components/ember-simple-auth/simple-auth.js:1195:11
__exports__.default<.setup@http://127.0.0.1/~jonchan/test/bower_components/ember-simple-auth/simple-auth.js:1149:9
__exports__.default<.authenticate/</<@http://127.0.0.1/~jonchan/test/bower_components/ember-simple-auth/simple-auth.js:1066:13
tryCatch@http://127.0.0.1/~jonchan/test/bower_components/ember/ember.js:47982:16
invokeCallback@http://127.0.0.1/~jonchan/test/bower_components/ember/ember.js:47994:17
publish@http://127.0.0.1/~jonchan/test/bower_components/ember/ember.js:47965:11
@http://127.0.0.1/~jonchan/test/bower_components/ember/ember.js:29462:9
Queue.prototype.invoke@http://127.0.0.1/~jonchan/test/bower_components/ember/ember.js:848:11
Queue.prototype.flush@http://127.0.0.1/~jonchan/test/bower_components/ember/ember.js:913:13
DeferredActionQueues.prototype.flush@http://127.0.0.1/~jonchan/test/bower_components/ember/ember.js:718:13
Backburner.prototype.end@http://127.0.0.1/~jonchan/test/bower_components/ember/ember.js:143:11
createAutorun/backburner._autorun<@http://127.0.0.1/~jonchan/test/bower_components/ember/ember.js:546:9
" ember.js:29488

以下是版本信息:

DEBUG: Ember             : 1.9.1
DEBUG: Ember Data        : 1.0.0-beta.14.1
DEBUG: Handlebars        : 2.0.0
DEBUG: jQuery            : 2.1.3
DEBUG: Ember Simple Auth : 0.7.2

最令人困惑的是,这只出现在Firefox上。这是Ember Simple Auth还是Ember的错误?我该如何解决?

2 个答案:

答案 0 :(得分:0)

我不知道只有Firefox发生了错误(我在Chrome 40中遇到了类似的错误),但是在使用Ember 1.9的ember-simple-auth 0.7.2中存在一个错误,禁止发送实际内容验证器中authenticate方法中的错误响应。

如果您在reject()的拒绝功能中返回authenticate,则不会引发其他错误。然而,这不会传播errorstatus或消息,因此我认为这是一个错误。

通过临时设置Ember.onerror=Ember.K,在github上提出了解决此问题的解决方法,因此不会传播其他错误,尽管它会传播原始authenticate拒绝并显示错误状态。

github repo中的问题只提到测试这个问题,但我在普通代码中遇到了这个问题。

请参阅:https://github.com/simplabs/ember-simple-auth/issues/407

答案 1 :(得分:0)

原来错误发生在resolve方法的authenticate部分。以下是修正它的原因:

App.GoogleAuthenticator = SimpleAuth.Authenticators.Base.extend({
    authenticate: function() {
        return new Ember.RSVP.Promise(function(resolve, reject) {
            gapi.auth.authorize({
                client_id: 'the client id',
                scope: ['the scopes'],
            }, function(data) {
                if (data && !data.error) {
                    resolve({
                        access_token: data.access_token // !! passing the entire 'data' object caused the error somehow
                    });
                } else {
                    reject((data || {}).error);
                }
            });
        });
    },
    // ...
});

我仍然不太确定为什么会导致这个错误。也许Google API的回复(完整版)与Ember Simple Auth不兼容。