Skip to content

Authentication

Use code + PKCE when your application can receive a browser callback, or device authorization when it cannot. The minimum integration shows where authentication fits into an integration.

Discovery and expired sessions

Feeds advertise the Authentication Document at https://readino.com/api/v2/auth in their links and HTTP Link header, with relation http://opds-spec.org/auth/document and type application/opds-authentication+json. A protected profile or shelf also returns this document in the body of a 401 response when authentication is required. The document's id is its own URL; it is distinct from the OAuth issuer. The discovery relation, media type, and 401 document follow Authentication for OPDS. The feed JSON example in Feeds, navigation, and search also shows this link. It remains present in the HTTP Link header on a 304 feed response.

The document contains id, title, localized description, authentication, and links for logo, account registration, the website, My Books, Saved Books, and service-desc. The latter points to https://readino.com/.well-known/oauth-authorization-server, which describes the v2 OAuth endpoints, public-client authentication (none), and PKCE S256. Its issuer is the origin, such as https://readino.com, using OAuth server metadata and issuer identification.

Authentication Document excerpt showing only the recommended code + PKCE entry:

{
  "id": "https://readino.com/api/v2/auth",
  "title": "Readino",
  "links": [
    {
      "rel": "http://opds-spec.org/shelf",
      "type": "application/opds+json",
      "title": "My Books",
      "href": "https://readino.com/api/v2/opds2/my_books"
    },
    {
      "rel": "service-desc",
      "href": "https://readino.com/.well-known/oauth-authorization-server",
      "type": "application/json"
    }
  ],
  "authentication": [
    {
      "type": "authorization_code",
      "allowsWebview": false,
      "links": [
        {
          "rel": "authorization",
          "href": "https://readino.com/api/v2/auth/default/start",
          "type": "text/html"
        },
        {
          "rel": "token",
          "href": "https://readino.com/api/v2/auth/token",
          "type": "application/json"
        }
      ]
    }
  ],
  "description": "Sign in to your Readino account to download books you've bought, open your Saved Books, and see prices for your country."
}

Handle authentication failures according to the endpoint:

Response Client behaviour
Protected resource: 401, WWW-Authenticate: Bearer Authenticate if no usable session exists
Protected resource or purchase API: 401, Bearer error="invalid_token" Refresh the existing session and retry as the same account

Purchase preparation/status use strict bearer validation: never turn their rejected authenticated requests into anonymous purchases. Shared licence-download endpoints can return a bearer 401 without the versioned Authentication Document body.

Choosing a method

authorization_code is Readino’s preferred description of code + PKCE. This value, the URI-form code alias, and the device-grant URN as an OPDS authentication type are not defined by the linked Authentication for OPDS draft; that draft defines the implicit type. OAuth's grant_type=authorization_code is standard, but its use as an OPDS authentication type is a separate convention.

Select by type, not by array position:

Authentication type Links to use Meaning
authorization_code authorization, token Preferred description of code + PKCE
urn:ietf:params:oauth:grant-type:device_code device_authorization or its authorization alias, token; optional verification Device authorization
http://opds-spec.org/auth/oauth/code authenticate, token, refresh Legacy description of code + PKCE
http://opds-spec.org/auth/oauth/implicit authenticate, refresh Implicit flow; compatibility only

Prefer authorization_code for PKCE, and do not offer both PKCE entries as separate sign-in choices. Use device authorization when a browser callback is impractical; retain implicit only for compatibility.

All flows use the public client ID http://opds-spec.org/auth/client, with no client secret or dynamic registration. Readino also emits the extension allowsWebview: false on the code and implicit entries: use an external browser, including when sign-in continues through Google or Apple. Resolve endpoints from the selected entry; the request bodies below are form-encoded even when a link's response type is JSON.

Code + PKCE

The exchange uses OAuth PKCE with S256.

  1. Generate an unpredictable state and a PKCE verifier (43–128 unreserved ASCII characters). Its challenge is unpadded base64url of SHA-256 of the verifier.
  2. Open the authorization link with URL-encoded client_id, response_type=code, redirect_uri, state, code_challenge, and code_challenge_method=S256.
  3. Receive code, state, and iss in the callback query. Check the pending state and expected issuer before exchanging the code.
  4. POST to the token link with grant_type=authorization_code, client_id, code, code_verifier, and the identical redirect_uri.

For example, send this request over HTTPS to the discovered token endpoint; do not hard-code the example URL. The authorization code and verifier below are illustrative, and the loopback URI represents this example's original callback. The public client_id is the fixed value used by Readino.

POST /api/v2/auth/token HTTP/1.1
Host: readino.com
Content-Type: application/x-www-form-urlencoded
Accept: application/json
Content-Length: 223

grant_type=authorization_code&client_id=http%3A%2F%2Fopds-spec.org%2Fauth%2Fclient&code=PLACEHOLDER_AUTHORIZATION_CODE&code_verifier=dBjftJeZ4CVP-mB92K27uhbUJU1p1r_wW1gFWFOEjXk&redirect_uri=http%3A%2F%2F127.0.0.1%3A49152%2F

The form body is one line, even if it wraps on screen. Use the verifier generated for the original authorization attempt; the decoded redirect_uri must match that attempt's URI exactly. Form-encode the values as in the OAuth token request.

Authentication redirects support the opds scheme and custom schemes such as fbreader and dinoreader. We can easily add schemes for third-party clients; contact us to have your client's scheme added.

HTTP or HTTPS loopback URLs with host 127.0.0.1 or [::1] are also accepted. A desktop client can use, for example, http://127.0.0.1:49152/. localhost, arbitrary external HTTPS callbacks, and unsupported custom schemes are rejected. Redirect fragments are rejected. The server checks custom schemes, not a registry of complete callback URLs, so the client must check its own callback target.

Authorization codes last five minutes and are single-use; plain PKCE is not accepted. Invalid authorization-start parameters produce a direct HTTP 400 text response, not a guaranteed OAuth error redirect. Code-exchange errors are JSON (invalid_request, invalid_client, or invalid_grant).

Device authorization

The request and polling protocol follows RFC 8628.

POST client_id to the device authorization link. Optional client_name (app_name is accepted as an alias) is display text only: at most 100 characters, without control characters. scope is optional; Readino does not advertise a scope catalogue.

The response provides device_code, user_code, verification_uri, verification_uri_complete, expires_in, and interval. Show the user code and verification address, or encode verification_uri_complete as a QR code. Approval happens in that browser, not merely by opening the address.

Poll the token link with form fields grant_type=urn:ietf:params:oauth:grant-type:device_code, client_id, and device_code. Codes last ten minutes; the initial interval is five seconds. expires_in and interval are in seconds; if interval is omitted, use the RFC default of five seconds. authorization_pending means keep polling; slow_down increases the interval by five seconds; access_denied and expired_token stop this attempt. Creating too many device requests can also return HTTP 429 with slow_down.

Tokens and compatibility implicit flow

A successful exchange returns access_token, refresh_token, token_type, and expires_in. Treat tokens as opaque, and send Authorization: Bearer <token> on account requests. Access tokens last five minutes and refresh tokens last two days; expires_in is the access-token lifetime in seconds. Each successful refresh issues a new two-day refresh-token lifetime, measured from that refresh. The implementation imposes no separate absolute session-age limit; this does not guarantee that an account or token can never be revoked.

Refresh by POSTing grant_type=refresh_token, refresh_token, and optionally the fixed client_id to the selected token/refresh endpoint. Readino's /api/v2/auth/token and /api/v2/auth/refresh accept this grant. Each successful refresh rotates and invalidates the old refresh token: serialize refreshes for one session and replace both stored tokens together. HTTP 400 with invalid_grant requires sign-in. Network errors, throttling, and server failures do not justify deleting the session.

If the server invalidates the old refresh token but the response carrying the new tokens is lost, retrying the old token returns invalid_grant. There is no replay grace period or response-recovery mechanism: the client must authenticate again. A timeout alone does not reveal whether the refresh was accepted; retain the session until the retry's result establishes that reauthentication is needed.

The advertised implicit compatibility method uses response_type=token, the same required client ID, state, and redirect validation. It returns id, access and refresh tokens, token_type, expires_in, and state in the query, not a fragment. It is omitted from OAuth server metadata. New integrations should use PKCE or device authorization.