Skip to content

Commit 8279807

Browse files
chrisbobbegnprice
authored andcommitted
isAppOwnDomain [nfc]: Define for reuse, and add tests.
1 parent 82fb34b commit 8279807

File tree

3 files changed

+35
-4
lines changed

3 files changed

+35
-4
lines changed

src/__tests__/isAppOwnDomain-test.js

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/* @flow strict-local */
2+
3+
import isAppOwnDomain from '../isAppOwnDomain';
4+
5+
describe('isAppOwnDomain', () => {
6+
test.each([
7+
['https://chat.zulip.org', true],
8+
['https://zulipchat.com', true],
9+
['https://zulip.com', true],
10+
['https://example.zulipchat.com', true],
11+
['https://example.zulip.com', true],
12+
['https://example.zulip.com/api/v1/server_settings', true],
13+
['https://example.zulip.com/avatar/1234', true],
14+
15+
['https://zulipchat.org', false],
16+
['https://www.google.com', false],
17+
['https://zulipchat.co.uk', false],
18+
['https://chat.zulip.io', false],
19+
])('%s should be %p', (urlStr: string, expected: boolean) => {
20+
expect(isAppOwnDomain(new URL(urlStr))).toBe(expected);
21+
});
22+
});

src/isAppOwnDomain.js

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
/* @flow strict-local */
2+
import config from './config';
3+
4+
/**
5+
* Whether a URL is hosted by the same org that publishes the app.
6+
*/
7+
export default function isAppOwnDomain(url: URL): boolean {
8+
return config.appOwnDomains.some(
9+
domain => url.host === domain || url.host.endsWith(`.${domain}`),
10+
);
11+
}

src/start/AuthScreen.js

+2-4
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import type {
1414
import type { RouteProp } from '../react-navigation';
1515
import type { AppNavigationProp } from '../nav/AppNavigator';
1616
import * as NavigationService from '../nav/NavigationService';
17-
import config from '../config';
17+
import isAppOwnDomain from '../isAppOwnDomain';
1818
import type { Dispatch } from '../types';
1919
import {
2020
IconApple,
@@ -295,16 +295,14 @@ class AuthScreenInner extends PureComponent<Props> {
295295
return false;
296296
}
297297

298-
const { host } = this.props.realm;
299-
300298
// The native flow for Apple auth assumes that the app and the server
301299
// are operated by the same organization, so that for a user to
302300
// entrust private information to either one is the same as entrusting
303301
// it to the other. Check that this realm is on such a server.
304302
//
305303
// (For other realms, we'll simply fall back to the web flow, which
306304
// handles things appropriately without relying on that assumption.)
307-
return config.appOwnDomains.some(domain => host === domain || host.endsWith(`.${domain}`));
305+
return isAppOwnDomain(this.props.realm);
308306
};
309307

310308
handleAuth = async (method: AuthenticationMethodDetails) => {

0 commit comments

Comments
 (0)