Requirements for OAuth 2 public clients
The initial purpose of the document was to identify OAuth 2 as a solution for replacing API keys in the command line clients. We determined that OAuth 2 is not a good fit for the command line clients, so design work for that issue has been moved to Personal Access Tokens. OAuth 2 public client implementation has not begun, as of the time of writing.
This document still outlines requirements for OAuth 2 public clients, which may be worth implementing in the future.
Summary
Using OAuth 2 to authenticate with Synapse has many benefits over using older authentication mechanisms. We can try to consolidate authentication services to using OAuth flows wherever possible. This document focuses on enabling the creation of OAuth 2 public clients, which would allow OAuth 2 based authentication in Synapse command line apps and third-party browser-based SPAs. Additionally, this will hasten the deprecation of API keys.
To securely support OAuth 2 public clients, these actions are needed:
Support PKCE in the authorization code flow: - PLFM-6337Getting issue details... STATUS
Add field to label OAuth 2 clients as either “public” or “confidential”; backfill existing clients as “confidential”: - PLFM-6338Getting issue details... STATUS
Public clients should not be issued secrets
Note: Refresh tokens become single-use bearer tokens.
Public clients should be required to use PKCE
Don’t save consent records for public OAuth clients
Revoke the current iteration of a refresh token when an expired/used refresh token is used: - PLFM-6339Getting issue details... STATUS
To fully support Synapse command line clients, in addition to the above, we must:
Add a bootstrapped OAuth client for Synapse command line apps: - PLFM-6340Getting issue details... STATUS
Add service to issue a (user-authenticated) refresh token for the Synapse command line apps OAuth client: - PLFM-6341Getting issue details... STATUS
Background
There are currently many ways to authenticate a request to Synapse, including
Session tokens. Session tokens grant access to all account functions. Session tokens expire after 24 hours, and they can also be refreshed (for an additional 24 hours) and revoked. To acquire a session token, users must either
Enter their username and password
Sign in using Google as an OIDC identity provider.
API keys. API keys grant access to all account functions. API keys do not expire, but they can be revoked, and a new key can be generated. Users may only have one API key active at a time. A user may retrieve their API key at any time (and as an implication, they are stored unhashed in Synapse).
Using an access token. Access tokens last 24 hours. Access tokens are also scoped, so that a token may only be used to perform specific types of actions. Only verified OAuth 2 clients may be issued access tokens. At this time, access tokens cannot be revoked unless they are issued with a refresh token.
There are two ways that a client can obtain an access token, both requiring client credentials.
Sending Synapse a valid authorization code, which is granted when a user authorizes the OAuth client. Authorization codes expire after one minute.
Sending Synapse a valid refresh token. The refresh token is granted when the user authorizes an app with the
offline_access
scope. Refresh tokens expire 180 days after being issued and are single-use, though a new refresh token is issued when one is used, so access should be considered non-expiring. Refresh tokens can be revoked, and access tokens associated with/granted by the refresh token will also be revoked. Additionally, we only store the hash of the refresh token, so the token can only be retrieved at the time it is generated.
Table Summary
| Session Token | API Key | OAuth 2.0 access/refresh token |
---|---|---|---|
Access Granted | Entire account | Entire account | Scoped, defined by the client |
Expires | 24 hrs after being issued | Forever | Access: 24 hrs after issued Refresh: 180 days after issued |
Revocable | Yes | Yes | Only if associated with refresh token |
Maximum that can be issued | 1 | 1 | Access: Unlimited Refresh: 100 per user per OAuth client |
Stored as/Re-retrievable | Unhashed/Yes | Unhashed/Yes | Access: Not stored/No Refresh: Hashed token/No |
Motivation
We can reduce the complexity and attack surface of authentication with Synapse by utilizing OAuth 2 login flows in places where other mechanisms are currently used.
In particular, we can aim to replace usage of the API key with OAuth refresh tokens. The most common use case for the API key is to authenticate Synapse command line clients. Using refresh/access tokens in place of an API key provides a couple of advantages:
Leased/timed expiration - if a particular session isn’t used for 180 days, the token expires
The ability to grant/revoke access on the machine-level, instead of having one key used everywhere
Scoped access, in cases where the command line app is running a job that only needs a subset of access/functionality
Using either the authorization code (currently supported) or device code (not currently supported) OAuth 2 flows require users to login on Synapse.org via a browser, instead of entering credentials at the command line, or pasting an API key into a config file.
The caveat to using OAuth 2.0 for native apps and JavaScript-based SPAs is that in these circumstances, the OAuth client cannot securely and confidentially store credentials. This scenario is a use case that is outlined in the OAuth 2.0 specification, but we currently expect all Synapse OAuth clients to be confidential clients. Therefore, we must consider the security and access implications of creating/allowing public OAuth clients.
Requirements/Notes/Considerations
This section contains an explanation of each requirement for supporting OAuth 2 public clients in Synapse. This section is summarized at the top of this document, and services are enumerated at the bottom of this document.
PKCE
Best practices suggest that authorization servers are required to support PKCE [2][3]. PKCE only provides benefits to public OAuth clients, since a malicious app must have knowledge of an OAuth client’s credentials to execute the attack.
For more details, expand the following section:
Designating new clients as either public or confidential
All existing clients are considered to be “confidential” clients, so we can easily backfill that information.
We can then use the public/confidential label to handle logic accordingly [2]:
Authorization servers MUST record the client type in the client registration details in order to identify and process requests accordingly.
As an example, we can require that public clients utilize PKCE, among other considerations.
In OAuth 2.0, the authorization server SHOULD NOT assume client type [1]. We can require client creators to specify their client type upon creation. This is a breaking API change, but it is acceptable because OAuth clients aren’t created programmatically (it wouldn’t make sense if they are, since they must be manually verified to be functional).
Public clients may or may not be issued a secret
Client credentials (other than the ID) are not useful for public clients [1]:
The authorization server MAY establish a client authentication method with public clients. However, the authorization server MUST NOT rely on public client authentication for the purpose of identifying the client.
OAuth 2.1 ([2]) suggests that they shouldn’t be required, so why issue them?
Secrets that are statically included as part of an app distributed to multiple users should not be treated as confidential secrets, as one user may inspect their copy and learn the shared secret. For this reason, it is NOT RECOMMENDED for authorization servers to require client authentication of public native apps clients using a shared secret, as this serves little value beyond client identification which is already provided by the "client_id" request parameter.
The only endpoints that require OAuth client credentials involve issuing or revoking tokens, so permitting public clients to have no secret should not cause any issues with, for example, public client metadata being editable by unauthorized parties.
Public clients may be required to use certain grant types
Currently, the only grant types implemented are the more secure possible grant types, likely meaning no restrictions at this time.
Users may need to explicitly opt-in to allowing public client access to their resources (e.g. a simple checkbox and supplemental info modal on their profile settings page)
Use of an expired refresh token invalidates the active refresh token
The following would only apply to public OAuth clients because the refresh token acts as a bearer token. The goal is to reduce the risk and impact of token theft/replay attacks.
OAuth 2.1 ([2]) Note on refresh tokens for public clients:
Authorization server MUST utilize one of these methods to detect refresh token replay by malicious actors for public clients:
* _Sender-constrained refresh tokens:_ the authorization server cryptographically binds the refresh token to a certain client instance by utilizing [I-D.ietf-oauth-token-binding] or [RFC8705]
* _Refresh token rotation:_ the authorization server issues a new refresh token with every access token refresh response. The previous refresh token is invalidated but information about the relationship is retained by the authorization server. If a refresh token is compromised and subsequently used by both the attacker and the legitimate client, one of them will present an invalidated refresh token, which will inform the authorization server of the breach. The authorization server cannot determine which party submitted the invalid refresh token, but it will revoke the active refresh token. This stops the attack at the cost of forcing the legitimate client to obtain a fresh authorization grant.
We will use refresh token rotation because it is partially implemented (we currently do not revoke an active token if an expired token is used). We can extend our implementation to include the missing behavior.
Token binding is complicated, so we won’t use it. For details, expand the following section.
Risk in Saving Consent Records for Public OAuth Clients
In Synapse, we keep track of when a user consents to a particular selection of scope and claims, so that a user does not have to re-authorize an application that has already been authorized. This is for convenience; this mechanism isn’t specification-related.
A new attack mechanism is opened up if this same mechanism is applied to public OAuth clients, because the client credentials (if they exist) are not secrets--a malicious actor could use a public client that a user has already authorized to gain access to an account without a user’s consent.
For an illustration of this scenario, expand the following section:
New Services
Enabling public clients in general is required before meeting requirements for browserless command line apps, so the first set of requirements below must be met before beginning the second set.
Public clients capable of using authorization code flow
Public clients are first- or third-party OAuth apps that cannot reliably store a client secret. Currently, the only way to acquire OAuth access is through the authorization code flow, which requires a web browser. The following services must be modified to support public OAuth clients that can use the authorization code flow
Endpoint | Request Body | Response Body | Notes/Modifications | New Service? |
---|---|---|---|---|
OAuthClient extended to require a new field | ❌ | |||
None | Do not generate client secrets for public clients. | ❌ | ||
OIDCAuthorizationRequest extended to include | ❌ | |||
Multiple parameters | Additional request parameter | ❌ | ||
Multiple parameters | Client secret not required if the client ID is a public client. | ❌ | ||
Multiple parameters | If a public client uses an expired/rotated refresh token, revoke the current ‘version’ of the refresh token (e.g. refresh token is | ❌ | ||
- | - | Restrict client 0 from using these endpoints (this is already the case, but it is unclear if it is explicit). | ❌ |
Support for first-party command line public clients
Command line apps (like the Synapse Python client) cannot always support the authorization code flow because some environments do not have a browser. To enable this, we must do at least one of the two:
Support another OAuth grant type (e.g.
device_code
)Add a user-authenticated service to generate an OAuth refresh token
A user-authenticated service is similar to the OAuth token generation service in GitHub, and is similiar to how users retrieve API keys today, so we will move forward with that.
Endpoint | Request Body | Response Body | Notes | New Service? |
---|---|---|---|---|
N/A | N/A | N/A | Add a bootstrapped ‘Synapse Command Line’ OAuth client. We use a new client and not client 0 for two reasons
| ❌ |
POST /oauth2/userGeneratedToken | OIDCTokenGenerationRequest {
} | OIDCTokenResponse | Generates a token response that users can copy and paste into the command line client. Effectively replaces API keys. In most cases, the | ✅ |
References
[2] OAuth 2.1 (IETF Draft, last updated 2020 April 24)
[3] OAuth 2.0 Security Best Current Practice (IETF Draft, last updated 2020 April 05)
[4] OAuth 2.0 Mutual-TLS Client Authentication and Certificate-Bound Access Tokens (RFC 8705)
Other Resources
OAuth 2.0 for Native Apps (RFC 8252)
OAuth 2.0 for Browser-Based Apps (IETF Draft, last updated 2020 April 05)