Skip to content

Commit

Permalink
fix(module:auth): fix unrecognized full URL anonymous key (#328)
Browse files Browse the repository at this point in the history
  • Loading branch information
cipchk committed Dec 18, 2018
1 parent 4e681dc commit e223ccb
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 39 deletions.
59 changes: 22 additions & 37 deletions packages/auth/src/token/base.interceptor.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,19 +101,15 @@ describe('auth: base.interceptor', () => {
it(`should be ignore /login`, (done: () => void) => {
genModule({ ignores: [/assets\//, /\/login/] }, basicModel);

http.get('/login', { responseType: 'text' }).subscribe(value => {
done();
});
http.get('/login', { responseType: 'text' }).subscribe(done);
const req = httpBed.expectOne('/login') as TestRequest;
expect(req.request.headers.get('token')).toBeNull();
req.flush('ok!');
});

it('should be non-ignore', (done: () => void) => {
genModule({ ignores: null }, basicModel);
http.get('/login', { responseType: 'text' }).subscribe(value => {
done();
});
http.get('/login', { responseType: 'text' }).subscribe(done);
const req = httpBed.expectOne('/login') as TestRequest;
expect(req.request.headers.get('token')).toBe('123');
req.flush('ok!');
Expand All @@ -123,32 +119,29 @@ describe('auth: base.interceptor', () => {
describe('#with allow_anonymous_key', () => {
it(`in params`, (done: () => void) => {
genModule({}, genModel(SimpleTokenModel, null));
http
.get('/user', {
responseType: 'text',
params: { _allow_anonymous: '' },
})
.subscribe(value => {
done();
});
const ret = httpBed.expectOne(
req => req.method === 'GET' && req.url === '/user',
) as TestRequest;
http.get('/user', { responseType: 'text', params: { _allow_anonymous: '' } }).subscribe(done);
const ret = httpBed.expectOne(() => true);
expect(ret.request.headers.get('Authorization')).toBeNull();
ret.flush('ok!');
});
it(`in params (full url)`, (done: () => void) => {
genModule({}, genModel(SimpleTokenModel, null));
http.get('https://ng-alain.com/api/user', { responseType: 'text', params: { _allow_anonymous: '' } }).subscribe(done);
const ret = httpBed.expectOne(() => true);
expect(ret.request.headers.get('Authorization')).toBeNull();
ret.flush('ok!');
});
it(`in url`, (done: () => void) => {
genModule({}, genModel(SimpleTokenModel, null));
http
.get('/user?_allow_anonymous=1', {
responseType: 'text',
})
.subscribe(value => {
done();
});
const ret = httpBed.expectOne(
req => req.method === 'GET',
) as TestRequest;
http.get('/user?_allow_anonymous=1', { responseType: 'text' }).subscribe(done);
const ret = httpBed.expectOne(() => true);
expect(ret.request.headers.get('Authorization')).toBeNull();
ret.flush('ok!');
});
it(`in url (full url)`, (done: () => void) => {
genModule({}, genModel(SimpleTokenModel, null));
http.get('https://ng-alain.com/api/user?_allow_anonymous=1', { responseType: 'text' }).subscribe(done);
const ret = httpBed.expectOne(() => true);
expect(ret.request.headers.get('Authorization')).toBeNull();
ret.flush('ok!');
});
Expand All @@ -175,12 +168,7 @@ describe('auth: base.interceptor', () => {
});
it('with location', (done: () => void) => {
const login_url = 'https://ng-alain.com/login';
genModule(
{
login_url,
},
genModel(SimpleTokenModel, null),
);
genModule({ login_url }, genModel(SimpleTokenModel, null));
http.get('/test', { responseType: 'text' }).subscribe(
() => {
expect(false).toBe(true);
Expand All @@ -198,10 +186,7 @@ describe('auth: base.interceptor', () => {
});

it('should be not navigate to login when token_invalid_redirect: false', (done: () => void) => {
genModule(
{ token_invalid_redirect: false },
genModel(SimpleTokenModel, null),
);
genModule({ token_invalid_redirect: false }, genModel(SimpleTokenModel, null));
http.get('/test', { responseType: 'text' }).subscribe(
() => {
expect(false).toBe(true);
Expand Down
3 changes: 1 addition & 2 deletions packages/auth/src/token/base.interceptor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import {
HttpRequest,
} from '@angular/common/http';
import { Injector, Optional } from '@angular/core';
import { Router } from '@angular/router';
import { Observable, Observer } from 'rxjs';

import { _HttpClient } from '@delon/theme';
Expand Down Expand Up @@ -35,7 +34,7 @@ export abstract class BaseInterceptor implements HttpInterceptor {

if (
options.allow_anonymous_key &&
(req.params.has(options.allow_anonymous_key) || this.injector.get(Router).parseUrl(req.urlWithParams).queryParamMap.has(options.allow_anonymous_key))
(req.params.has(options.allow_anonymous_key) || new RegExp(`[\?|&]${options.allow_anonymous_key}=[^&]+`).test(req.urlWithParams))
) {
return next.handle(req);
}
Expand Down

0 comments on commit e223ccb

Please sign in to comment.