# SharePoint: an app with a certificate

With **Application Permission (cert based)**, a [SharePoint](VFS-SharePoint.md) item acts as the app itself and proves it with a certificate. The private key stays on your server and is never sent to Microsoft; only a signature made with it is. This is the form Microsoft recommends for servers, and the one to use in production.

## A certificate

Any certificate with an RSA key will do; it does not have to come from a public authority. Make one for this purpose only, for instance with OpenSSL:

```
openssl req -x509 -newkey rsa:2048 -nodes -days 730 -subj "/CN=CrushMFT SharePoint" -keyout sharepoint.key -out sharepoint.cer
```

You get two files:

| File | For |
|---|---|
| `sharepoint.cer` | the certificate, public. It is uploaded to Microsoft. |
| `sharepoint.key` | the private key, in the text form that begins with `-----BEGIN PRIVATE KEY-----`. It goes into the item, and nowhere else. |

## At Microsoft

1. Register an app: see [The app registration](VFS-SharePoint.md#the-app-registration-for-all-three). No redirect URI is needed.
2. Give it an **application** permission of Microsoft Graph and grant admin consent. Prefer `Sites.Selected` with a grant per site: see [Only some sites](VFS-SharePoint-App-Secret.md#only-some-sites).
3. Under **Certificates & secrets**, on the tab **Certificates**, **Upload certificate** and choose `sharepoint.cer`.
4. Copy the **Thumbprint** that is then shown beside it.

## In the item

1. Choose the item type **SharePoint** and the choice **Application Permission (cert based)**.
2. A window, "Please enter Sharepoint Oauth (Cert Based)", asks for:

   | Field | Enter |
   |---|---|
   | **Client ID** | the application (client) ID |
   | **Thumbprint** | the thumbprint from the portal |
   | **Private Key** | the whole text of `sharepoint.key`, with its first and last line |
   | **Tenant** | the directory (tenant) ID |

3. The item is filled in: the **User name** is `app_permission_cert~`, the client ID, `~` and the thumbprint.
4. Say where: paste a **Shared Link**, or fill in the site and library. See [Where in SharePoint](VFS-SharePoint.md#where-in-sharepoint).
5. Press **Test**.

The private key is stored with the item, encrypted like the item's address. After that, delete the key file from wherever you made it, or lock it away.

## The certificate expires

On the day the certificate ends, the item stops working.

1. Some weeks before, make a new certificate.
2. Upload it to the app registration **beside** the old one. Both are accepted.
3. In the item, choose **Application Permission (cert based)** again and enter the new thumbprint and key.
4. When everything runs on the new one, remove the old certificate from the registration.

Done in this order, there is no moment without access.

## When it does not work

| What you see | Usually |
|---|---|
| "AADSTS700027: client assertion contains an invalid signature" | the private key does not belong to the uploaded certificate; or the thumbprint is of another certificate |
| the key is not accepted | it is not in the form `-----BEGIN PRIVATE KEY-----` (PKCS#8). Convert it: `openssl pkcs8 -topk8 -nocrypt -in old.key -out sharepoint.key` |
| "access denied" | no admin consent; no grant on the site with `Sites.Selected` |
| it stopped working | the certificate expired |
