# Access Control Plugin for Unity

This Unity plugin provides methods to ensure users are authorized to run the current application and thereby prevents unauthorized access.

## Compatibility

The Access Control Plugin for Unity currently supports the following Unity versions:

-   2019.4.\*

Other versions may work but are not thoroughly tested.

Additionally, when building the projects targeting Windows, Api Compatibility Level needs to be set to .Net 4.x.

## Installation

Follow the steps below to integrate this plugin in your Unity project:

1. Download the latest release as a `.unitypackage` .
1. Open your Unity project
1. Double-click on the downloaded `.unitypackage` to import the package into Unity
1. Click "Import" to import all files into your project

## Quick setup

### Windows

1. Set the `InnoactiveAccessControlScene` as the first scene in the projects build settings (index 0) to ensure the checks are always run first.
1. Obtain a set of OAuth2 client credentials from your administrator
1. Set your OAuth2 Client-ID and Backend URL (the url of your portal instance) in the `Innoactive-Access-Control/Resources/OAuth2ClientSettings` .

### Android

1. Set the `InnoactiveAccessControlScene` as the first scene in the projects build settings (index 0) to ensure the checks are always run first.
1. Enable `Custom Main Gradle Template` by navigating to Project Settings > Player > Publishing Settings
1. Add the following dependencies to the `mainTemplate.gradle` file
```
implementation 'io.jsonwebtoken:jjwt-api:0.11.2'
implementation 'io.jsonwebtoken:jjwt-impl:0.11.2'
implementation('io.jsonwebtoken:jjwt-orgjson:0.11.2') {
    exclude group: 'org.json', module: 'json' //provided by Android natively
}
implementation 'org.bouncycastle:bcprov-jdk15on:1.66'
implementation "org.jetbrains.kotlin:kotlin-stdlib:1.5.20"
```

### Unity Editor

No setup needed, please note that authorization in Unity Editor will always succeed in order to not impact software development.

## Customize the authorization scene

The _InnoactiveAccessControlScene_ can be copied and changed as desired.

The current implementation provides a very simple visualization of the authorization being in progress and redirects to the next scene in the list on successful authorization.

This simple scene can (and usually should) be customized to match your application's look & feel to provide a consistent user experience throughout the application.

## Components

### AuthorizationManager

The `AuthorizationManager` component takes care of the authorization procedure. It provides two events:

1. `OnAuthorizationSuccessful` is called when the authorization process has been finished successfully and the user is indeed allowed to start the application
1. `OnAuthorizationFailed` is called if any step in the authorization process failed and can be used to define specific behaviour upon failed authorization

### DisplayErrorOnAuthorizationFailed

The `DisplayErrorOnAuthorizationFailed` component listens to the managers `OnAuthorizationFailed` event and shows a respective message to the user using UnityEngine's UI components.

### LoadSceneOnAuthorizationSuccess

The `LoadSceneOnAuthorizationSuccess` component listens to the managers `OnAuthorizationSuccessful` event and loads the scene with the specified index (defaults to the second scene in the list of scenes, see your BuildSettings).
