]> BookStack Code Mirror - website/blob - content/docs/admin/oidc-auth.md
Actualiser content/docs/admin/installation.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://foss.video/w/1b6sc98un7ugGv98v9UaRC)
25 - [Azure Active Directory OIDC Guide](https://foss.video/w/n67qNijhf8BdTRQys8SDYf)
26 - [Authentik BookStack OIDC Guide](https://foss.video/w/a744K8GxFF1LqBFSadAsuV)
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. This applies to both ID tokens and userinfo responses.
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 - RP-initiated logout is supported if enabled, but any other logout mechanisms are not supported.
37
38 ### BookStack Configuration
39
40 To set up OIDC based authentication add or modify the following variables in your `.env` file:
41
42 ```bash
43 # Set OIDC to be the authentication method
44 AUTH_METHOD=oidc
45
46 # Control if BookStack automatically initiates login via your OIDC system 
47 # if it's the only authentication method. Prevents the need for the
48 # user to click the "Login with x" button on the login page.
49 # Setting this to true enables auto-initiation.
50 AUTH_AUTO_INITIATE=false
51
52 # Set the display name to be shown on the login button.
53 # (Login with <name>)
54 OIDC_NAME=SSO
55
56 # Name of the claims(s) to use for the user's display name.
57 # Can have multiple attributes listed, separated with a '|' in which 
58 # case those values will be joined with a space.
59 # Example: OIDC_DISPLAY_NAME_CLAIMS=given_name|family_name
60 OIDC_DISPLAY_NAME_CLAIMS=name
61
62 # OAuth Client ID to access the identity provider
63 OIDC_CLIENT_ID=abc123
64
65 # OAuth Client Secret to access the identity provider
66 OIDC_CLIENT_SECRET=def456
67
68 # Issuer URL
69 # Must start with 'https://'
70 OIDC_ISSUER=https://instance.authsystem.example.com
71
72 # The "end session" (RP-initiated logout) URL to call during BookStack logout.
73 # By default this is false which disables RP-initiated logout.
74 # Setting to "true" will enable logout if found as supported by auto-discovery.
75 # Otherwise, this can be set as a specific URL endpoint.
76 OIDC_END_SESSION_ENDPOINT=false
77
78 # Enable auto-discovery of endpoints and token keys.
79 # As per the standard, expects the service to serve a 
80 # `<issuer>/.well-known/openid-configuration` endpoint.
81 OIDC_ISSUER_DISCOVER=true
82
83 ############################################################
84 ## NOTE: The below are only needed if not using the above ##
85 ##       auto-discovery option.                           ##
86 ############################################################
87
88 # Path to identity provider token signing public RSA key
89 OIDC_PUBLIC_KEY=file:///keys/idp-public-key.pem
90
91 # Full URL to the OIDC authorize endpoint
92 OIDC_AUTH_ENDPOINT=https://instance.authsystem.example.com/v1/authorize
93
94 # Full URL to the OIDC token endpoint
95 OIDC_TOKEN_ENDPOINT=https://instance.authsystem.example.com/v1/token
96
97 # Full URL to the OIDC userinfo endpoint
98 # Won't be used if all required claims are provided in the ID token.
99 OIDC_USERINFO_ENDPOINT=https://instance.authsystem.example.com/v1/userinfo
100 ```
101
102 A user in BookStack will be linked to an OIDC provided account via the `sub` claim.
103 If the value of this ID changes in the identity provider it can be updated in BookStack, 
104 by an admin, by changing the "External Authentication ID" field on the user's profile.
105
106 ### Authentication System Configuration
107
108 With OIDC you don't often need to configure much specifically for BookStack.
109 Most often, you'll just need to ensure any callback/redirect URIs are set as below:
110
111 - **Login Callback/Redirect URI:** `https://example.com/oidc/callback`
112 - **Logout Callback/Redirect URIs:**
113   - `https://example.com`
114   - `https://example.com/login`
115   - `https://example.com/login?prevent_auto_init=true`
116   - *Only one URL will actually be used but it depends upon specific configuration set. Some systems will allow you to instead use a wildcard like `https://example.com/*`.*
117
118 Change `https://example.com` to be the base URL of your BookStack instance.
119
120 Since v24.02 BookStack will make use of Proof Key for Code Exchange (PKCE) during authentication. 
121 If your authentication system provides the option, you should enforce PKCE to be required for extra security.
122
123 ### Switching to OIDC with Existing Users
124
125 When switching `AUTH_METHOD` from `standard` to `oidc`, BookStack will not 
126 link OIDC user accounts to existing BookStack users, where the email address is 
127 matching, since the "External Authentication ID" value of the existing BookStack user does 
128 not match the unique user ID provided by the OIDC system.
129
130 You can overcome this situation by logging into BookStack with an admin account while `AUTH_METHOD=standard`.
131 While logged in, change `AUTH_METHOD` to `oidc`.
132 This change of authentication method will show an "External Authentication ID" text
133 field, below the name and email inputs, when viewing a user account in BookStack.
134 Here you can enter the unique user ID that would be provided by your OIDC provider.
135 Once saved BookStack will then use this value to match OIDC and BookStack user 
136 accounts upon next login attempt.
137
138 If you need to update accounts in bulk, you could instead directly update the 
139 `external_auth_id` field of the `users` table within your BookStack database.
140
141 ### Debugging
142
143 To help when setting up or configuring BookStack to use your OIDC system, the below
144 `.env` option can help provide more insight:
145
146 ```bash
147 # Dump out the details fetched from the identity provider.
148 # Only set this option to true if debugging since it will block logins
149 # and potentially show private details.
150 OIDC_DUMP_USER_DETAILS=false
151 ```
152
153 Further to this, details of any BookStack errors encountered can be found by following
154 our [general debugging documentation](/docs/admin/debugging/).
155
156 ### Using a Different ID Claim
157
158 By default, BookStack will use the `sub` claim as a unique identifier to match up a user
159 between BookStack and the identify provider.
160 For the vast majority of use-cases, this is fine since this claim is part of the 
161 OIDC standard.
162
163 In some very select scenarios, you may want to use a different claim as the unique identifier.
164 This can be done by setting an `OIDC_EXTERNAL_ID_CLAIM` option in your `.env` like shown below,
165 where the value of the option is the name of the claim:
166
167 ```bash
168 # Configure a custom ID Token claim to be used as the
169 # "External Authentication ID" within BookStack.
170 OIDC_EXTERNAL_ID_CLAIM=upn
171 ```
172
173 Note that changing this with existing BookStack OIDC users, without changing their "External Authentication ID" values,
174 may cause issues upon future login since their existing store ID in BookStack may no longer align.
175
176 ### Group Sync
177
178 BookStack has the ability to sync OIDC user groups with BookStack roles.
179 By default this will match OIDC group names with the BookStack role display names with casing ignored.
180 This can be overridden by via the 'External Authentication IDs' field which can be seen when editing a role while OIDC authentication is enabled.
181 If filled, the names in this field will be used and the BookStack role display name will be ignored.
182 You can match on multiple names by separating them with a comma.
183 Commas can be escaped with a backslash (`\,`) if you need to map using a literal comma character.
184
185 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.
186
187 This feature requires the OIDC server to provide a claim in the ID token with an array of group names.
188 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).
189
190 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.
191
192 Here are the settings required to be added to your `.env` file to enable group syncing:
193
194 ```bash
195 # Enable OIDC group sync.
196 OIDC_USER_TO_GROUPS=true
197
198 # Set the attribute from which BookStack will read groups names from.
199 OIDC_GROUPS_CLAIM=groups
200
201 # Additional scopes to send with the authentication request.
202 # By default BookStack only sends the 'openid', 'profile' & 'email' scopes.
203 # Many platforms require specific scopes to be requested for group data.
204 # Multiple scopes can be added via comma separation.
205 OIDC_ADDITIONAL_SCOPES=groups
206
207 # Remove the user from roles that don't match OIDC groups upon login.
208 # Note: While this is enabled the "Default Registration Role", editable within the 
209 # BookStack settings view, will be considered a matched role and assigned to the user.
210 OIDC_REMOVE_FROM_GROUPS=true
211 ```
212
213 #### Nested Groups Claim Example
214
215 The below shows a reduced example of JSON data for an ID token, that has group data within a nested property,
216 along with the `OIDC_GROUPS_CLAIM` value that would be used for this structure to detect the provided "Editor" and "Admin" roles.
217
218 ```bash
219 OIDC_GROUPS_CLAIM=resource_access.bookstack.roles
220 ```
221
222 ```json
223 {
224   ...
225   "resource_access": {
226     "bookstack": {
227       "roles": [
228         "Editor",
229         "Admin"
230       ]
231     }
232   },
233   "email": "[email protected]"
234   ...
235 }
236 ```