Storing Credentials Securely
You should take great care to ensure your credentials are stored securely. If someone obtains youraccess_token with the wallet:transactions:send permission, s/he will be able to send all the bitcoin, litecoin or ethereum out of your account.
You should avoid storing API keys in your code base (which gets added to version control). The recommended best practice is to store them in environment variables. Separating credentials from your code base and database is always good practice.
OAuth2 access tokens and refresh tokens should be stored encrypted, with the encryption key stored in environment variables. To increase the security of your OAuth2 implementation, you should always specify a state parameter, request moderate wallet:transactions:send limits and implement 2FA authentication.
State Variable
To help protect against cross-site request forgery (CSRF), we recommended that you include a stateGET parameter during the OAuth2 authorization process. Verifying that this variable matches upon receipt of an authorization code will mitigate CSRF attempts. Make sure that you use a string that is at least 8 characters long.
An example of a request with state is as follows:
state param will be passed back via the redirect url with code param. You can read more about it here.
OAuth2 Redirect URI
For added security, allredirect_uris must use SSL (i.e. begin with https://). URIs without SSL can only be used for development and testing and will not be supported in production.
PKCE (Proof Key for Code Exchange)
For additional security, you can implement PKCE (Proof Key for Code Exchange) in your OAuth2 flow. PKCE provides protection against authorization code interception attacks, especially important for mobile and single-page applications. To use PKCE:- Generate a code verifier: Create a cryptographically random string between 43-128 characters.
- Create a code challenge: Transform the code verifier using one of the supported methods.
- Include in authorization request: Add
code_challengeandcode_challenge_methodparameters to your/oauth2/authrequest. - Include in token exchange: Add the original
code_verifierto your/oauth2/tokenrequest.
CautionIf you include a
code_challenge in your authorization request, you must include the corresponding code_verifier in your token exchange request. Failure to do so will result in an authentication error.Code Challenge Methods
Coinbase supports two methods for generating the code challenge:S256 Method (Recommended)
TheS256 method provides the strongest security by using SHA256 hashing:
- Hash the code verifier using SHA256
- Base64url-encode the hash (without padding)
- This method protects against code verifier exposure even if the authorization request is intercepted
Plain Method
Theplain method uses the code verifier directly as the code challenge:
- The code challenge equals the code verifier
- Simpler to implement but provides less security protection
- Suitable for environments where SHA256 hashing is not available
- If
code_challenge_methodis not specified, it defaults toplain - We strongly recommend using
S256whenever possible for enhanced security - The
plainmethod should only be used when SHA256 hashing is not feasible in your environment
PKCE Implementation Examples
S256 Method
Plain Method
Helper Functions
Validating SSL Certificates
It is also very important that your application validates our SSL certificate when it connects overhttps. This helps prevent a man in the middle attack. If you are using a client library, this may be turned on by default, but you should confirm this. Anytime you see a setting to ‘verify SSL’ you should ensure it is set to true.