PageRenderTime 23ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/docs/auth/index.md

https://github.com/backstage/backstage
Markdown | 118 lines | 97 code | 21 blank | 0 comment | 0 complexity | 4de93eee78e0d3a8e6e7573b56849655 MD5 | raw file
Possible License(s): Apache-2.0, MIT, BSD-3-Clause, MPL-2.0-no-copyleft-exception
  1. ---
  2. id: index
  3. title: Adding Authentication
  4. description: How to add authentication to a Backstage application
  5. ---
  6. Authentication in Backstage identifies the user, and provides a way for plugins
  7. to make requests on behalf of a user to third-party services. Backstage can have
  8. zero (guest access), one, or many authentication providers. The default
  9. `@backstage/create-app` template uses guest access for easy startup.
  10. See [Using authentication and identity](using-auth.md) for tips on using
  11. Backstage identity information in your app or plugins.
  12. ## Adding an authentication provider
  13. Backstage comes with many common authentication providers in the core library:
  14. - [Auth0](auth0/provider.md)
  15. - [Azure](microsoft/provider.md)
  16. - [GitHub](github/provider.md)
  17. - [GitLab](gitlab/provider.md)
  18. - [Google](google/provider.md)
  19. - [Okta](okta/provider.md)
  20. - [OneLogin](onelogin/provider.md)
  21. These built-in providers handle the authentication flow for a particular service
  22. including required scopes, callbacks, etc. These providers are each added to a
  23. Backstage app in a similar way.
  24. ### Adding provider configuration
  25. Each built-in provider has a configuration block under the `auth` section of
  26. `app-config.yaml`. For example, the GitHub provider:
  27. ```yaml
  28. auth:
  29. environment: development
  30. providers:
  31. github:
  32. development:
  33. clientId: ${AUTH_GITHUB_CLIENT_ID}
  34. clientSecret: ${AUTH_GITHUB_CLIENT_SECRET}
  35. ```
  36. See the documentation for a particular provider to see what configuration is
  37. needed.
  38. The `providers` key may have several authentication providers, if multiple
  39. authentication methods are supported. Each provider may also have configuration
  40. for different authentication environments (development, production, etc). This
  41. allows a single auth backend to serve multiple environments, such as running a
  42. local frontend against a deployed backend. The provider configuration matching
  43. the local `auth.environment` setting will be selected.
  44. ### Adding the provider to the sign-in page
  45. After configuring an authentication provider, the `app` frontend package needs a
  46. small update to show this provider as a login option. The `SignInPage` component
  47. handles this, and takes either a `provider` or `providers` (array) prop of
  48. `SignInProviderConfig` definitions.
  49. These reference the `ApiRef` exported by the provider. Again, an example using
  50. GitHub that can be adapted to any of the built-in providers:
  51. ```diff
  52. # packages/app/src/App.tsx
  53. + import { githubAuthApiRef } from '@backstage/core-plugin-api';
  54. + import { SignInProviderConfig, SignInPage } from '@backstage/core-components';
  55. + const githubProvider: SignInProviderConfig = {
  56. + id: 'github-auth-provider',
  57. + title: 'GitHub',
  58. + message: 'Sign in using GitHub',
  59. + apiRef: githubAuthApiRef,
  60. +};
  61. +
  62. const app = createApp({
  63. apis,
  64. + components: {
  65. + SignInPage: props => (
  66. + <SignInPage
  67. + {...props}
  68. + auto
  69. + provider={githubProvider}
  70. + />
  71. + ),
  72. + },
  73. bindRoutes({ bind }) {
  74. ```
  75. To also allow unauthenticated guest access, use the `providers` prop for
  76. `SignInPage`:
  77. ```diff
  78. const app = createApp({
  79. apis,
  80. + components: {
  81. + SignInPage: props => (
  82. + <SignInPage
  83. + {...props}
  84. + providers={['guest', githubProvider]}
  85. + />
  86. + ),
  87. + },
  88. bindRoutes({ bind }) {
  89. ```
  90. ## Adding a custom authentication provider
  91. There are generic authentication providers for OAuth2 and SAML. These can reduce
  92. the amount of code needed to implement a custom authentication provider that
  93. adheres to these standards.
  94. Backstage uses [Passport](http://www.passportjs.org/) under the hood, which has
  95. a wide library of authentication strategies for different providers. See
  96. [Add authentication provider](add-auth-provider.md) for details on adding a new
  97. Passport-supported authentication method.