organizationAuthList.tsx 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. import EmptyMessage from 'sentry/components/emptyMessage';
  2. import ExternalLink from 'sentry/components/links/externalLink';
  3. import Panel from 'sentry/components/panels/panel';
  4. import PanelAlert from 'sentry/components/panels/panelAlert';
  5. import PanelBody from 'sentry/components/panels/panelBody';
  6. import PanelHeader from 'sentry/components/panels/panelHeader';
  7. import {t, tct} from 'sentry/locale';
  8. import type {AuthProvider} from 'sentry/types/auth';
  9. import type {Organization} from 'sentry/types/organization';
  10. import {descopeFeatureName} from 'sentry/utils';
  11. import getCsrfToken from 'sentry/utils/getCsrfToken';
  12. import withOrganization from 'sentry/utils/withOrganization';
  13. import SettingsPageHeader from 'sentry/views/settings/components/settingsPageHeader';
  14. import PermissionAlert from 'sentry/views/settings/organization/permissionAlert';
  15. import ProviderItem from './providerItem';
  16. const PROVIDER_POPULARITY: Record<string, number> = {
  17. google: 0,
  18. github: 1,
  19. okta: 2,
  20. 'active-directory': 3,
  21. saml2: 4,
  22. onelogin: 5,
  23. rippling: 6,
  24. auth0: 7,
  25. jumpcloud: 8,
  26. };
  27. type Props = {
  28. organization: Organization;
  29. providerList: AuthProvider[];
  30. activeProvider?: AuthProvider;
  31. };
  32. function OrganizationAuthList({organization, providerList, activeProvider}: Props) {
  33. const features = organization.features;
  34. // Sort provider list twice: first, by popularity,
  35. // and then a second time, to sort unavailable providers for the current plan to the end of the list.
  36. const sortedByPopularity = (providerList ?? []).sort((a, b) => {
  37. if (!(a.key in PROVIDER_POPULARITY)) {
  38. return -1;
  39. }
  40. if (!(b.key in PROVIDER_POPULARITY)) {
  41. return 1;
  42. }
  43. if (PROVIDER_POPULARITY[a.key] === PROVIDER_POPULARITY[b.key]) {
  44. return 0;
  45. }
  46. return PROVIDER_POPULARITY[a.key] > PROVIDER_POPULARITY[b.key] ? 1 : -1;
  47. });
  48. const list = sortedByPopularity.sort((a, b) => {
  49. const aEnabled = features.includes(descopeFeatureName(a.requiredFeature));
  50. const bEnabled = features.includes(descopeFeatureName(b.requiredFeature));
  51. if (aEnabled === bEnabled) {
  52. return 0;
  53. }
  54. return aEnabled ? -1 : 1;
  55. });
  56. const warn2FADisable =
  57. organization.require2FA &&
  58. list.some(({requiredFeature}) =>
  59. features.includes(descopeFeatureName(requiredFeature))
  60. );
  61. return (
  62. <div className="sso">
  63. <SettingsPageHeader title="Authentication" />
  64. <PermissionAlert />
  65. <Panel>
  66. <PanelHeader>{t('Choose a provider')}</PanelHeader>
  67. <PanelBody>
  68. {!activeProvider && (
  69. <PanelAlert type="info">
  70. {tct(
  71. 'Get started with Single Sign-on for your organization by selecting a provider. Read more in our [link:SSO documentation].',
  72. {
  73. link: (
  74. <ExternalLink href="https://docs.sentry.io/product/accounts/sso/" />
  75. ),
  76. }
  77. )}
  78. </PanelAlert>
  79. )}
  80. {warn2FADisable && (
  81. <PanelAlert type="warning">
  82. {t('Require 2FA will be disabled if you enable SSO.')}
  83. </PanelAlert>
  84. )}
  85. <form
  86. action={`/organizations/${organization.slug}/auth/configure/`}
  87. method="POST"
  88. >
  89. <input type="hidden" name="csrfmiddlewaretoken" value={getCsrfToken()} />
  90. <input type="hidden" name="init" value="1" />
  91. {list.map(provider => (
  92. <ProviderItem
  93. key={provider.key}
  94. provider={provider}
  95. active={!!activeProvider && provider.key === activeProvider.key}
  96. />
  97. ))}
  98. {list.length === 0 && (
  99. <EmptyMessage>
  100. {t('No authentication providers are available.')}
  101. </EmptyMessage>
  102. )}
  103. </form>
  104. </PanelBody>
  105. </Panel>
  106. </div>
  107. );
  108. }
  109. export default withOrganization(OrganizationAuthList);
  110. // For tests
  111. export {OrganizationAuthList};