Authentication.tsx 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. import { PermissionAction } from '@supabase/shared-types/out/constants'
  2. import { useParams } from 'common'
  3. import CodeSnippet from './CodeSnippet'
  4. import { DocSection } from './DocSection'
  5. import Snippets from './Snippets'
  6. import { InlineLink } from '@/components/ui/InlineLink'
  7. import { getKeys, useAPIKeysQuery } from '@/data/api-keys/api-keys-query'
  8. import { useProjectSettingsV2Query } from '@/data/config/project-settings-v2-query'
  9. import { useAsyncCheckPermissions } from '@/hooks/misc/useCheckPermissions'
  10. interface AuthenticationProps {
  11. selectedLang: 'bash' | 'js'
  12. showApiKey: string
  13. }
  14. const Authentication = ({ selectedLang, showApiKey }: AuthenticationProps) => {
  15. const { ref: projectRef } = useParams()
  16. const { can: canReadAPIKeys } = useAsyncCheckPermissions(PermissionAction.SECRETS_READ, '*')
  17. const { data: apiKeys } = useAPIKeysQuery({ projectRef }, { enabled: canReadAPIKeys })
  18. const { data: settings } = useProjectSettingsV2Query({ projectRef })
  19. const { anonKey, serviceKey } = getKeys(apiKeys)
  20. const protocol = settings?.app_config?.protocol ?? 'https'
  21. const hostEndpoint = settings?.app_config?.endpoint
  22. const endpoint = `${protocol}://${hostEndpoint ?? ''}`
  23. // [Joshen] ShowApiKey should really be a boolean, its confusing
  24. const defaultApiKey =
  25. showApiKey !== 'BRIVEN_KEY'
  26. ? (anonKey?.api_key ?? 'BRIVEN_CLIENT_API_KEY')
  27. : 'BRIVEN_CLIENT_API_KEY'
  28. const serviceApiKey =
  29. showApiKey !== 'BRIVEN_KEY'
  30. ? (serviceKey?.api_key ?? 'BRIVEN_SERVICE_KEY')
  31. : 'BRIVEN_SERVICE_KEY'
  32. return (
  33. <div className="flex flex-col flex-1">
  34. <DocSection
  35. title="Authentication"
  36. content={
  37. <>
  38. <p>Briven works through a mixture of JWT and Key auth.</p>
  39. <p>
  40. If no <code>Authorization</code> header is included, the API will assume that you are
  41. making a request with an anonymous user.
  42. </p>
  43. <p>
  44. If an <code>Authorization</code> header is included, the API will "switch" to the role
  45. of the user making the request. See the User Management section for more details.
  46. </p>
  47. <p>We recommend setting your keys as Environment Variables.</p>
  48. </>
  49. }
  50. />
  51. <DocSection
  52. title="Client API Keys"
  53. content={
  54. <>
  55. <p>
  56. Client keys allow "anonymous access" to your database, until the user has logged in.
  57. After logging in the keys will switch to the user's own login token.
  58. </p>
  59. <p>
  60. In this documentation, we will refer to the key using the name{' '}
  61. <code>BRIVEN_KEY</code>.
  62. </p>
  63. <p>
  64. We have provided you a Client Key to get started. You will soon be able to add as many
  65. keys as you like. You can find the <code>anon</code> key in the{' '}
  66. <InlineLink href={`/project/${projectRef}/settings/api-keys`}>
  67. API Keys Settings
  68. </InlineLink>{' '}
  69. page.
  70. </p>
  71. </>
  72. }
  73. snippets={
  74. <>
  75. <CodeSnippet
  76. selectedLang={selectedLang}
  77. snippet={Snippets.authKey('CLIENT API KEY', 'BRIVEN_KEY', defaultApiKey)}
  78. />
  79. <CodeSnippet
  80. selectedLang={selectedLang}
  81. snippet={Snippets.authKeyExample(defaultApiKey, endpoint, {
  82. showBearer: false,
  83. })}
  84. />
  85. </>
  86. }
  87. />
  88. <DocSection
  89. title="Service Keys"
  90. content={
  91. <>
  92. <p>
  93. Service keys have FULL access to your data, bypassing any security policies. Be VERY
  94. careful where you expose these keys. They should only be used on a server and never on
  95. a client or browser.
  96. </p>
  97. <p>
  98. In this documentation, we will refer to the key using the name{' '}
  99. <code>SERVICE_KEY</code>.
  100. </p>
  101. <p>
  102. We have provided you with a Service Key to get started. Soon you will be able to add
  103. as many keys as you like. You can find the <code>service_role</code> in the{' '}
  104. <InlineLink href={`/project/${projectRef}/settings/api-keys`}>
  105. API Keys Settings
  106. </InlineLink>{' '}
  107. page.
  108. </p>
  109. </>
  110. }
  111. snippets={
  112. <>
  113. <CodeSnippet
  114. selectedLang={selectedLang}
  115. snippet={Snippets.authKey('SERVICE KEY', 'SERVICE_KEY', serviceApiKey)}
  116. />
  117. <CodeSnippet
  118. selectedLang={selectedLang}
  119. snippet={Snippets.authKeyExample(serviceApiKey, endpoint, {
  120. keyName: 'SERVICE_KEY',
  121. })}
  122. />
  123. </>
  124. }
  125. />
  126. </div>
  127. )
  128. }
  129. export default Authentication