# Testing SSO and Local Authentication

## 1. Apply the database migration

```bash
flask db upgrade
```

The migration adds `users.auth_method` and `users.login_id`.

## 2. Install the new dependencies

```bash
python -m pip install -r requirements.txt
```

New direct dependencies: `PyJWT` and `requests`.

## 3. Local login test

In Employee Master create/edit a user and choose **Local ID & password**.

- Login ID: e.g. `VENDORADMIN01` or leave blank to default to Employee ID
- Initial password: minimum 8 characters
- Keep employee Active and Eligible for portal
- Assign the required role(s)

Open `/auth/login`, use the Local ID & password panel, and verify role-based landing/menu access.

A Local user can change their own password from **My Profile**.

## 4. SSO user test before Entra configuration

Create/edit a user and choose **Piramal SSO**. The account must use the employee's exact official email registered in Microsoft Entra.

Until `.env` contains the Entra settings, the SSO button will show configuration pending.

## 5. Configure Microsoft Entra

Set:

```text
SSO_ENABLED=true
SSO_TENANT_ID=<tenant-guid>
SSO_CLIENT_ID=<client-id>
SSO_CLIENT_SECRET=<client-secret-value>
SSO_REDIRECT_URI=https://sevasetu.chlsoftech.com/auth/sso/callback
SSO_SCOPES=openid profile email
```

Restart the application after changing environment variables.

## 6. SSO test

1. Open `/auth/login`.
2. Click **Continue with Piramal SSO**.
3. Sign in with the same official email configured in Employee Master.
4. On first success, Seva Setu binds the Entra object ID to that user.
5. Confirm the role-wise landing page and menus.

Negative tests:

- Entra-valid email not present in Employee Master -> access denied.
- Employee inactive -> access denied.
- Employee not eligible for portal -> access denied.
- Employee configured as Local -> SSO denied.
- SSO user tries local credentials -> local login denied.

## 7. Changing authentication method

- **Local -> SSO:** local Login ID/password are cleared and the next successful SSO login binds the Entra identity.
- **SSO -> Local:** SSO binding is cleared. Admin must set a Local Login ID and password.
- **Reset SSO binding:** use only when an employee's Entra identity has changed; the next SSO login will bind again by official email.
