# Install M7 Identity SDK for CLI and background processes

<!-- m7-identity-discovery:begin -->
## Connect to M7 Identity

Issuer: <https://sso.user.m7.org>

Discovery JSON: <https://sso.user.m7.org/.well-known/openid-configuration>

- [Discovery and signing keys](https://m7.org/docs/api/sso.user.m7.org/discovery-and-keys.md)
- [Integration quickstart](https://m7.org/docs/api/sso.user.m7.org/quickstart.md)
- [Provider profile](https://m7.org/docs/api/sso.user.m7.org/provider-profile.md)
- [M7 Identity SDK and current downloads](https://m7.org/docs/sdk/m7-identity/)

Live discovery supplies endpoint locations and advertised capabilities. Use the
provider profile for the integration contract and current testing status.
<!-- m7-identity-discovery:end -->

The prepared Token/PHP 0.1.5-rc.1 candidate adds receiver-authenticated organization
lookup, ACL member management and canonical org principal references. The verified
public download remains 0.1.4. See [candidate support status](https://m7.org/docs/sdk/m7-identity/README.md#prepared-tokenphp-update)
before selecting those additions.

The download commands below install the current Token/PHP 0.1.4 and CLI/PHP
0.1.3 releases. M7 C Crypto and the compiled PHP extension are optional; see
[native runtime requirements](https://m7.org/docs/sdk/m7-identity/README.md#optional-native-runtime) before
selecting a native verification or encryption feature.

The stable releases are `token-php` 0.1.4 and `cli-php` 0.1.3. The CLI requires
`token-php >=0.1.3 <0.2.0` for client credentials, device code,
refresh-package exchange, ACK, ensure, and authenticated resource requests.

For the complete operator processes, use the public
[CLI subject guide](https://m7.org/docs/sdk/m7-identity/cli/README.md), which covers client credentials, device
authorization, managed ensure, ACK, and authenticated requests beside this
installation page.

Download `token-php` 0.1.4 as its [ZIP](https://m7.org/downloads/m7-identity/token-php/0.1.4/m7-identity-token-php-0.1.4.zip),
[SHA-256 checksum](https://m7.org/downloads/m7-identity/token-php/0.1.4/m7-identity-token-php-0.1.4.zip.sha256),
and [release manifest](https://m7.org/downloads/m7-identity/token-php/0.1.4/m7-identity-token-php-0.1.4.zip.manifest.json).

Download `cli-php` 0.1.3 as its [ZIP](https://m7.org/downloads/m7-identity/cli-php/0.1.3/m7-identity-cli-php-0.1.3.zip),
[SHA-256 checksum](https://m7.org/downloads/m7-identity/cli-php/0.1.3/m7-identity-cli-php-0.1.3.zip.sha256),
and [release manifest](https://m7.org/downloads/m7-identity/cli-php/0.1.3/m7-identity-cli-php-0.1.3.zip.manifest.json).

## Upgrade agents sharing a profile

CLI/PHP 0.1.3 adds shared-profile coordination. Stop workers running older code,
verify and install the new package, and restart every participant against the
same protected store. Preserve the existing keychain. See the
[shared-profile upgrade and recovery guide](https://m7.org/docs/sdk/m7-identity/cli/shared-profiles.md).

## Verify and extract the CLI

These releases include RFC 8693 exchange and nested target-claim validation.
See [token-exchange setup](https://m7.org/docs/sdk/m7-identity/cli/installation.md#install-token-exchange-support) after installation.

Keep each ZIP with its `.sha256` and `.manifest.json` sidecars. Verify both
archives before extraction:

```sh
shasum -a 256 -c m7-identity-token-php-0.1.4.zip.sha256
shasum -a 256 -c m7-identity-cli-php-0.1.3.zip.sha256
unzip m7-identity-token-php-0.1.4.zip -d ./vendor
unzip m7-identity-cli-php-0.1.3.zip -d ./vendor
```

Point the CLI at the separately installed token package:

```sh
export M7_IDENTITY_TOKEN_ROOT="$PWD/vendor/m7-identity-token-php-0.1.4"
```

## Use the client-credentials CLI

Confirm the packaged command and dependency load:

```sh
./vendor/m7-identity-cli-php-0.1.3/bin/m7-identity --version
./vendor/m7-identity-cli-php-0.1.3/bin/m7-identity client-credentials --help
./vendor/m7-identity-cli-php-0.1.3/bin/m7-identity request --help
```

Start the controlling-terminal prompt flow with:

```sh
./vendor/m7-identity-cli-php-0.1.3/bin/m7-identity client-credentials --interactive
```

To use the protected keychain, select an existing machine app. Its profile
defaults to `default`; a successful complete response is stored before stdout:

```sh
./vendor/m7-identity-cli-php-0.1.3/bin/m7-identity client-credentials \
  --app user.m7.org-cc \
  --interactive

./vendor/m7-identity-cli-php-0.1.3/bin/m7-identity token status \
  --app user.m7.org-cc \
  --grant client-credentials
```

The second command prints metadata, never the token. Calls without `--app`
remain stateless.

Or configure an unattended call. The secret file must not be a symlink and
must grant no group or other permissions:

```sh
chmod 0600 /run/secrets/m7-client-secret
export M7_CLIENT_CREDENTIALS_CLIENT_ID='YOUR_REGISTERED_CLIENT_ID'
export M7_CLIENT_CREDENTIALS_CLIENT_SECRET_FILE='/run/secrets/m7-client-secret'
export M7_CLIENT_CREDENTIALS_AUD='https://api.user.m7.org'

./vendor/m7-identity-cli-php-0.1.3/bin/m7-identity client-credentials --output token
```

Any defined `M7_CLIENT_CREDENTIALS_*` value overrides its matching CLI option.
An empty defined value also overrides the option and may fail validation; the
command never silently falls back around deployment configuration.

Create and run a managed public device client from the same installation:

```sh
./vendor/m7-identity-cli-php-0.1.3/bin/m7-identity app upsert user.m7.org-device \
  --client-id YOUR_REGISTERED_DEVICE_CLIENT_ID \
  --type native \
  --authority consumer

./vendor/m7-identity-cli-php-0.1.3/bin/m7-identity profile upsert \
  --app user.m7.org-device \
  --grant device-code \
  --config-json '{"auth_method":"none","aud":["https://user.m7.org"],"scope":"openid profile offline_access","refresh_mode":"strict_rotation"}'

./vendor/m7-identity-cli-php-0.1.3/bin/m7-identity device-code \
  --app user.m7.org-device
```

The upserts make this setup block repeatable. They replace the editable app and
profile definitions rather than merging stale fields, while retaining stored
passwords and complete token history.

The raw stateless form is also available:

```sh
export M7_DEVICE_CODE_AUTH_METHOD='none'
export M7_DEVICE_CODE_CLIENT_ID='YOUR_REGISTERED_DEVICE_CLIENT_ID'
export M7_DEVICE_CODE_SCOPE='openid profile offline_access'

./vendor/m7-identity-cli-php-0.1.3/bin/m7-identity device-code
```

Interactive configuration is also available:

```sh
./vendor/m7-identity-cli-php-0.1.3/bin/m7-identity device-code --interactive
```

The browser approval page owns account authentication. Do not enter an M7
password into the CLI or store it beside the device code.

To rotate the managed bundle in place:

```sh
M7_EXCHANGE_REFRESH_MODE=strict_rotation \
  ./vendor/m7-identity-cli-php-0.1.3/bin/m7-identity exchange \
    --app user.m7.org-device
```

To exchange a stateless protected bundle returned by a raw device flow:

```sh
chmod 0600 /run/secrets/m7-refresh-bundle.json
export M7_EXCHANGE_AUTH_METHOD='none'
export M7_EXCHANGE_CLIENT_ID='YOUR_REGISTERED_DEVICE_CLIENT_ID'

./vendor/m7-identity-cli-php-0.1.3/bin/m7-identity exchange \
  --bundle-file /run/secrets/m7-refresh-bundle.json
```

The raw output is a rotated replacement package. It does not rewrite its input,
so persist the entire output before retiring the preceding bundle. The separate
store can import a protected complete bundle explicitly:

```sh
./vendor/m7-identity-cli-php-0.1.3/bin/m7-identity token import \
  --app YOUR_LOCAL_APP \
  --grant device-code \
  --file /run/secrets/m7-refresh-bundle.json
```

Omitting `--profile` selects `default`; app and grant are always explicit.

For managed two-phase activation, exchange stores pending and ACK promotes the
exact generation after authority success:

```sh
M7_EXCHANGE_REFRESH_MODE=ack \
  ./vendor/m7-identity-cli-php-0.1.3/bin/m7-identity exchange \
    --app user.m7.org-device

./vendor/m7-identity-cli-php-0.1.3/bin/m7-identity ack \
  --app user.m7.org-device
```

On failure, pending storage is retained. Use `--id GENERATION` if several
pending generations exist.

For a fault-tolerant ACK lifecycle, set
`M7_EXCHANGE_REFRESH_MODE=ack_supersede_pending` and run `exchange --app
user.m7.org-device`; then run `ack --app user.m7.org-device`. With no pending
state it starts normally from active. If a pending child already exists,
Identity disables it and issues the newest child while the predecessor stays
active until ACK. A locally retained pending generation is removed only after
its successor is stored. The protected-file forms remain available for
stateless/manual operation.

Call a protected API with a managed token:

```sh
./vendor/m7-identity-cli-php-0.1.3/bin/m7-identity request \
  --app user.m7.org-device \
  --url https://api.user.m7.org/api/v2/session/me \
  --response json \
  --query include=groups
```

With `--app`, the command reuses a fresh token or ensures and stores a
replacement before sending. Use `--minimum-ttl` and `--refresh-mode` to control
managed preflight; native profiles without a configured mode default to
`ack_supersede_pending`. Without `--method`, requests use GET unless JSON data
is supplied, which selects POST. The command also accepts `--bundle PATH`,
`--bundle-stdin`, protected manual token input, and separate fingerprint or
fresh DPoP proof input; those explicit credential sources are never refreshed
or stored. It does not blindly retry an API `401`.
With `--response json`, an incorrectly typed or malformed 2xx response also
fails while retaining its original body for diagnosis.

The remainder of this page describes the independently published library
artifact for PHP programs and background workers.

## 1. Check the library runtime

Use PHP 8.1 or newer with OpenSSL:

```sh
php -v
php -m | grep -E '^(curl|openssl)$'
```

OpenSSL is required. cURL is recommended for token acquisition, authenticated
resource requests, live certificate retrieval, introspection, and UserInfo;
supported PHP streams are the fallback transport.

## 2. Verify and extract the release

Keep these files together:

```text
m7-identity-token-php-0.1.4.zip
m7-identity-token-php-0.1.4.zip.sha256
m7-identity-token-php-0.1.4.zip.manifest.json
```

Verify the archive with one available command:

```sh
shasum -a 256 -c m7-identity-token-php-0.1.4.zip.sha256
```

```sh
sha256sum -c m7-identity-token-php-0.1.4.zip.sha256
```

Extract it into an application-owned, non-public package directory:

```sh
unzip m7-identity-token-php-0.1.4.zip -d ./vendor
```

The archive creates `m7-identity-token-php-0.1.4/` and includes its autoloader,
source, package metadata, documentation, manifest, and payload checksums.

## 3. Load the package

Load the bundled autoloader from the extracted release:

```php
require_once __DIR__ . '/vendor/m7-identity-token-php-0.1.4/autoload.php';
```

Do not edit the extracted package in place. Keep the archive manifest and
digest as the installation receipt, and install a new verified release when
upgrading.

## 4. Validate a token locally

The following command reads the serialized token from standard input so it does
not need to appear in the process argument list:

```php
<?php

require_once __DIR__ . '/vendor/m7-identity-token-php-0.1.4/autoload.php';

use M7\Identity\M7IdentitySDK;

$serializedToken = trim(stream_get_contents(STDIN));
if ($serializedToken === '') {
    fwrite(STDERR, "A token is required on standard input.\n");
    exit(2);
}

$certificate = file_get_contents('/path/to/trusted-public-certificate.pem');
$identity = new M7IdentitySDK();
$report = $identity->validate($serializedToken, [
    'audience' => 'https://api.example.m7.org/',
    'client_id' => 'YOUR_REGISTERED_CLIENT_ID',
    'principal_type' => 'consumer',
    'scope' => ['profile.read'],
    'cert_pem' => $certificate,
]);

if (!$report->ok()) {
    fwrite(STDERR, $report->reason() . "\n");
    exit(1);
}

fwrite(STDOUT, "Token validation passed.\n");
```

Read the token from the consuming process's protected runtime channel. Do not
store it in the script or use a literal token in a shell command.

## 5. Add live operations only when required

`clientCredentials()` needs an HTTPS token endpoint, a confidential client, and
at least one audience. `introspect()` needs an HTTPS introspection endpoint and
the registered client authentication method. `userinfo()` needs the
protected-resource endpoint and, for built-in M7 selectors, the registered
client ID. Store any client secret in the process secret manager and inject it
only into the trusted runtime.

Use bounded connection and response settings. Treat `active() === false`,
transport failures, malformed responses, and subject mismatches as closed
authorization outcomes.

## Install token-exchange support

RFC 8693 exchange and the nested `m7.target_client_id` validation option are
included in Token/PHP 0.1.4. CLI/PHP 0.1.3 requires
`token-php >=0.1.3 <0.2.0`; the ZIP installation above supplies both.

For development, a reviewed source checkout containing both matching packages
can also load the command directly:

```sh
export M7_IDENTITY_TOKEN_ROOT="$PWD/packages/token-php"
export PATH="$PWD/packages/cli-php/bin:$PATH"
m7-identity token-exchange --help
```

`M7_IDENTITY_TOKEN_ROOT` names the package directory containing `autoload.php`,
not the repository root. The CLI can also load the sibling Token/PHP package
automatically. In PHP, load `packages/token-php/autoload.php` from that checkout
before constructing `M7IdentitySDK`. An older loaded library without
`tokenExchange()` fails locally. See the [exchange guide](https://m7.org/docs/sdk/m7-identity/cli/token-exchange.md)
for server prerequisites, protected input/output, refresh and validation.

## Install organization and account-email operations

The verified Token/PHP 0.1.4 download includes `managedOrganizations()`,
endpoint resolution, `accountEmails()`, all five Organizations/Emails classes,
and the default facade. Use the complete archive above with the CLI's
package-root `M7_IDENTITY_TOKEN_ROOT` setting. The shared platform instead
uses an SDK-root layout containing `packages/token-php/autoload.php`.
Encryption retains its separate native runtime requirements. For usage see
[organizations](https://m7.org/docs/sdk/m7-identity/cli/organization-access.md) and [account emails](https://m7.org/docs/sdk/m7-identity/cli/account-emails.md).
