Skip to content

Commit 572458a

Browse files
authored
Merge pull request #668 from auth0/feature/remove-black-white-list-references
Replace black and whitelist references
2 parents 3c45ffb + ac9e111 commit 572458a

File tree

6 files changed

+58
-69
lines changed

6 files changed

+58
-69
lines changed

README.md

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ const isExpired = helper.isTokenExpired(myRawToken);
4040

4141
## Usage: Injection
4242

43-
Import the `JwtModule` module and add it to your imports list. Call the `forRoot` method and provide a `tokenGetter` function. You must also whitelist any domains that you want to make requests to by specifying a `whitelistedDomains` array.
43+
Import the `JwtModule` module and add it to your imports list. Call the `forRoot` method and provide a `tokenGetter` function. You must also add any domains to the `allowedDomains`, that you want to make requests to by specifying an `allowedDomains` array.
4444

4545
Be sure to import the `HttpClientModule` as well.
4646

@@ -60,8 +60,8 @@ export function tokenGetter() {
6060
JwtModule.forRoot({
6161
config: {
6262
tokenGetter: tokenGetter,
63-
whitelistedDomains: ["example.com"],
64-
blacklistedRoutes: ["http://example.com/examplebadroute/"],
63+
allowedDomains: ["example.com"],
64+
disallowedRoutes: ["http://example.com/examplebadroute/"],
6565
},
6666
}),
6767
],
@@ -122,33 +122,33 @@ JwtModule.forRoot({
122122
});
123123
```
124124

125-
### `whitelistedDomains: array`
125+
### `allowedDomains: array`
126126

127127
Authenticated requests should only be sent to domains you know and trust. Many applications make requests to APIs from multiple domains, some of which are not controlled by the developer. Since there is no way to know what the API being called will do with the information contained in the request, it is best to not send the user's token to all APIs in a blind fashion.
128128

129-
List any domains you wish to allow authenticated requests to be sent to by specifying them in the `whitelistedDomains` array. **Note that standard http port 80 and https port 443 requests don't require a port to be specified. A port is only required in the whitelisted host name if you are authenticating against a non-standard port e.g. localhost:3001**
129+
List any domains you wish to allow authenticated requests to be sent to by specifying them in the `allowedDomains` array. **Note that standard http port 80 and https port 443 requests don't require a port to be specified. A port is only required in the allowed domains host name if you are authenticating against a non-standard port e.g. localhost:3001**
130130

131131
```ts
132132
// ...
133133
JwtModule.forRoot({
134134
config: {
135135
// ...
136-
whitelistedDomains: ["localhost:3001", "foo.com", "bar.com"],
136+
allowedDomains: ["localhost:3001", "foo.com", "bar.com"],
137137
},
138138
});
139139
```
140140

141-
### `blacklistedRoutes: array`
141+
### `disallowedRoutes: array`
142142

143143
If you do not want to replace the authorization headers for specific routes, list them here. This can be useful if your
144-
initial auth route(s) are on a whitelisted domain and take basic auth headers. These routes need to be prefixed with the correct protocol (`http://`, `https://`). If you want to blacklist the route regardless of the protocol, you can prefix it with `//`.
144+
initial auth route(s) are on an allowed domain and take basic auth headers. These routes need to be prefixed with the correct protocol (`http://`, `https://`). If you want to add a route to the list of disallowed routes regardless of the protocol, you can prefix it with `//`.
145145

146146
```ts
147147
// ...
148148
JwtModule.forRoot({
149149
config: {
150150
// ...
151-
blacklistedRoutes: [
151+
disallowedRoutes: [
152152
"http://localhost:3001/auth/",
153153
"https://foo.com/bar/",
154154
"//foo.com/bar/baz",
@@ -158,21 +158,21 @@ JwtModule.forRoot({
158158
});
159159
```
160160

161-
**Note:** If requests are sent to the same domain that is serving your Angular application, you do not need to add that domain to the `whitelistedDomains` array. However, this is only the case if you don't specify the domain in the `Http` request.
161+
**Note:** If requests are sent to the same domain that is serving your Angular application, you do not need to add that domain to the `allowedDomains` array. However, this is only the case if you don't specify the domain in the `Http` request.
162162

163-
For example, the following request assumes that the domain is the same as the one serving your app. It doesn't need to be whitelisted in this case.
163+
For example, the following request assumes that the domain is the same as the one serving your app. It doesn't need to be allowed in this case.
164164

165165
```ts
166166
this.http.get('/api/things')
167167
.subscribe(...)
168168
```
169169

170-
However, if you are serving your API at the same domain as that which is serving your Angular app **and** you are specifying that domain in `Http` requests, then it **does** need to be whitelisted.
170+
However, if you are serving your API at the same domain as that which is serving your Angular app **and** you are specifying that domain in `Http` requests, then it **does** need to be explicitely allowed.
171171

172172
```ts
173173
// Both the Angular app and the API are served at
174174
// localhost:4200 but because that domain is specified
175-
// in the request, it must be whitelisted
175+
// in the request, it must be allowed
176176
this.http.get('http://localhost:4200/api/things')
177177
.subscribe(...)
178178
```
@@ -213,7 +213,7 @@ JwtModule.forRoot({
213213
config: {
214214
// ...
215215
authScheme: (request) => {
216-
if (request.url.includes("foo")) {
216+
if (request.url.includes("foo")) {
217217
return "Basic ";
218218
}
219219

@@ -270,7 +270,7 @@ export function jwtOptionsFactory(tokenService) {
270270
tokenGetter: () => {
271271
return tokenService.getAsyncToken();
272272
},
273-
whitelistedDomains: ["example.com"]
273+
allowedDomains: ["example.com"]
274274
}
275275
}
276276

@@ -306,7 +306,7 @@ export function jwtOptionsFactory(storage) {
306306
tokenGetter: () => {
307307
return storage.get('access_token');
308308
},
309-
whitelistedDomains: ["example.com"]
309+
allowedDomains: ["example.com"]
310310
}
311311
}
312312

package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@
99
"test": "ng test",
1010
"lint": "ng lint",
1111
"e2e": "ng e2e",
12-
"release": "np",
13-
"release:preview": "np --preview ",
14-
"release:dry": "np --no-publish"
12+
"release": "np --contents ./dist/angular-jwt/ --any-branch",
13+
"release:preview": "np --preview --contents ./dist/angular-jwt/ --any-branch",
14+
"release:dry": "np --no-publish --contents ./dist/angular-jwt/ --any-branch"
1515
},
1616
"private": false,
1717
"dependencies": {

projects/angular-jwt/package.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,9 @@
2020
],
2121
"homepage": "https://github.com/auth0/angular2-jwt",
2222
"peerDependencies": {
23-
"@angular/common": ">=7.1.0"
23+
"@angular/common": ">=9.0.0"
2424
},
25-
"dependencies": {
26-
"tslib": "^2.0.0"
27-
}
25+
"dependencies": {
26+
"tslib": "^2.0.0"
27+
}
2828
}

projects/angular-jwt/src/lib/angular-jwt.module.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,11 @@ export interface JwtConfig {
1616
) => string | null | Promise<string | null>;
1717
headerName?: string;
1818
authScheme?: string | ((request?: HttpRequest<any>) => string);
19-
whitelistedDomains?: Array<string | RegExp>;
20-
blacklistedRoutes?: Array<string | RegExp>;
19+
allowedDomains?: Array<string | RegExp>;
20+
disallowedRoutes?: Array<string | RegExp>;
2121
throwNoTokenError?: boolean;
2222
skipWhenExpired?: boolean;
23-
};
23+
}
2424

2525
export interface JwtModuleOptions {
2626
jwtOptionsProvider?: Provider;

projects/angular-jwt/src/lib/jwt.interceptor.ts

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@ export class JwtInterceptor implements HttpInterceptor {
1818
) => string | null | Promise<string | null>;
1919
headerName: string;
2020
authScheme: string | ((request?: HttpRequest<any>) => string);
21-
whitelistedDomains: Array<string | RegExp>;
22-
blacklistedRoutes: Array<string | RegExp>;
21+
allowedDomains: Array<string | RegExp>;
22+
disallowedRoutes: Array<string | RegExp>;
2323
throwNoTokenError: boolean;
2424
skipWhenExpired: boolean;
2525
standardPorts: string[] = ["80", "443"];
@@ -34,30 +34,30 @@ export class JwtInterceptor implements HttpInterceptor {
3434
config.authScheme || config.authScheme === ""
3535
? config.authScheme
3636
: "Bearer ";
37-
this.whitelistedDomains = config.whitelistedDomains || [];
38-
this.blacklistedRoutes = config.blacklistedRoutes || [];
37+
this.allowedDomains = config.allowedDomains || [];
38+
this.disallowedRoutes = config.disallowedRoutes || [];
3939
this.throwNoTokenError = config.throwNoTokenError || false;
4040
this.skipWhenExpired = config.skipWhenExpired;
4141
}
4242

43-
isWhitelistedDomain(request: HttpRequest<any>): boolean {
43+
isAllowedDomain(request: HttpRequest<any>): boolean {
4444
const requestUrl: URL = new URL(request.url, window.location.origin);
4545

4646
// If the host equals the current window origin,
47-
// the domain is whitelisted by default
47+
// the domain is allowed by default
4848
if (requestUrl.host === window.location.host) {
4949
return true;
5050
}
5151

52-
// If not the current domain, check the whitelist
52+
// If not the current domain, check the allowed list
5353
const hostName = `${requestUrl.hostname}${
5454
requestUrl.port && !this.standardPorts.includes(requestUrl.port)
5555
? ":" + requestUrl.port
5656
: ""
5757
}`;
5858

5959
return (
60-
this.whitelistedDomains.findIndex((domain) =>
60+
this.allowedDomains.findIndex((domain) =>
6161
typeof domain === "string"
6262
? domain === hostName
6363
: domain instanceof RegExp
@@ -67,11 +67,11 @@ export class JwtInterceptor implements HttpInterceptor {
6767
);
6868
}
6969

70-
isBlacklistedRoute(request: HttpRequest<any>): boolean {
70+
isDisallowedRoute(request: HttpRequest<any>): boolean {
7171
const requestedUrl: URL = new URL(request.url, window.location.origin);
7272

7373
return (
74-
this.blacklistedRoutes.findIndex((route: string | RegExp) => {
74+
this.disallowedRoutes.findIndex((route: string | RegExp) => {
7575
if (typeof route === "string") {
7676
const parsedRoute: URL = new URL(route, window.location.origin);
7777
return (
@@ -121,10 +121,7 @@ export class JwtInterceptor implements HttpInterceptor {
121121
request: HttpRequest<any>,
122122
next: HttpHandler
123123
): Observable<HttpEvent<any>> {
124-
if (
125-
!this.isWhitelistedDomain(request) ||
126-
this.isBlacklistedRoute(request)
127-
) {
124+
if (!this.isAllowedDomain(request) || this.isDisallowedRoute(request)) {
128125
return next.handle(request);
129126
}
130127
const token = this.tokenGetter(request);

src/app/services/example-http.service.spec.ts

Lines changed: 21 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -28,20 +28,20 @@ describe("Example HttpService: with simple tokken getter", () => {
2828

2929
const validRoutes = [
3030
`/assets/example-resource.json`,
31-
`http://whitelisted.com/api/`,
32-
`http://whitelisted.com/api/test`,
33-
`http://whitelisted.com:443/api/test`,
34-
`http://whitelisted-regex.com/api/`,
35-
`https://whitelisted-regex.com/api/`,
31+
`http://allowed.com/api/`,
32+
`http://allowed.com/api/test`,
33+
`http://allowed.com:443/api/test`,
34+
`http://allowed-regex.com/api/`,
35+
`https://allowed-regex.com/api/`,
3636
`http://localhost:3000`,
3737
`http://localhost:3000/api`,
3838
];
3939
const invalidRoutes = [
40-
`http://whitelisted.com/api/blacklisted`,
41-
`http://whitelisted.com/api/blacklisted-protocol`,
42-
`http://whitelisted.com:80/api/blacklisted-protocol`,
43-
`http://whitelisted.com/api/blacklisted-regex`,
44-
`http://whitelisted-regex.com/api/blacklisted-regex`,
40+
`http://allowed.com/api/disallowed`,
41+
`http://allowed.com/api/disallowed-protocol`,
42+
`http://allowed.com:80/api/disallowed-protocol`,
43+
`http://allowed.com/api/disallowed-regex`,
44+
`http://allowed-regex.com/api/disallowed-regex`,
4545
`http://foo.com/bar`,
4646
"http://localhost/api",
4747
"http://localhost:4000/api",
@@ -54,15 +54,11 @@ describe("Example HttpService: with simple tokken getter", () => {
5454
JwtModule.forRoot({
5555
config: {
5656
tokenGetter: tokenGetter,
57-
whitelistedDomains: [
58-
"whitelisted.com",
59-
/whitelisted-regex*/,
60-
"localhost:3000",
61-
],
62-
blacklistedRoutes: [
63-
"http://whitelisted.com/api/blacklisted-protocol",
64-
"//whitelisted.com/api/blacklisted",
65-
/blacklisted-regex*/,
57+
allowedDomains: ["allowed.com", /allowed-regex*/, "localhost:3000"],
58+
disallowedRoutes: [
59+
"http://allowed.com/api/disallowed-protocol",
60+
"//allowed.com/api/disallowed",
61+
/disallowed-regex*/,
6662
],
6763
},
6864
}),
@@ -77,7 +73,7 @@ describe("Example HttpService: with simple tokken getter", () => {
7773
});
7874

7975
validRoutes.forEach((route) =>
80-
it(`should set the correct auth token for a whitelisted domain: ${route}`, () => {
76+
it(`should set the correct auth token for a allowed domain: ${route}`, () => {
8177
service.testRequest(route).subscribe((response) => {
8278
expect(response).toBeTruthy();
8379
});
@@ -92,7 +88,7 @@ describe("Example HttpService: with simple tokken getter", () => {
9288
);
9389

9490
invalidRoutes.forEach((route) =>
95-
it(`should not set the auth token for a blacklisted route: ${route}`, () => {
91+
it(`should not set the auth token for a disallowed route: ${route}`, () => {
9692
service.testRequest(route).subscribe((response) => {
9793
expect(response).toBeTruthy();
9894
});
@@ -120,11 +116,7 @@ describe("Example HttpService: with request based tokken getter", () => {
120116
JwtModule.forRoot({
121117
config: {
122118
tokenGetter: tokenGetterWithRequest,
123-
whitelistedDomains: [
124-
"example-1.com",
125-
"example-2.com",
126-
"example-3.com",
127-
],
119+
allowedDomains: ["example-1.com", "example-2.com", "example-3.com"],
128120
},
129121
}),
130122
],
@@ -175,7 +167,7 @@ authSchemes.forEach((scheme) => {
175167
config: {
176168
tokenGetter: tokenGetter,
177169
authScheme: scheme[0],
178-
whitelistedDomains: ["whitelisted.com"],
170+
allowedDomains: ["allowed.com"],
179171
},
180172
}),
181173
],
@@ -185,11 +177,11 @@ authSchemes.forEach((scheme) => {
185177
});
186178

187179
it(`should set the correct auth scheme a request (${scheme[1]})`, () => {
188-
service.testRequest("http://whitelisted.com").subscribe((response) => {
180+
service.testRequest("http://allowed.com").subscribe((response) => {
189181
expect(response).toBeTruthy();
190182
});
191183

192-
const httpRequest = httpMock.expectOne("http://whitelisted.com");
184+
const httpRequest = httpMock.expectOne("http://allowed.com");
193185
expect(httpRequest.request.headers.has("Authorization")).toEqual(true);
194186
expect(httpRequest.request.headers.get("Authorization")).toEqual(
195187
`${scheme[1]}${tokenGetter()}`

0 commit comments

Comments
 (0)