Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ensure allowAnonymous works with missing_refresh_token #430

Merged
merged 1 commit into from
Feb 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 33 additions & 13 deletions projects/auth0-angular/src/lib/auth.interceptor.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ describe('The Auth HTTP Interceptor', () => {
it('pass through and do not have access tokens attached', fakeAsync(async (
done: () => void
) => {
config.httpInterceptor = (null as unknown) as HttpInterceptorConfig;
config.httpInterceptor = null as unknown as HttpInterceptorConfig;
await assertPassThruApiCallTo('https://my-api.com/api/public', done);
}));
});
Expand Down Expand Up @@ -310,9 +310,9 @@ describe('The Auth HTTP Interceptor', () => {
it('does not execute HTTP call when not able to retrieve a token', fakeAsync(async (
done: () => void
) => {
((auth0Client.getTokenSilently as unknown) as jest.SpyInstance).mockReturnValue(
throwError({ error: 'login_required' })
);
(
auth0Client.getTokenSilently as unknown as jest.SpyInstance
).mockReturnValue(throwError({ error: 'login_required' }));

httpClient.request('get', 'https://my-api.com/api/calendar').subscribe({
error: (err) => expect(err).toEqual({ error: 'login_required' }),
Expand All @@ -327,19 +327,29 @@ describe('The Auth HTTP Interceptor', () => {
it('does execute HTTP call when not able to retrieve a token but allowAnonymous is set to true', fakeAsync(async (
done: () => void
) => {
((auth0Client.getTokenSilently as unknown) as jest.SpyInstance).mockReturnValue(
throwError({ error: 'login_required' })
);
(
auth0Client.getTokenSilently as unknown as jest.SpyInstance
).mockReturnValue(throwError({ error: 'login_required' }));

await assertPassThruApiCallTo('https://my-api.com/api/orders', done);
}));

it('does execute HTTP call when missing_refresh_token but allowAnonymous is set to true', fakeAsync(async (
done: () => void
) => {
(
auth0Client.getTokenSilently as unknown as jest.SpyInstance
).mockReturnValue(throwError({ error: 'missing_refresh_token' }));

await assertPassThruApiCallTo('https://my-api.com/api/orders', done);
}));

it('emit error when not able to retrieve a token but allowAnonymous is set to false', fakeAsync(async (
done: () => void
) => {
((auth0Client.getTokenSilently as unknown) as jest.SpyInstance).mockRejectedValue(
{ error: 'login_required' }
);
(
auth0Client.getTokenSilently as unknown as jest.SpyInstance
).mockRejectedValue({ error: 'login_required' });

httpClient.request('get', 'https://my-api.com/api/calendar').subscribe({
error: (err) => expect(err).toEqual({ error: 'login_required' }),
Expand All @@ -354,9 +364,19 @@ describe('The Auth HTTP Interceptor', () => {
}));

it('does not emit error when not able to retrieve a token but allowAnonymous is set to true', fakeAsync(async () => {
((auth0Client.getTokenSilently as unknown) as jest.SpyInstance).mockRejectedValue(
{ error: 'login_required' }
);
(
auth0Client.getTokenSilently as unknown as jest.SpyInstance
).mockRejectedValue({ error: 'login_required' });

await assertPassThruApiCallTo('https://my-api.com/api/orders', () => {
expect(authState.setError).not.toHaveBeenCalled();
});
}));

it('does not emit error when missing_refresh_token but allowAnonymous is set to true', fakeAsync(async () => {
(
auth0Client.getTokenSilently as unknown as jest.SpyInstance
).mockRejectedValue({ error: 'missing_refresh_token' });

await assertPassThruApiCallTo('https://my-api.com/api/orders', () => {
expect(authState.setError).not.toHaveBeenCalled();
Expand Down
15 changes: 9 additions & 6 deletions projects/auth0-angular/src/lib/auth.interceptor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,17 +31,18 @@ import { Auth0ClientService } from './auth.client';
import { AuthState } from './auth.state';
import { AuthService } from './auth.service';

const waitUntil = <TSignal>(signal$: Observable<TSignal>) => <TSource>(
source$: Observable<TSource>
) => source$.pipe(mergeMap((value) => signal$.pipe(first(), mapTo(value))));
const waitUntil =
<TSignal>(signal$: Observable<TSignal>) =>
<TSource>(source$: Observable<TSource>) =>
source$.pipe(mergeMap((value) => signal$.pipe(first(), mapTo(value))));

@Injectable()
export class AuthHttpInterceptor implements HttpInterceptor {
constructor(
private configFactory: AuthClientConfig,
@Inject(Auth0ClientService) private auth0Client: Auth0Client,
private authState: AuthState,
private authService: AuthService,
private authService: AuthService
) {}

intercept(
Expand All @@ -54,7 +55,7 @@ export class AuthHttpInterceptor implements HttpInterceptor {
}

const isLoaded$ = this.authService.isLoading$.pipe(
filter((isLoading) => !isLoading),
filter((isLoading) => !isLoading)
);

return this.findMatchingRoute(req, config.httpInterceptor).pipe(
Expand Down Expand Up @@ -207,7 +208,9 @@ export class AuthHttpInterceptor implements HttpInterceptor {
!!route &&
isHttpInterceptorRouteConfig(route) &&
!!route.allowAnonymous &&
['login_required', 'consent_required'].includes(err.error)
['login_required', 'consent_required', 'missing_refresh_token'].includes(
err.error
)
);
}
}