在 Ember-simple-auth 中附加 firebase JWT

Append firebase JWT in Ember-simple-auth

我正在尝试在我的 Ember 应用程序 (2.10) 中进行授权。我的工作流程是

  1. 用户点击 Facebook 登录按钮 然后
  2. 我正在使用 torii 获取访问令牌/我的用户数据库在 firebase 上/
  3. 然后我通过 facebook 提供商将令牌发送到 firebase.auth。它returns JWT token.

问题 是我获得了 JWT 令牌,现在我必须登录我的 ember 应用程序。我正在尝试在此处自定义 torii 身份验证器。我如何在 ember 应用程序中实现它。下面是我的验证器:

authenticate() {
return this._super(...arguments).then((torii) => {
    const serverTokenEndpoint = this.get('serverTokenEndpoint');
    return this.get('ajax').request(serverTokenEndpoint, {
        type: 'POST',
        data: {
        'type': torii.provider,
        'client_id': this.client,
        'token': torii.authorizationCode
        }
    }).then((token) => {
        var provider = new firebase.auth.FacebookAuthProvider();
        firebase.auth().signInWithPopup(provider).then(function(result) {
        // This gives Facebook Access Token.
        // JWT-token=result.user.Cd
        // JWT-token.iat at=result.user.ea.Sa
        // JWT-token-refresh = result.user.refreshToken
        console.log(result)
        //   token = result.user.Cd;
        // const expiresAt = this._absolutizeExpirationTime(result.user.ea.Sa);
        token = Ember.assign(token, { 'expires_at': result.user.ea.Sa });
        // this._scheduleAccessTokenRefresh(result.user.ea.Sa, expiresAt, result.user.refreshToken, torii);
        return Ember.assign(token, {'torii': torii});
        });
    });
    });  
}

查看 ESA 存储库中的 this guide。它涵盖了 torii 和 Github auth,但一般概念对于您的用例是相同的。

@marcoow 我确实尝试过这个并且它进行了身份验证但是当令牌过期时我无法刷新token.Seems这不是正确的方法,我如何使用 firebase

刷新令牌
export default ToriiAuthenticator.extend({
    torii: Ember.inject.service(),
    ajax: Ember.inject.service(),
    refreshAccessTokens: true,
    rejectWithResponse: false,

    restore(data) {
        return new RSVP.Promise((resolve, reject) => {
            const now = (new Date()).getTime();
            const refreshAccessTokens = this.get('refreshAccessTokens');
            if (!isEmpty(data['expires_at']) && data['expires_at'] < now) {
                // if (refreshAccessTokens) {
                this._refreshAccessToken(data['expires_in'], data['refresh_token']).then(() => {
                    resolve();
                }).catch(function(error) {
                    reject();
                });

                // } else {
                // reject();
                // }
            } else {
                if (!this._validate(data)) {
                    reject();
                } else {
                    this._scheduleAccessTokenRefresh(data['expires_in'], data['expires_at'], data['refresh_token']);
                    resolve(data);
                }
            }
        });
    },
    authenticate() {
        return new Ember.RSVP.Promise((resolve, reject) => {
            var provider = new firebase.auth.FacebookAuthProvider();
            firebase.auth().signInWithPopup(provider).then((result) => {
                var expires_in = this._absolutizeExpirationTime(result.user.ea.Sa);
                var expiresAt = result.user.ea.Sa;
                result = Ember.assign(result, { 'expires_at': expiresAt, 'expires_in': expires_in, 'access_token': result.user.Cd, 'refresh_token': result.refresh_token });
                resolve(result)
            });
            // const useResponse = this.get('rejectWithResponse');
            // const provider = new firebase.auth.FacebookAuthProvider();
            // firebase.auth().signInWithPopup(provider).then((result) => {
            //     let expires_in = result.user.ea.Sa;
            //     const expiresAt = this._absolutizeExpirationTime(expires_in);
            //     this._scheduleAccessTokenRefresh(expires_in, expiresAt, result.refresh_token);
            //     if (!isEmpty(expiresAt)) {
            //         result = Ember.assign(result, { 'expires_at': expiresAt, 'expires_in': expires_in, 'access_token': result.user.Cd, 'refresh_token': result.refresh_token });
            //     }
            //     // resolve(result);
            // }, (response) => {
            //     Ember.run(null, reject, useResponse ? response : response.responseJSON);
            // }).catch(function(error) {
            //     console.log(error);
            // });
        });
    },
    invalidate(data) {
        const serverTokenRevocationEndpoint = this.get('serverTokenRevocationEndpoint');
        return new RSVP.Promise((resolve) => {
            if (isEmpty(serverTokenRevocationEndpoint)) {
                resolve();
            } else {
                if (!Ember.isEmpty(data.access_token)) {
                    delete data.access_token;
                    firebase.auth().signOut();
                    resolve();
                }
            }
        });
    },
    _scheduleAccessTokenRefresh(expiresIn, expiresAt, refreshToken) {
        console.log('sched')
        const refreshAccessTokens = this.get('_refreshAccessTokens');
        if (refreshAccessTokens) {
            const now = (new Date()).getTime();
            if (isEmpty(expiresAt) && !isEmpty(expiresIn)) {
                expiresAt = new Date(now + expiresIn * 1000).getTime();
            }
            const offset = this.get('tokenRefreshOffset');
            if (!isEmpty(refreshToken) && !isEmpty(expiresAt) && expiresAt > now - offset) {
                run.cancel(this._refreshTokenTimeout);
                delete this._refreshTokenTimeout;
                if (!testing) {
                    this._refreshTokenTimeout = run.later(this, this._refreshAccessToken, expiresIn, refreshToken, expiresAt - now - offset);
                }
            }
        }
    },

    _refreshAccessToken(expiresIn, refreshToken) {
        console.log('refresh');
        const data = { 'grant_type': 'refresh_token', 'refresh_token': refreshToken };
        firebase.auth().currentUser.getToken(/ forceRefresh / true).then((response) => {
            return new RSVP.Promise((resolve, reject) => {
                // firebase.auth().currentUser.getToken(true).then((response) => {
                expiresIn = response.user.ea.Sa || expiresIn;
                refreshToken = response.refresh_token || refreshToken;
                const expiresAt = this._absolutizeExpirationTime(expiresIn);
                const data = assign(response, { 'expires_in': expiresIn, 'expires_at': expiresAt, 'refresh_token': refreshToken });
                this._scheduleAccessTokenRefresh(expiresIn, null, refreshToken);
                this.trigger('sessionDataUpdated', data);
                resolve(data);
            }, (response) => {
                warn(`Access token could not be refreshed - server responded with ${response.responseJSON}.`);
                reject();
            });
        });
    },

    _absolutizeExpirationTime(expiresIn) {
        if (!isEmpty(expiresIn)) {
            return new Date((new Date().getTime()) + expiresIn * 1000).getTime();
        }
    },

    _validate(data) {
        return !isEmpty(data['access_token']);
    }
});