# OpenID Connect - Native SSO for Mobile Apps

Source: https://apereo.github.io/cas/development/authentication/OIDC-Authentication-NativeSSO-MobileApps.html

Native SSO is a new OpenID Connect extension designed to streamline the user experience mobile or desktop app suites by utilising a device session. If you are a vendor that offers a suite of mobile or desktop applications that require the user to be authenticated, this may relevant for you. Single-app vendors are not concerned with this OpenID Connect extension for device-based single sign-on (SSO) as it relies on the sharing of an ID token and a device-bound secret between the applications participating in the SSO. This requires a level of trust between the apps that is only practical when they belong to the same vendor.

The flow sequence in summary is as follows:

- The client app #1 performs a regular `code` flow to authenticate the end-user, using the special `device_sso` scope value to signal its intent to obtain a `device_secret` for the purposes of native SSO.
- The client app #2 uses the native SSO to sign-in the end-user and obtain tokens by making a direct back-channel token request, saving the end-user from the redirection to the OpenID provider to login into the app.

## Application Configuration

Client applications and relying parties must be configured to support the `device_sso` scope. This scope is used to indicate that the application intends to use the native SSO feature.

```json
{
  "@class" : "org.apereo.cas.services.OidcRegisteredService",
  "clientId": "...",
  "clientSecret": "...",
  "serviceId" : "...",
  "name": "OIDC",
  "id": 1,
  "scopes" : [ "java.util.HashSet", 
    [ "openid", "profile", "device_sso" ]
  ]
}
```

Once tokens are exchanged, the above scope allows the authentication flow to eventually generate a `device_secret`:

```json
{
  "access_token"  : "...",
  "token_type"    : "Bearer",
  "expires_in"    : 600,
  "scope"         : "openid profile",
  "id_token"      : "eyJraWQiOiIxZTlnZGs3IiwiYWxnIjoiUl...",
  "device_secret" : "..."
}
```

Client app #2 that finds an ID token and a `device_secret` available in its credential store shared with the vendor’s other apps may assume the user is already signed in and thus proceed with a direct back-channel token exchange request to CAS.

## Refresh Tokens

Client app #1 should send its `device_secret` as the `device_secret` request parameter when it refreshes its tokens, as the specification asks. When the device secret is still valid, the refresh response carries no new `device_secret`, and the refreshed ID token is bound to the device secret the client already holds, so both remain usable for native SSO. When the request carries no device secret, or one that is no longer valid, the response carries a new `device_secret` along with an ID token bound to it, and the client should replace both in its credential store. A device secret is valid while the single sign-on session it was issued in is valid; CAS keeps a record of it in the ticket registry for that long. The [stateless ticket registry](https://apereo.github.io/cas/development/ticketing/Stateless-Ticket-Registry.html) keeps no such record, so every refresh there issues a new device secret.

```bash
curl -X POST "https://sso.example.org/cas/oidc/token" \
  -d "grant_type=refresh_token&refresh_token=...&client_id=...&client_secret=...&device_secret=..."
```

## Token Exchange Request

A [token exchange request](https://apereo.github.io/cas/development/authentication/OAuth-ProtocolFlow-TokenExchange.html) requires the following:

- The client application must be configured to support the `urn:ietf:params:oauth:grant-type:token-exchange` grant type.
- The client application must bypass the approval prompt. CAS keeps no record of the end-user’s consent, so an application that asks for consent cannot receive tokens without the end-user, and its token exchange requests are refused with `interaction_required`; the application must then send the end-user through the authorization endpoint.
- The token exchange policy for the client application must list, in `allowedSubjectClientIds`, the client ids of the applications whose ID tokens it may use to sign in the end-user. When the policy defines `allowedActorTokenTypes`, it must include `urn:openid:params:token-type:device-secret`. The policy must not set `requireActorToken`: the device secret is no actor.

```json
{
  "@class" : "org.apereo.cas.services.OidcRegisteredService",
  "clientId": "...",
  "clientSecret": "...",
  "serviceId" : "...",
  "name": "OIDC",
  "id": 1,
  "scopes" : [ "java.util.HashSet", [ "openid", "email", "profile", "device_sso" ] ],
  "bypassApprovalPrompt": true,
  "generateRefreshToken": true,
  "supportedGrantTypes": [ "java.util.HashSet", 
    [ "authorization_code", "refresh_token", "urn:ietf:params:oauth:grant-type:token-exchange" ] ],
  "tokenExchangePolicy": {
    "@class": "org.apereo.cas.support.oauth.services.DefaultRegisteredServiceOAuthTokenExchangePolicy",
    "allowedTokenTypes": [ "java.util.HashSet", [
      "urn:ietf:params:oauth:token-type:access_token",
      "urn:ietf:params:oauth:token-type:id_token"
    ] ],
    "allowedSubjectClientIds": [ "java.util.HashSet", [ "..." ] ]
  }
}
```

When making the request,

- The `audience` request parameter should be set to the issuer of CAS, as the specification asks. It names the purview of CAS rather than a recipient, so the issued tokens keep the audience they would have without it.
- The `subject_token` request parameter must be set to the ID token obtained.
- The `subject_token_type` request parameter must be set to `urn:ietf:params:oauth:token-type:id_token`.
- The `actor_token` request parameter must be set to the `device_secret` obtained.
- The `actor_token_type` request parameter must be set to `urn:openid:params:token-type:device-secret`.
- The `requested_token_type` request parameter must be set to `urn:ietf:params:oauth:token-type:access_token` or `urn:ietf:params:oauth:token-type:id_token`.

An ID token is only accepted as the `subject_token` along with the `device_secret` it is bound to, and the single sign-on session it names must still be valid. A token exchange request that presents an ID token without its device secret is refused with `invalid_request`. A device secret that does not match the ID token, or a single sign-on session that has ended, is refused with `invalid_grant`. The device secret proves the ID token; it names no actor, so the issued tokens carry no `act` claim.

Tokens issued by the exchange belong to that single sign-on session. An exchange for an access token also returns an ID token for the client application when the `openid` scope is granted, and a refresh token when the application is configured to receive refresh tokens; the refresh token ends with the single sign-on session, and lives no longer than the `maxTimeToLive` of the token exchange policy, when the policy defines one. A requested ID token is returned in `access_token`, with the token type `N_A`, as the [token exchange](https://apereo.github.io/cas/development/authentication/OAuth-ProtocolFlow-TokenExchange.html) describes.
