]> BookStack Code Mirror - website/blob - content/docs/admin/oidc-auth.md
Update filesystem-permissions.md
[website] / content / docs / admin / oidc-auth.md
1 +++
2 title = "OpenID Connect Authentication"
3 description = "How to use an OpenID Connect identity provider as your primary way to access BookStack"
4 date = "2021-10-21"
5 type = "admin-doc"
6 +++
7
8 OpenID Connect (OIDC) can be used within BookStack as a primary method of authentication.
9 This replaces the default email & password authentication mechanism.
10 BookStack supports a simple level of auto-discovery to ease endpoint and key management.
11
12 When used, BookStack will attempt to match the OIDC user to an existing BookStack user
13 based on the "External Authentication ID" value stored against the Bookstack user. 
14 If this match cannot be made, BookStack will effectively auto-register that user to 
15 provide a seamless access experience. They will be given the default role set under the
16 "Default user role after registration" option in the application settings.
17
18 {{<toc>}}
19
20 ### Video Guides
21
22 We have a range of videos available that can help show, for a range of identity platforms, the exact steps required to set-up a BookStack OpenID Connect authentication integration:
23
24 - [General setup guide (Using Okta as an example)](https://www.youtube.com/watch?v=TJQ4NJrMvkw)
25 - [Azure Active Directory OIDC Guide](https://www.youtube.com/watch?v=CL5kMFkopHY)
26 - [Authentik BookStack OIDC Guide](https://www.youtube.com/watch?v=M1_WPhR4hRc)
27
28 ### Requirements & Limitations
29
30 Listed below are some considerations to keep in mind in regard to BookStack's OIDC implementation:
31
32 - Only RS256 is currently supported as a token signing algorithm, Token encryption is not supported.
33 - Discovery covers fetching the auth & token endpoints, in addition to parsing any keys at the JWKS URI,
34   from the `<issuer>/.well-known/openid-configuration` endpoint.
35   - Issuer discovery is not supported.
36
37 ### BookStack Configuration
38
39 To set up OIDC based authentication add or modify the following variables in your `.env` file:
40
41 ```bash
42 # Set OIDC to be the authentication method
43 AUTH_METHOD=oidc
44
45 # Control if BookStack automatically initiates login via your OIDC system 
46 # if it's the only authentication method. Prevents the need for the
47 # user to click the "Login with x" button on the login page.
48 # Setting this to true enables auto-initiation.
49 AUTH_AUTO_INITIATE=false
50
51 # Set the display name to be shown on the login button.
52 # (Login with <name>)
53 OIDC_NAME=SSO
54
55 # Name of the claims(s) to use for the user's display name.
56 # Can have multiple attributes listed, separated with a '|' in which 
57 # case those values will be joined with a space.
58 # Example: OIDC_DISPLAY_NAME_CLAIMS=given_name|family_name
59 OIDC_DISPLAY_NAME_CLAIMS=name
60
61 # OAuth Client ID to access the identity provider
62 OIDC_CLIENT_ID=abc123
63
64 # OAuth Client Secret to access the identity provider
65 OIDC_CLIENT_SECRET=def456
66
67 # Issuer URL
68 # Must start with 'https://'
69 OIDC_ISSUER=https://instance.authsystem.example.com
70
71 # Enable auto-discovery of endpoints and token keys.
72 # As per the standard, expects the service to serve a 
73 # `<issuer>/.well-known/openid-configuration` endpoint.
74 OIDC_ISSUER_DISCOVER=true
75
76 ############################################################
77 ## NOTE: The below are only needed if not using the above ##
78 ##       auto-discovery option.                           ##
79 ############################################################
80
81 # Path to identity provider token signing public RSA key
82 OIDC_PUBLIC_KEY=file:///keys/idp-public-key.pem
83
84 # Full URL to the OIDC authorize endpoint
85 OIDC_AUTH_ENDPOINT=https://instance.authsystem.example.com/v1/authorize
86
87 # Full URL to the OIDC token endpoint
88 OIDC_TOKEN_ENDPOINT=https://instance.authsystem.example.com/v1/token
89 ```
90
91 A user in BookStack will be linked to an OIDC provided account via the `sub` claim.
92 If the value of this ID changes in the identity provider it can be updated in BookStack, 
93 by an admin, by changing the "External Authentication ID" field on the user's profile.
94
95 ### Callback URL
96
97 Should your OIDC provider require a callback URL, the following can be used: `https://example.com/oidc/callback`.
98 Change `https://example.com` to be the base URL of your BookStack instance.
99
100 ### Switching to OIDC with Existing Users
101
102 When switching `AUTH_METHOD` from `standard` to `oidc`, BookStack will not 
103 link OIDC user accounts to existing BookStack users, where the email address is 
104 matching, since the "External Authentication ID" value of the existing BookStack user does 
105 not match the unique user ID provided by the OIDC system.
106
107 You can overcome this situation by logging into BookStack with an admin account while `AUTH_METHOD=standard`.
108 While logged in, change `AUTH_METHOD` to `oidc`.
109 This change of authentication method will show an "External Authentication ID" text
110 field, below the name and email inputs, when viewing a user account in BookStack.
111 Here you can enter the unique user ID that would be provided by your OIDC provider.
112 Once saved BookStack will then use this value to match OIDC and BookStack user 
113 accounts upon next login attempt.
114
115 If you need to update accounts in bulk, you could instead directly update the 
116 `external_auth_id` field of the `users` table within your BookStack database.
117
118 ### Debugging
119
120 To help when setting up or configuring BookStack to use your OIDC system, the below
121 `.env` option can help provide more insight:
122
123 ```bash
124 # Dump out the details fetched from the identity provider.
125 # Only set this option to true if debugging since it will block logins
126 # and potentially show private details.
127 OIDC_DUMP_USER_DETAILS=false
128 ```
129
130 Further to this, details of any BookStack errors encountered can be found by following
131 our [general debugging documentation](/docs/admin/debugging/).
132
133 ### Using a Different ID Claim
134
135 By default, BookStack will use the `sub` claim as a unique identifier to match up a user
136 between BookStack and the identify provider.
137 For the vast majority of use-cases, this is fine since this claim is part of the 
138 OIDC standard.
139
140 In some very select scenarios, you may want to use a different claim as the unique identifier.
141 This can be done by setting an `OIDC_EXTERNAL_ID_CLAIM` option in your `.env` like shown below,
142 where the value of the option is the name of the claim:
143
144 ```bash
145 # Configure a custom ID Token claim to be used as the
146 # "External Authentication ID" within BookStack.
147 OIDC_EXTERNAL_ID_CLAIM=upn
148 ```
149
150 Note that changing this with existing BookStack OIDC users, without changing their "External Authentication ID" values,
151 may cause issues upon future login since their existing store ID in BookStack may no longer align.
152
153 ### Group Sync
154
155 BookStack has the ability to sync OIDC user groups with BookStack roles.
156 By default this will match OIDC group names with the BookStack role display names with casing ignored.
157 This can be overridden by via the 'External Authentication IDs' field which can be seen when editing a role while OIDC authentication is enabled.
158 If filled, the names in this field will be used and the BookStack role display name will be ignored.
159 You can match on multiple names by separating them with a comma.
160 Commas can be escaped with a backslash (`\,`) if you need to map using a literal comma character.
161
162 When matching OIDC groups with role names or 'External Authentication IDs' values, BookStack will standardise the names of OIDC groups to be lower-cased and spaces will be replaced with hyphens. For example, to match a OIDC group named "United Kingdom" an 'External Authentication IDs' value of "united-kingdom" could be used.
163
164 This feature requires the OIDC server to provide a claim in the ID token with an array of group names.
165 You'll need to specify the attribute using the `OIDC_GROUPS_CLAIM` to tell BookStack what claim it can find groups on. This value can use dot-notation to access nested properties in the ID token JSON data, an example of which can be [found below](#nested-groups-claim-example).
166
167 Keep in mind you can use the `OIDC_DUMP_USER_DETAILS` option, as shown in the above [debugging](#debugging) section to dump out claim values provided by your authentication system to help understand what is being provided by your authentication system.
168
169 Here are the settings required to be added to your `.env` file to enable group syncing:
170
171 ```bash
172 # Enable OIDC group sync.
173 OIDC_USER_TO_GROUPS=true
174
175 # Set the attribute from which BookStack will read groups names from.
176 OIDC_GROUPS_CLAIM=groups
177
178 # Additional scopes to send with the authentication request.
179 # By default BookStack only sends the 'openid', 'profile' & 'email' scopes.
180 # Many platforms require specific scopes to be requested for group data.
181 # Multiple scopes can be added via comma separation.
182 OIDC_ADDITIONAL_SCOPES=groups
183
184 # Remove the user from roles that don't match OIDC groups upon login.
185 # Note: While this is enabled the "Default Registration Role", editable within the 
186 # BookStack settings view, will be considered a matched role and assigned to the user.
187 OIDC_REMOVE_FROM_GROUPS=true
188 ```
189
190 #### Nested Groups Claim Example
191
192 The below shows a reduced example of JSON data for an ID token, that has group data within a nested property,
193 along with the `OIDC_GROUPS_CLAIM` value that would be used for this structure to detect the provided "Editor" and "Admin" roles.
194
195 ```bash
196 OIDC_GROUPS_CLAIM=resource_access.bookstack.roles
197 ```
198
199 ```json
200 {
201   ...
202   "resource_access": {
203     "bookstack": {
204       "roles": [
205         "Editor",
206         "Admin"
207       ]
208     }
209   },
210   "email": "[email protected]"
211   ...
212 }
213 ```