Implement login with PADMA using OAuth2 and OpenID Connect

Automatic translation. View original

This guide is for developers integrating an external application with PADMA. Implement the button Sign in with PADMA using OAuth2 and OpenID Connect. Your users authorize access to the necessary data without sharing their password with your application.

Accounts is PADMA's authentication service. An account represents a school or organization to which the user has access. Your application must respect the authorized accounts and the user's permissions.

OAuth2 defines access authorization. OpenID Connect (OIDC) adds user authentication and identity. To integrate a web, SPA, or mobile application, use the Authorization Code flow with PKCE and request the scope openid.

1. Register your application

Contact Support to request registration. Provide the application name, client type, return URL (callback or redirect_uri) and the required scopes. You will need the issuer, the client_id, the registered callback, and for a confidential client, the client_secret.

Client type When to use Authentication at the token endpoint
Public SPA, mobile app, or other client that cannot protect a secret. client_id, without client_secret. PKCE S256 is mandatory.
Confidential Application with backend that stores the secret exclusively on the server. client_id and client_secret, preferably via HTTP Basic. PKCE is also recommended.

Register HTTPS callbacks without wildcards. For local public clients, HTTP callbacks are also accepted at 127.0.0.1 or localhost, with dynamic port; scheme, host, and path must match the registered callback. Use the same redirect_uri in authorization and code exchange. In mobile apps, open authorization in the system browser.

Never put a client_secret in the frontend, in mobile applications, repositories, or logs. If your application cannot keep the secret on the server, register it as a public client.

2. Configure the issuer and discovery

The issuer identifies the service that issues the tokens. Use https://accounts.derose.app and maintain this value throughout the flow.

Configure an OIDC library with this issuer. It should query the OpenID Connect configuration, also called discovery. Use the endpoints returned by this document.

The available endpoints are:

Operation Endpoint
Authorize GET /oauth/authorize
Exchange code or renew tokens POST /oauth/token
Query OIDC identity GET or POST /oauth/userinfo
Query PADMA identity and context GET /v1/me
Revoke POST /oauth/revoke
Check if a token is active POST /oauth/introspect
Obtain public signing keys, JWKS GET /oauth/discovery/keys
Register client dynamically, with restrictions POST /oauth/registration

In the examples, replace ISSUER_DA_INTEGRACAO with the issuer URL indicated above. Also replace the client ID and the callback with the registered values:

ISSUER='ISSUER_DA_INTEGRACAO'
CLIENT_ID='SEU_CLIENT_ID'
REDIRECT_URI='https://app.example/oauth/callback'

3. Choose the scopes and accounts

The scopes define the permissions requested by the application. Request only the scopes your application needs. They must also be enabled for the registered client.

Scope What it allows
openid OIDC authentication and stable identifier sub.
profile preferred_username, name and locale.
email email and email_verified.
account Authorized accounts: accounts, with account_name, brand_name and roles of each account. It also includes these fields at the main response level.
money Access to Money MCP, when the token also has the correct resource.
crm Reading via CRM MCP, when the token also has the correct resource.
learn Manage publications, students, accesses, subscriptions, courses, and attendance in Learn via MCP, according to user permissions and with the correct resource.

The default scopes are openid profile. Request email, account, money, crm and learn explicitly when required. The login examples below request openid profile email account.

With the scope account, the consent screen allows selecting one or multiple enabled accounts. The current account starts selected. The authorization is bound to the chosen accounts: changing the current account later does not transfer tokens to another account, and new user associations are not included without a new authorization.

If the user loses access to an account, it stops being part of the effective access. The token may remain valid as long as there is another active authorized account. The fields account_name, brand_name and roles at the main level represent the primary available account; use the list accounts to work with multiple accounts.

4. Start login with PKCE

Before redirecting the user, generate random values with a cryptographically secure generator:

  • state: binds the callback to the login attempt and protects against CSRF.
  • nonce: binds the ID token to the same attempt.
  • code_verifier: PKCE secret of 43 to 128 characters allowed by the PKCE standard.

Store the three values in the session associated with the login attempt. Generate a new set for each attempt and compute:

code_challenge = BASE64URL(SHA256(code_verifier), sem padding)

Open the user’s browser at the authorization URL. Replace {authorization_endpoint} with the endpoint returned by discovery. Replace the uppercase names with your integration values. Line breaks are for readability only. Encode parameters as URL components.

{authorization_endpoint}
  ?client_id=SEU_CLIENT_ID
  &redirect_uri=https%3A%2F%2Fapp.example%2Foauth%2Fcallback
  &response_type=code
  &scope=openid%20profile%20email%20account
  &state=STATE_ALEATORIO
  &nonce=NONCE_ALEATORIO
  &code_challenge=CHALLENGE_CALCULADO
  &code_challenge_method=S256

Send only the challenge to authorization. The code_verifier remains with your application until the code exchange. The user logs into Accounts and approves or denies the requested permissions.

5. Validate the callback and exchange the code

After approval, Accounts redirects to the registered callback:

https://app.example/oauth/callback?code=CODIGO_RECEBIDO&state=STATE_ALEATORIO

Compare state with the stored value, using a constant-time comparison. Reject callbacks without state, with a different value, or already processed. If it comes error instead of code, validate the state and handle the denial or failure without attempting to exchange a nonexistent code.

The code is single-use and expires in 10 minutes. Exchange it at the token endpoint, using application/x-www-form-urlencoded:

curl --request POST "$ISSUER/oauth/token" \
  --data-urlencode 'grant_type=authorization_code' \
  --data-urlencode "client_id=$CLIENT_ID" \
  --data-urlencode 'code=CODIGO_RECEBIDO' \
  --data-urlencode "redirect_uri=$REDIRECT_URI" \
  --data-urlencode 'code_verifier=VERIFIER_GUARDADO'

This example is for a public client. For a confidential client, replace the client_id body parameter with HTTP Basic authentication using --user "$CLIENT_ID:$CLIENT_SECRET". Store CLIENT_SECRET only on the backend. If the confidential client started the flow without PKCE, omit code_verifier.

The response includes values like these, all fictitious:

{
  "access_token": "ACCESS_TOKEN_RECEBIDO",
  "token_type": "Bearer",
  "expires_in": 3600,
  "refresh_token": "REFRESH_TOKEN_RECEBIDO",
  "scope": "openid profile email account",
  "id_token": "ID_TOKEN_JWT_RECEBIDO"
}

The access token and refresh token are opaque: do not attempt to decode them. The id_token is a signed JWT, issued when you request openid.

Validate the ID token before starting the session

Use an OIDC library to verify:

  • Signature with the keys from jwks_uri published in discovery and algorithm RS256.
  • iss exactly matching the configured issuer.
  • aud containing your client_id.
  • exp still valid.
  • nonce matching the stored value for the login attempt.

The ID token expires in 5 minutes. Decoding the JWT does not replace validation. Use sub as the stable user identifier; email and username may change. If your application accepts other OIDC providers, associate identity by the pair iss and sub.

6. Query the identity

Send the access token in the header Authorization, without placing it in the URL:

curl "$ISSUER/oauth/userinfo" \
  --header 'Authorization: Bearer ACCESS_TOKEN_RECEBIDO'

If you also use the ID token, check if the sub returned by UserInfo matches the sub validated ID token.

To obtain the PADMA representation, including the granted scopes and the resource when requested, check:

curl "$ISSUER/v1/me" \
  --header 'Authorization: Bearer ACCESS_TOKEN_RECEBIDO'

Example response from /v1/me, with fictitious data and two authorized accounts:

{
  "sub": "550e8400-e29b-41d4-a716-446655440000",
  "scope": "openid profile email account",
  "preferred_username": "ana.silva",
  "name": "Ana Silva",
  "locale": "pt",
  "email": "ana@example.com",
  "email_verified": true,
  "account_name": "escola-exemplo",
  "brand_name": "padma",
  "roles": ["admin"],
  "accounts": [
    {
      "account_name": "escola-exemplo",
      "brand_name": "padma",
      "roles": ["admin"]
    },
    {
      "account_name": "outra-escola",
      "brand_name": "padma",
      "roles": ["teacher"]
    }
  ]
}

The claims are the identity fields returned by the service. Handle these fields according to the granted scopes. They may be omitted or contain null values; do not assume that all profiles have the same data or roles.

7. Authorize access to an API with resource

The resource identifies the API that will receive the access token. Use the canonical URL published by the service. For example, Money MCP uses https://money.derose.app/mcp and requires the scope money.

Copy this URL to the variable RESOURCE. For authorization, encode the URL as a query string component and replace RESOURCE_CODIFICADO below.

In this case, replace the parameter scope in the authorization example with the value below and add resource:

&scope=openid%20profile%20email%20account%20money
&resource=RESOURCE_CODIFICADO

In the code exchange, add the same value:

--data-urlencode "resource=$RESOURCE"

The resource must match exactly in the authorization and code exchange, including domain and path. In /v1/me, this token will have aud equal to the resource and scope including money. CRM MCP follows the same principle, with scope crm and resource https://crm.derose.app/mcp.

. aud of the ID token identifies your client_id; the aud API in /v1/me identifies the resource. The API needs to check its resource and the required scopes. An ID token does not replace the access token for API calls.

For login and identity query without a target API, omit resource. This flow remains available. Existing integrations with API keys, such as business APIs /v0, maintain their authentication mechanism; confirm the contract of the service you intend to access.

8. Renew or revoke tokens

The access token expires in 1 hour. To renew it in a public client:

curl --request POST "$ISSUER/oauth/token" \
  --data-urlencode 'grant_type=refresh_token' \
  --data-urlencode "client_id=$CLIENT_ID" \
  --data-urlencode 'refresh_token=REFRESH_TOKEN_ATUAL'

In a confidential client, use HTTP Basic instead of the parameter client_id, as in the code exchange.

Each renewal returns a new refresh token and invalidates the previous one. Replace tokens atomically and avoid concurrent renewals with the same refresh token. In the renewal, you can omit resource: Accounts preserves the original. If you send it, it must be exactly the same; another value causes invalid_target.

The renewable session has an absolute limit of 1 year from the original authorization. Rotation does not extend this period. When the session expires, start a new authorization. Access may also end earlier due to revocation, application removal, or loss of all active authorized accounts.

When the user disconnects the integration, revoke tokens and remove the local session:

curl --request POST "$ISSUER/oauth/revoke" \
  --data-urlencode "client_id=$CLIENT_ID" \
  --data-urlencode 'token=ACCESS_TOKEN_OU_REFRESH_TOKEN_ATUAL'

Confidential clients also use HTTP Basic on revocation. Store tokens securely and never log tokens, codes, secrets, verifiers, or the full ID token in logs.

To check a token without using it, the client can call /oauth/introspect, authenticating in the same way and sending token. It can only inspect tokens issued to itself; check the boolean active before considering the other fields.

Common errors

Error What to check
access_denied The user denied consent. Allow retry when desired.
invalid_client Issuer, client_id, client type and secret used.
invalid_scope Requested scopes and scopes enabled for the application.
invalid_grant Expired or already used code, incorrect callback or verifier, invalid refresh token or renewal deadline exceeded.
invalid_target Resource omitted or different in code exchange, or different from the original in renewal.
invalid_request Mandatory parameters and request format.
invalid_token / HTTP 401 Access token expired, revoked or without valid account context.

In responses 401, also check the header WWW-Authenticate; do not rely on a JSON body to identify failure. Renew the token when possible or request new authentication. Avoid indefinitely repeating the same request with an invalid token.

MCP Agents: CIMD and dynamic registration

For MCP agents, first use a client already configured for the same issuer. If none exists, prefer Client ID Metadata Document (CIMD); use Dynamic Client Registration (DCR) as an alternative when necessary. Capabilities are published in discovery.

  • CIMD: the client_id is a stable and public HTTPS URL that returns a JSON document with the same client_id, application name, callbacks, grants and authentication method none. Accounts queries and validates the document. These clients are public and use PKCE S256.
  • DCR: use the registration_endpoint returned by discovery. Accounts accepts public clients with local loopback callbacks or official hosted callbacks from Claude: https://claude.ai/api/mcp/auth_callback and https://claude.com/api/mcp/auth_callback. It is not an unrestricted registration of any web application and does not provide client_secret.

Use allowed callbacks, without wildcards, and respect Retry-After if the server responds 429. After registration, the agent uses the same authorization flow, scopes, and resource described above.

Need to register your application or clarify an integration error? Contact Support.

Back to General

Contact support