> ## Documentation Index
> Fetch the complete documentation index at: https://doc.lucidworks.com/llms.txt
> Use this file to discover all available pages before exploring further.

# OAuth

export const LwTemplate = ({title = "Key questions to get you started", icon = "sparkles", cta = "Powered by Agent Studio", linkHref = "https://lucidworks.com/demo/?utm_source=docs&utm_medium=referral&utm_campaign=docs_cta_ai"}) => {
  const [isLoaded, setIsLoaded] = useState(false);
  useEffect(() => {
    const timer = setTimeout(() => {
      setIsLoaded(true);
    }, 500);
    return () => clearTimeout(timer);
  }, []);
  return <div className="lw-template-container">
      <Card title={title} icon={icon}>
        {isLoaded && <span dangerouslySetInnerHTML={{
    __html: `<lw-template id="a029c1a9-28be-427e-b0e1-5d918920246a"></lw-template
            >`
  }} />}
        <Link href={linkHref} className="agent-studio-link text-left text-gray-600 gap-2 dark:text-gray-400 text-sm font-medium flex flex-row items-center hover:text-primary dark:hover:text-primary-light group-hover:text-primary group-hover:dark:text-primary-light">Powered by Lucidworks Agent Studio</Link>
      </Card>
    </div>;
};

[old doc.lw link]: https//doc.lucidworks.com/app-studio/4.2/3163

[localhost link]: http://localhost:3000/docs/5/app-studio/reference/modules/security/authentication/oauth/overview

[mintlify link]: https://doc.lucidworks.com/docs/5/app-studio/reference/modules/security/authentication/oauth/overview

Authentication against OAuth 2.0 services is possible using the individual authentication provider modules which simplify the configuration of OAuth parameters and deal with the differences that exist between common OAuth endpoints.

A general OAuth provider can be used by including the `twigkit.security.provider.oauth` module. This requires a custom `spring-security.xml` file, but it is recommended to use one of the prepackaged modules we provide for these services.

Using OAuth can create a seamless experience where, after the token handshake and approval is complete, the user will always be logged into the application as long as their session is active.

<LwTemplate />

## ADFS

The Active Directory Federation Services (ADFS) security provider is available in Appkit.

<img src="https://mintcdn.com/lucidworks/J_LymSfRoWq3UOvg/assets/images/appkit/4.2/adfs-login-screen.png?fit=max&auto=format&n=J_LymSfRoWq3UOvg&q=85&s=2b78f728fa6075eb35bf16308272e66f" alt="ADFS login screen" width="1610" height="1102" data-path="assets/images/appkit/4.2/adfs-login-screen.png" />

<Note>
  OAuth based authentication against ADFS requires **ADFS 3** (or newer) which is available from Windows Server 2012 R2 onwards.
</Note>

**Set Up Active Directory Federation Services (ADFS) Authentication for App Studio or Appkit**

<Accordion title="Set Up Active Directory Federation Services (ADFS) Authentication for App Studio or Appkit">
  {/* // Update how-to header information */}

  {/* // formatted */}

  The Active Directory Federation Services (ADFS) security provider is available in Appkit.

  <Note>OAuth based authentication against ADFS requires **ADFS 3** (or newer) which is available from Windows Server 2012 R2 onwards.</Note>

  To authenticate against ADFS, perform the steps in this article.

  ## 1 Add the security provider dependency

  To add ADFS as a security provider in the Oauth Security module:

  1. Remove any existing security provider dependency from the `pom.xml` under the root of the project.
  2. Add a security provider dependency for ADFS to the `dependencies` tag in `pom.xml`:

     ```
     <dependency>
         <groupId>twigkit</groupId>
         <artifactId>twigkit.security.provider.oauth.adfs3</artifactId>
         <version>${project.parent.version}</version>
     </dependency>
     ```
  3. Configure Appkit to invoke the Oauth Security module on startup. Change the `security.conf` file in `src/main/resources/conf/security/` to contain:

     ```
     type: oauth
     ```

     <Note>   You must remove any existing `spring-security.xml` file because this module encapsulates all Spring configuration automatically.</Note>

  ## 2 Set up the application (relying party trust) in ADFS

  <img src="https://mintcdn.com/lucidworks/J_LymSfRoWq3UOvg/assets/images/appkit/4.2/adfs-relying-party-1.png?fit=max&auto=format&n=J_LymSfRoWq3UOvg&q=85&s=f0e5727c16ea809e8240f37a8e75b789" alt="adfs-relying-party-1" width="1460" height="1178" data-path="assets/images/appkit/4.2/adfs-relying-party-1.png" />

  <img src="https://mintcdn.com/lucidworks/J_LymSfRoWq3UOvg/assets/images/appkit/4.2/adfs-relying-party-2.png?fit=max&auto=format&n=J_LymSfRoWq3UOvg&q=85&s=03576bb0e64ccab4c4c743e86093f690" alt="adfs-relying-party-2" width="1460" height="1178" data-path="assets/images/appkit/4.2/adfs-relying-party-2.png" />

  1. Add a new relying party in ADFS management. The identifier is usually set to the URL of the Appkit application itself.

       <img src="https://mintcdn.com/lucidworks/J_LymSfRoWq3UOvg/assets/images/appkit/4.2/adfs-relying-party-3.png?fit=max&auto=format&n=J_LymSfRoWq3UOvg&q=85&s=a8ebc8d274ea547d2a8016b8ff6821af" alt="adfs-relying-party-3" width="1460" height="1178" data-path="assets/images/appkit/4.2/adfs-relying-party-3.png" />

       <img src="https://mintcdn.com/lucidworks/J_LymSfRoWq3UOvg/assets/images/appkit/4.2/adfs-relying-party-4.png?fit=max&auto=format&n=J_LymSfRoWq3UOvg&q=85&s=c8f1fddb88c5159bcd1ecbaa96ad66ae" alt="adfs-relying-party-4" width="2462" height="1002" data-path="assets/images/appkit/4.2/adfs-relying-party-4.png" />
  2. Edit the Claim Rules to pass through the credentials as shown in the screenshot. It is important the configuration is as shown in the screenshots here. The User Principal Name is passed through as the 'UPN' claim and the Token-Groups Unqualified Name is passed through as the 'Role' claim.

       <img src="https://mintcdn.com/lucidworks/J_LymSfRoWq3UOvg/assets/images/appkit/4.2/adfs-principal-roles-claim-rule.png?fit=max&auto=format&n=J_LymSfRoWq3UOvg&q=85&s=31a14b30e72551294225529451661983" alt="adfs-principal-roles-claim-rule" width="1126" height="1194" data-path="assets/images/appkit/4.2/adfs-principal-roles-claim-rule.png" />
  3. Open a PowerShell session to create the OAuth ADFS 'client' for the Appkit application:

     ```
     Add-ADFSClient -Name "Appkit OAuth" -ClientId "1234567890-ABCDEF" -RedirectUri="http://localhost:8080/oauthLogin"
     ```

  The `ClientId` should be a GUID and the `RedirectUri` must point to the Appkit application URL, with `/oauthLogin` appended. Here, `localhost` is used for testing an Appkit application running on the local development machine.

  ## 3 Configure the OAuth module for the application setup in ADFS

  Create a new configuration file in `conf/security/oauth.conf`.

  ### `oauth.conf`

  ```yaml theme={"dark"}
  client-id: 168f3ee4-63fc-4723-a61a-6473f6cb515d
  adfs-url: https://your-adfs-server/adfs
  resource: http://localhost:8080
  high-trust: false
  ```

  Here, you must change the `client-id`, `adfs-url` and `resource` parameters for the environment in question.

  The `client-id` is the ID that was set up against ADFS using the `Add-ADFSClient` PowerShell command.

  The `adfs-url` is the URL of the ADFS server with the `/adfs` context appended.

  The `resource` is the relying party trust identifier set up in ADFS management.

  The \`high-trust' parameter is only required when integrating the ADFS and SharePoint modules. This article is only concerned with authentication against ADFS.

  ## 3 Add the Spring filter to the web.xml

  Add this to the `web.xml` file of the project:

  ```xml theme={"dark"}
  <!-- Spring Security -->
      <context-param>
          <param-name>contextConfigLocation</param-name>
          <param-value>classpath:spring-security.xml</param-value>
      </context-param>

      <listener>
          <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
      </listener>

      <filter>
          <filter-name>springSecurityFilterChain</filter-name>
          <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
      </filter>

      <filter-mapping>
          <filter-name>springSecurityFilterChain</filter-name>
          <url-pattern>/*</url-pattern>
      </filter-mapping>

      <listener>
        <listener-class>
          org.springframework.web.context.request.RequestContextListener
        </listener-class>
      </listener>
  <!-- Spring Security Ends -->
  ```

  Inclusion of a 'RequestContextListener' is not required in a standard Appkit-plus-Spring Security application.

  ## 4 Test the authentication

  If a user is not logged via ADFS prior to visiting the application they will be redirected to the ADFS OAuth login page. The user experience is a typical login page.

  After the user logs in, the user will be returned to the application as an authenticated user.

  The Appkit user’s details will also be populated with any basic information available from the decoded OAuth token such as roles and the user principal name.
</Accordion>

## Facebook

**Set Up Facebook Authentication**

<Accordion title="Set Up Facebook Authentication">
  To authenticate against Facebook, perform the steps in this article.

  ## Add the security provider dependency

  To add Facebook as a security provider in the Oauth Security module:

  1. Remove any existing security provider dependency from the `pom.xml` under the root of the project.
  2. Add a security provider dependency for Facebook to the `dependencies` tag in `pom.xml`:
     ```xml theme={"dark"}
     <dependency>
         <groupId>twigkit</groupId>
         <artifactId>twigkit.security.provider.oauth.facebook</artifactId>
         <version>${project.parent.version}</version>
     </dependency>
     ```
  3. Configure Appkit to invoke the Oauth Security module on startup. Change the `security.conf` file in `src/main/resources/conf/security/` to contain:

     ```
     type: oauth
     ```

     <Note>   You must remove any existing `spring-security.xml` file because this module encapsulates all Spring configuration automatically.</Note>

  ## Configure the OAuth module for the application setup in Facebook

  Add the relevant configuration in a file in `conf/security/oauth.conf`. For example:

  ```yaml theme={"dark"}
  client-id: 1669796236622670
  client-secret: c064ea0f5e5c13a2a6c5f6366f7426cb
  scope: public_profile,email
  ```

  Including the user profile scope lets Appkit automatically pull the user profile information into their Appkit user details.

  This guide assumes the `client-id` and `client-secret` settings have already been generated in the 'Facebook for Developers' admin console and provided to you as an application developer. For more information, see the [Facebook documentation on OAuth](https://developers.facebook.com/docs/facebook-login/guides/advanced/manual-flow).

  <Note>The application’s 'Site URL' must be configured in the console under 'settings' as `http://your-application-url/oauthLogin`.</Note>

  ## Add the Spring filter to the web.xml

  Add this to the `web.xml` file of the project:

  ```xml theme={"dark"}
  <!-- Spring Security -->
      <context-param>
          <param-name>contextConfigLocation</param-name>
          <param-value>classpath:spring-security.xml</param-value>
      </context-param>

      <listener>
          <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
      </listener>

      <filter>
          <filter-name>springSecurityFilterChain</filter-name>
          <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
      </filter>

      <filter-mapping>
          <filter-name>springSecurityFilterChain</filter-name>
          <url-pattern>/*</url-pattern>
      </filter-mapping>

      <listener>
        <listener-class>
          org.springframework.web.context.request.RequestContextListener
        </listener-class>
      </listener>
  <!-- Spring Security Ends -->
  ```

  Inclusion of a 'RequestContextListener' is not required in a standard Appkit-plus-Spring Security application.

  ## Test the authentication

  If a user is not logged into a Facebook account prior to visiting the application they will be redirected to the Facebook login page. They will then be asked if they want to approve the application to access the services defined in the scopes parameter. If the user profile scope is included the Appkit user’s details will also be populated with any basic information available from the decoded OAuth token such as first name, last name and email address.

  Using OAuth can create a seamless experience where, after the token handshake and approval is complete, the user will always be logged into the application as long as their session with Facebook is active.

  User logs in:

  <img src="https://mintcdn.com/lucidworks/J_LymSfRoWq3UOvg/assets/images/appkit/4.2/facebook-login.png?fit=max&auto=format&n=J_LymSfRoWq3UOvg&q=85&s=4d353725e9d73bc13492cde0de955b73" alt="Facebook account login screen example" width="873" height="561" data-path="assets/images/appkit/4.2/facebook-login.png" />

  User approves application:

  <img src="https://mintcdn.com/lucidworks/J_LymSfRoWq3UOvg/assets/images/appkit/4.2/facebook-approval.png?fit=max&auto=format&n=J_LymSfRoWq3UOvg&q=85&s=3c2bc9a7e16de5de8d6637a6e8882e9b" alt="Facebook approval screen example" width="675" height="584" data-path="assets/images/appkit/4.2/facebook-approval.png" />

  The user is signed into the application with an OAuth token, and will be signed in automatically from now on unless the token expires or the user logs out of Facebook entirely.

  <img src="https://mintcdn.com/lucidworks/J_LymSfRoWq3UOvg/assets/images/appkit/4.2/facebook-app.png?fit=max&auto=format&n=J_LymSfRoWq3UOvg&q=85&s=2f01d6ef7006614d8a97f76aa347cb97" alt="Facebook application example" width="1280" height="647" data-path="assets/images/appkit/4.2/facebook-app.png" />
</Accordion>

## Google

**Set Up Google Authentication**

<Accordion title="Set Up Google Authentication">
  To authenticate against Google services, perform the steps in this article.

  ## 1 Add the security provider dependency

  To add Google as a security provider in the Oauth Security module:

  1. Remove any existing security provider dependency from the `pom.xml` under the root of the project.
  2. Add a security provider dependency for Google to the `dependencies` tag in `pom.xml`:
     ```xml theme={"dark"}
     <dependency>
         <groupId>twigkit</groupId>
         <artifactId>twigkit.security.provider.oauth.google</artifactId>
         <version>${project.parent.version}</version>
     </dependency>
     ```
  3. Configure Appkit to invoke this module on startup. Change the `security.conf` file in `src/main/resources/conf/security/` to contain:
     ```
     type: oauth
     ```
  4. Remove any existing `spring-security.xml` file, because this module encapsulates all Spring configuration automatically.

  ## 2 Configure the OAuth module for application setup in Google Developer Console

  Add the relevant configuration in a file in `conf/security/oauth.conf`:

  ```yaml wrap theme={"dark"}
  client-id: 419084461435-2370lvl0rpcr8b8lu0ljkv3l2ib2ahfc.apps.googleusercontent.com
  client-secret: 5p-Ph7AcvnFBfMQSSUNv-umQ
  scope: https://www.googleapis.com/auth/userinfo.profile
  ```

  Including the user profile scope lets Appkit automatically pull the user profile information into their Appkit user details.

  This guide assumes the `client-id` and `client-secret` settings have already been generated in the Google Developer Console and provided to you as an application developer. For more information about this, see the [Google documentation on OAuth](https://developers.google.com/identity/protocols/OAuth2).

  <Note>The authorized redirect URI must be configured in the Google Developer Console as `http://your-application-url/oauthLogin`.</Note>

  ## 3 Add the Spring filter to the web.xml

  Add this to the `web.xml` of the project:

  ```xml theme={"dark"}
  <!-- Spring Security -->
      <context-param>
          <param-name>contextConfigLocation</param-name>
          <param-value>classpath:spring-security.xml</param-value>
      </context-param>

      <listener>
          <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
      </listener>

      <filter>
          <filter-name>springSecurityFilterChain</filter-name>
          <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
      </filter>

      <filter-mapping>
          <filter-name>springSecurityFilterChain</filter-name>
          <url-pattern>/*</url-pattern>
      </filter-mapping>

      <listener>
        <listener-class>
          org.springframework.web.context.request.RequestContextListener
        </listener-class>
      </listener>
  <!-- Spring Security Ends -->
  ```

  Inclusion of a 'RequestContextListener' is not required in a standard Appkit-plus-Spring Security application.

  ## 4 Test the authentication

  If a user is not logged into a Google account prior to visiting the application they will be redirected to the Google login page. They will then be asked if they want to approve the application to access the services defined in the scopes parameter. If the user profile scope is included the Appkit user’s details will also be populated with any basic information available from the decoded OAuth token such as first name, last name and email address.

  Using OAuth can create a seamless experience where, after the token handshake and approval is complete, the user will always be logged into the application as long as their session with Google is active.

  User logs in:

  <img src="https://mintcdn.com/lucidworks/J_LymSfRoWq3UOvg/assets/images/appkit/4.2/google-login.png?fit=max&auto=format&n=J_LymSfRoWq3UOvg&q=85&s=d15fb8210c6601973f845c077bc4fec5" alt="Google account login screen example" width="378" height="489" data-path="assets/images/appkit/4.2/google-login.png" />

  User approves application:

  <img src="https://mintcdn.com/lucidworks/J_LymSfRoWq3UOvg/assets/images/appkit/4.2/google-approval.png?fit=max&auto=format&n=J_LymSfRoWq3UOvg&q=85&s=9eb4b16c53c6769b40792fd8257dc026" alt="Google approval screen example" width="1155" height="503" data-path="assets/images/appkit/4.2/google-approval.png" />

  The user is signed into the application with an OAuth token, and will be signed in automatically from now on unless the token expires or the user logs out of Google entirely.

  <img src="https://mintcdn.com/lucidworks/J_LymSfRoWq3UOvg/assets/images/appkit/4.2/google-app.png?fit=max&auto=format&n=J_LymSfRoWq3UOvg&q=85&s=d75aacc7dc3d6349e7411217727ba86f" alt="Google application example" width="1399" height="677" data-path="assets/images/appkit/4.2/google-app.png" />
</Accordion>

## Office 365

**Set Up Office 365 Authentication**

<Accordion title="Set Up Office 365 Authentication">
  To authenticate against Office 365, perform the steps in this article.

  ## 1 Add the security provider dependency

  To add Office 365 as a security provider in the Oauth Security module:

  1. Remove any existing security provider dependency from the `pom.xml` under the root of the project.
  2. Add a security provider dependency for Office 365 to the `dependencies` tag in `pom.xml`:
     ```xml theme={"dark"}
     <dependency>
         <groupId>twigkit</groupId>
         <artifactId>twigkit.security.provider.oauth.office365</artifactId>
         <version>${project.parent.version}</version>
     </dependency>
     ```
  3. Configure Appkit to invoke the Oauth Security module on startup. Change the `security.conf` file in `src/main/resources/conf/security/` to contain:

     ```
     type: oauth
     ```

     <Note>   You must remove any existing `spring-security.xml` file because this module encapsulates all Spring configuration automatically.</Note>

  ## 2 Configure the OAuth module for the application setup in Azure AD

  Add the relevant configuration in a file in `conf/security/oauth.conf`:

  ```yaml theme={"dark"}
  azure-ad-tenant-id: 746a834e-ba89-4191-926d-f2c220b79a4a
  resource: https://your-organisation.sharepoint.com/
  client-id: 08de5b32-569a-4387-ab94-08b7b6fc1ed8
  client-secret: x7vRc8VJ9CHjSM5XXyHpCSRaD5JH/6VsB8P5ZGz4B8M=
  ```

  This guide assumes these settings have already been set up in the Azure AD instance in Microsoft’s Azure portal and provided to you as an application developer.

  <Note>The reply URL must be configured in Azure AD as `http://your-application-url/oauthLogin`.</Note>

  ## 3 Add the Spring filter to the web.xml file

  Add this to the `web.xml` file of the project:

  ```xml theme={"dark"}
  <!-- Spring Security -->
      <context-param>
          <param-name>contextConfigLocation</param-name>
          <param-value>classpath:spring-security.xml</param-value>
      </context-param>

      <listener>
          <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
      </listener>

      <filter>
          <filter-name>springSecurityFilterChain</filter-name>
          <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
      </filter>

      <filter-mapping>
          <filter-name>springSecurityFilterChain</filter-name>
          <url-pattern>/*</url-pattern>
      </filter-mapping>

      <listener>
        <listener-class>
          org.springframework.web.context.request.RequestContextListener
        </listener-class>
      </listener>
  <!-- Spring Security Ends -->
  ```

  Inclusion of a 'RequestContextListener' is not required in a standard Appkit-plus-Spring Security application.

  ## 4 Test the authentication

  If a user is not logged into Office 365 prior to visiting the application, then they will be redirected to the Microsoft Online login page.

  If the Azure administrator has set app approval explicitly, after the user is logged in, the user will be returned to the application as an authenticated user.

  The Appkit user’s details will be populated with any basic information available from the decoded OAuth token such as first name, last name and email address.

  If approval is not set by the Azure AD administrator, then there will be an intermediary step after the user logs into Microsoft Online requesting their explicit approval for the application to access their credentials.

  Using OAuth can create a seamless experience where, after the token handshake and approval is complete, the user will always be logged into the application as long as their session with Microsoft Online/Office 365 is active.

  User logs in:

  <img src="https://mintcdn.com/lucidworks/J_LymSfRoWq3UOvg/assets/images/appkit/4.2/microsoft-online-login.png?fit=max&auto=format&n=J_LymSfRoWq3UOvg&q=85&s=5834a534fe81ce17a614198a41732ba3" alt="Microsoft Online login screen example" width="702" height="374" data-path="assets/images/appkit/4.2/microsoft-online-login.png" />

  User approves application:

  <img src="https://mintcdn.com/lucidworks/J_LymSfRoWq3UOvg/assets/images/appkit/4.2/microsoft-online-approval.png?fit=max&auto=format&n=J_LymSfRoWq3UOvg&q=85&s=5873a3edb3842c42ee37c9b5de90f2b8" alt="Microsoft Online approval example" width="676" height="343" data-path="assets/images/appkit/4.2/microsoft-online-approval.png" />

  (This is optional, depending on the Azure AD configuration.)

  The user is signed into the application with an OAuth token, and will be signed in automatically from now on unless the token expires or the user logs out of Office 365 entirely.

  <img src="https://mintcdn.com/lucidworks/J_LymSfRoWq3UOvg/assets/images/appkit/4.2/office-365-app.png?fit=max&auto=format&n=J_LymSfRoWq3UOvg&q=85&s=6dd0f72ffb1344159c518986d467cd74" alt="Office 365 application example" width="955" height="383" data-path="assets/images/appkit/4.2/office-365-app.png" />
</Accordion>
