簡體   English   中英

如何在graphql中實現自動刷新令牌以進行基於jwt的身份驗證?

[英]How to implement auto refresh token in graphql for jwt based authentication?

我試圖在基於Apollo的graphql server (2.0)中為基於JWT的身份驗證找出這種情況。

基本上在登錄后,用戶從服務器獲取accessToken和refreshToken。

AccessToken在一段時間后過期,服務器發送一條錯誤消息,表明令牌已過期( TokenExpiredError ),然后客戶端需要通過傳遞refreshToken與服務器進行新的accessToken通信。

流量如下 -

  1. 發生TokenExpiredError
  2. 在客戶端獲取該錯誤
  3. 使用舊的accessToken對所有請求進行排隊(這樣服務器不會被太多的refreshToken調用淹沒,並且服務器會生成許多accessTokens)
  4. 在graphql server上調用refreshToken api以獲取新的accessToken
  5. 使用新的accessToken更新所有授權呼叫的accessToken
  6. 注銷用戶incase refreshToken本身已過期
  7. 防止任何種類的競爭條件黑白電話

我已經在客戶端實現了refreshToken變異但是無法弄清楚何時發生錯誤停止所有請求 - >請求新令牌 - >再次進行所有待處理請求以及刷新令牌是否已過期注銷用戶。

我按照這種方法最終解決了我的問題

向別人發布我的方法

// @flow
import { ApolloLink, Observable } from 'apollo-link';
import type { ApolloClient } from 'apollo-client';
import type { Operation, NextLink } from 'apollo-link';

import { refreshToken2, getToken } from './token-service';
import { GraphQLError } from 'graphql';

export class AuthLink extends ApolloLink {
     tokenRefreshingPromise: Promise<boolean> | null;

injectClient = (client: ApolloClient): void => {
    this.client = client;
};

refreshToken = (): Promise<boolean> => {
    //if (!this.tokenRefreshingPromise) this.tokenRefreshingPromise = refreshToken(this.client);
    if (!this.tokenRefreshingPromise) this.tokenRefreshingPromise = refreshToken2();
    return this.tokenRefreshingPromise;
};

setTokenHeader = (operation: Operation): void => {
    const token = getToken();
    if (token) operation.setContext({ headers: { authorization: `Bearer ${token}` } });
};

request(operation: Operation, forward: NextLink) {
    // set token in header
    this.setTokenHeader(operation);
    // try refreshing token once if it has expired
    return new Observable(observer => {
        let subscription, innerSubscription, inner2Subscription;
        try {
            subscription = forward(operation).subscribe({
                next: result => {
                    if (result.errors) {
                        console.log("---->", JSON.stringify(result.errors))
                        for (let err of result.errors) {
                            switch (err.extensions.code) {
                              case 'E140':
                                console.log('E140', result)
                                observer.error(result.errors)
                                break;
                              case 'G130':
                                    this.refreshToken().then(response => {
                                        if (response.data && !response.errors) {
                                            this.setTokenHeader(operation);
                                            innerSubscription = forward(operation).subscribe(observer);
                                        } else {
                                            console.log("After refresh token", JSON.stringify(response));
                                            observer.next(response)
                                        }
                                    }).catch(console.log);
                                break;
                            }
                          }
                    } 
                    observer.next(result)

                  },
                complete: observer.complete.bind(observer),
                error: netowrkError => {
                    observer.error(netowrkError);
                  }
                },
            });
        } catch (e) {
            observer.error(e);
        }
        return () => {
            if (subscription) subscription.unsubscribe();
            if (innerSubscription) innerSubscription.unsubscribe();
            if (inner2Subscription) inner2Subscription.unsubscribe();
        };
    });
}
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM