使用Typescript JsonServiceClient - ServiceStack

时间:2017-11-21 20:34:00

标签: typescript servicestack

在得到this SO question的答案后,我意识到我有一个跨域问题,即httponly标记的ServiceStack bearerToken Cookie没有被发送到不同域上的资源微服务。

ServiceStack的文档解释了如何在文档here中解决此问题。

我无法在ServiceStack文档中实现代码示例的Typescript版本。我猜测文档中的示例代码是C#。

这是我的带有注释的Typescript代码:

import { JsonServiceClient, IReturn } from 'servicestack-client';
import { AuthService } from './auth.service';
//dtos generated by myauthservice/types
import { GetAccessToken, ConvertSessionToToken, ConvertSessionToTokenResponse } from './dtos'
import { AppModule } from '../../app.module';
import { Router } from '@angular/router';

export class JsonServiceClientAuth extends JsonServiceClient {

    private router: Router;
    private authClient: JsonServiceClient;

    constructor(baseUrl: string) {
        console.log('JsonServiceClientAuth.baseUrl', baseUrl);
        super(baseUrl);
        //Router is not injected via the contructor because clients of JsonServiceClientAuth need to create instances of it
        this.router = AppModule.injector.get(Router);
        this.authClient = new JsonServiceClient("http://localhost:5006");

        this.onAuthenticationRequired = async () => {
            console.log('JsonServiceClientAuth.onAuthenticationRequired()');
            console.log('An API is not receiving the bearerToken being redirected to Login');
            this.router.navigate(['/login']);
        };
    }

    get<T>(request: IReturn<T> | string, args?: any): Promise<T> {
        return new Promise<T>((resolve, reject) => {

            //cross domain issue here, ss-tok httponly Cookie will not be sent to the Resource Service
            //ConvertSessionToToken per Service Stacks documentation: http://docs.servicestack.net/jwt-authprovider#converting-an-existing-authenticated-session-into-a-jwt-token

            //but send expects 2-4 argumments, one being the method name
            //the Typecript send will not use the dto ConvertSessionToToken request and get the url
            //var tokenResponse = this.send(new ConvertSessionToToken())

            console.log('this.bearToken', this.bearerToken); //undefined
            //so try JsonServiceClient.get
            this.authClient.get(new ConvertSessionToToken())
                .then(res => {
                    console.log('ConvertSessionToToken.res', res); 
                    console.log('this.bearToken', this.bearerToken); //undefined
                    //next problem there is no function getTokenCookie in Typescript that I can find, not in the dtos and not in JsonServiceClient
                    //var jwtToken = this.getTokenCookie(); //From ss-tok Cookie

                    //maybe this will work?
                    //super.setBearerToken(this.bearerToken)
                    super.get(request).then(res => {
                        console.log('suger.get.res', res);
                        resolve(res);
                    }, msg => {
                        console.log('suger.get.msg', res);
                        reject(msg)
                    })
                    .catch(ex => {
                        console.log('suger.get.ex', ex);
                        reject(ex);
                    });
                }, msg => {
                    console.log('ConvertSessionToToken.msg', msg);
                    reject(msg);
                })
                .catch(ex =>{
                    console.log('ConvertSessionToToken.ex', ex);
                    reject(ex);
                });
        });
    }

}

1 个答案:

答案 0 :(得分:0)

为什么要实现Custom JsonServiceClient?只需使用已有bearerToken的现有refreshTokenJsonServiceClient属性,这些属性已内置支持发送JWT承载令牌并使用刷新令牌重新获取过期的JWT令牌。

您可以查看client.auth.spec.ts以获取支持行为的示例。