Configure Snowflake OAuth for custom clients¶
This topic describes how to configure OAuth support for custom clients.
Workflow¶
The following high-level steps are required to configure OAuth for custom clients:
Register your client with Snowflake. To register your client, create an integration. An integration is a Snowflake object that provides an interface between Snowflake and third-party services, such as a client that supports OAuth.
The registration process defines a client ID and client secrets.
Configure calls to the Snowflake OAuth endpoints to request authorization codes from the Snowflake authorization server and to request and refresh access tokens.
The optional “scope” parameters in the initial authorization request limit the role permitted by the access token and can additionally be used to configure the refresh token behavior.
Note
In-session role switching to secondary roles is not supported with Snowflake OAuth.
If this behavior is necessary with your OAuth workflow, use External OAuth instead.
For more information, see Using secondary roles with External OAuth.
Create a Snowflake OAuth integration¶
Create a Snowflake OAuth integration using the
CREATE SECURITY INTEGRATION command. Be sure to
specify OAUTH_CLIENT = CUSTOM
when creating the integration.
Note
Only account administrators (users with the ACCOUNTADMIN role) or a role with the global CREATE INTEGRATION privilege can execute this SQL command.
Blocking specific roles from using the integration¶
The optional BLOCKED_ROLES_LIST parameter allows you to list Snowflake roles that a user cannot explicitly consent to using with the integration.
By default, the ACCOUNTADMIN, SECURITYADMIN, and ORGADMIN roles are included in this list and cannot be removed. If you have a business need to allow users to use Snowflake OAuth with these roles, and your security team allows it, please contact Snowflake Support to request that these roles be allowed for your account.
Using Client Redirect with Snowflake OAuth custom clients¶
Snowflake supports using Client Redirect with Snowflake OAuth Custom Clients, including using Client Redirect and OAuth with supported Snowflake Clients.
For more information, see Redirecting client connections.
Managing network policies¶
Snowflake supports network policies for OAuth. For more information, see Network policies.
Integration example¶
The following example creates an OAuth integration that uses key pair authentication. The integration allows refresh tokens, which expire after 1 day (86400 seconds). The integration blocks users from starting a session with SYSADMIN as the active role:
CREATE SECURITY INTEGRATION oauth_kp_int
TYPE = OAUTH
ENABLED = TRUE
OAUTH_CLIENT = CUSTOM
OAUTH_CLIENT_TYPE = 'CONFIDENTIAL'
OAUTH_REDIRECT_URI = 'https://localhost.com'
OAUTH_ISSUE_REFRESH_TOKENS = TRUE
OAUTH_REFRESH_TOKEN_VALIDITY = 86400
BLOCKED_ROLES_LIST = ('SYSADMIN')
OAUTH_CLIENT_RSA_PUBLIC_KEY ='
MIIBI
...
';
Call the OAuth endpoints¶
OAuth endpoints are the URLs that clients call to request authorization codes and to request and refresh access tokens. These endpoints refer to specific OAuth 2.0 policies that execute when the endpoint is called.
Snowflake provides the following OAuth endpoints:
- Authorization:
<snowflake_account_url>/oauth/authorize
- Token requests:
<snowflake_account_url>/oauth/token-request
Where <snowflake_account_url>
is a valid Snowflake account URL. For example, you might use the endpoints
https://myorg-account_xyz.snowflakecomputing.com/oauth/authorize
and
https://myorg-account_xyz.snowflakecomputing.com/oauth/token-request
. For a list of supported formats for the Snowflake account URL,
see Connecting with a URL.
To see a list of valid OAuth endpoints for a security integration, execute DESCRIBE INTEGRATION,
and then view the values in the OAUTH_ALLOWED_AUTHORIZATION_ENDPOINTS
and OAUTH_ALLOWED_TOKEN_ENDPOINTS
properties.
Token endpoint¶
This endpoint returns access tokens or refresh tokens depending on the request parameters. The token endpoint is as follows:
<snowflake_account_url>/oauth/token-request
Where:
snowflake_account_url
Specifies a valid Snowflake account URL. For example,
https://myorg-account_xyz.snowflakecomputing.com/oauth/token-request
.
HTTP method¶
POST
Ensure that the content-type header in the POST request is set as follows:
Content-type: application/x-www-form-urlencoded
Request header¶
The client ID and client secret must be included in the authorization header. Currently, Snowflake only supports the Basic Authentication Scheme, which means that the value expected is in the following form:
Basic Base64(client_id:client_secret)
Where:
Header Value |
Data Type |
Required |
Description |
---|---|---|---|
|
String |
Yes |
Client ID of the integration. |
|
String |
Yes |
Client secret for the integration. |
Both the client ID and client secret can be retrieved using the SYSTEM$SHOW_OAUTH_CLIENT_SECRETS function.
Note the :
character between client_id
and client_secret
.
Request body¶
Parameter |
Data Type |
Required |
Description |
---|---|---|---|
|
String |
Yes |
Type of grant requested: . |
|
String |
Yes |
Authorization code returned from the token endpoint. Used and required when |
|
String |
Yes |
Refresh token returned from an earlier request to the token endpoint when redeeming the authorization code. Used and required when |
|
String |
Yes |
Redirect URI as used in the authorization URL when requesting an authorization code. Used and required when |
|
String |
No |
Required only if the authorization request was sent to the Authorization Endpoint with a |
Response¶
A JSON object is returned with the following fields:
Field |
Data Type |
Description |
---|---|---|
|
String |
Access token used to establish a Snowflake session |
|
String |
Refresh token. Not issued if the client is configured to not issue refresh tokens or if the user did not consent to the |
|
Integer |
Number of seconds remaining until the token expires |
|
String |
Access token type. Currently, always |
|
String |
Username that the access token belongs to. Currently only returned when exchanging an authorization code for an access token. |
Successful response example¶
The following example shows a successful response when exchanging an authorization code for an access and refresh token:
{
"access_token": "ACCESS_TOKEN",
"expires_in": 600,
"refresh_token": "REFRESH_TOKEN",
"token_type": "Bearer",
"username": "user1",
}
Unsuccessful response example¶
The following example shows an unsuccessful response:
{
"data" : null,
"message" : "This is an invalid client.",
"code" : null,
"success" : false,
"error" : "invalid_client"
}
The message
string value is a description of the error and error
is the error type. For more information on the types of
errors returned, see OAuth Error Codes.
Token exchange¶
This endpoint returns an OAuth access token in exchange for a JSON Web Token (JWT). For an example, see Tutorial 1 (step 5). In the tutorial you send a request to this endpoint to exchange a JWT token for an OAuth token and use the OAuth token to access a public endpoint exposed by a Snowpark Container Services service.
The token endpoint is as follows:
<snowflake_account_url>/oauth/token
Where:
snowflake_account_url
Specifies a valid Snowflake account URL. For example,
https://myorg-account_xyz.snowflakecomputing.com/oauth/token
.
HTTP method¶
POST
Ensure that the content-type header in the POST request is set as follows:
Content-type: application/x-www-form-urlencoded
Request body¶
Parameter |
Data Type |
Required |
Description |
---|---|---|---|
|
String |
Yes |
Pass this as string |
|
String |
Yes |
Pass this as string |
|
String |
Yes |
Pass the JWT token. |
For example,
{
'grant_type': 'urn:ietf:params:oauth:grant-type:jwt-bearer',
'scope': 'session:role:TEST_ROLE ab12-orgname-acctname.snowflakecomputing.app',
'assertion': '<token>'
}
When specifying scope
, the session:role:role_name
is optional. If not provided, the default role of the user is used.
{
'grant_type': 'urn:ietf:params:oauth:grant-type:jwt-bearer',
'scope': 'ab12-orgname-acctname.snowflakecomputing.app',
'assertion': '<token>'
}
Response¶
An OAuth access token is returned
Proof key for code exchange¶
Snowflake supports Proof Key for Code Exchange (PKCE) for obtaining access tokens using the authorization_code
grant type as
described in RFC 7636. PKCE can be used to lessen the possibility of an authorization code
interception attack, and is suitable for clients that might not be able to fully keep the client secret secure.
By default, PKCE is optional and is enforced only if the code_challenge
and code_challenge_method
parameters are both
included in the authorization endpoint URL. However, Snowflake highly recommends that your client require PKCE for all authorizations to
make the OAuth flow more secure.
The following describes how PKCE for Snowflake works:
The client creates a secret called the code verifier and performs a transformation on it to generate the code challenge. The client holds onto the secret.
Important
Generate the code verifier from the allowed ASCII characters according to Section 4.1 of RFC 7636.
The client directing the user to the Authorization URL appends the following two query parameters:
code_challenge
Specifies the code challenge generated in Step 1.
code_challenge_method
Specifies the transformations used on the code verifier in Step 1 to generate the code challenge. Currently, Snowflake only supports SHA256, so this value must be set to
S256
. The transformation algorithm for SHA256 isBASE64URL-ENCODE(SHA256(ASCII(code_verifier)))
.
After the user consents to the requested scopes or Snowflake determines that consent is present for that user, the authorization code is issued.
The client receives the authorization code from the Snowflake authorization server, which it then submits along with the
code_verifier
in the request to the token endpoint.Snowflake transforms the
code_verifier
value and verifies that the transformed value matches thecode_challenge
value used when generating authorizations. If these values match, then the authorization server issues the access and refresh tokens.
Using key-pair authentication¶
Snowflake supports using key pair authentication rather than the typical username/password authentication when calling the OAuth token endpoint. This authentication method requires a 2048-bit (minimum) RSA key pair. Generate the PEM (Privacy Enhanced Mail) public-private key pair using OpenSSL. The public key is assigned to the Snowflake user who uses the Snowflake client.
To configure the public/private key pair:
From the command line in a terminal window, generate an encrypted private key:
$ openssl genrsa 2048 | openssl pkcs8 -topk8 -v2 des3 -inform PEM -out rsa_key.p8
OpenSSL prompts for a passphrase used to encrypt the private key file. Snowflake recommends using a strong passphrase to protect the private key. Record this passphrase. You must input it when connecting to Snowflake. Note that the passphrase is only used for protecting the private key and is never sent to Snowflake.
Sample PEM private key
-----BEGIN ENCRYPTED PRIVATE KEY----- MIIE6TAbBgkqhkiG9w0BBQMwDgQILYPyCppzOwECAggABIIEyLiGSpeeGSe3xHP1 wHLjfCYycUPennlX2bd8yX8xOxGSGfvB+99+PmSlex0FmY9ov1J8H1H9Y3lMWXbL ... -----END ENCRYPTED PRIVATE KEY-----
From the command line, generate the public key by referencing the private key:
$ openssl rsa -in rsa_key.p8 -pubout -out rsa_key.pub
Sample PEM public key
-----BEGIN PUBLIC KEY----- MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAy+Fw2qv4Roud3l6tjPH4 zxybHjmZ5rhtCz9jppCV8UTWvEXxa88IGRIHbJ/PwKW/mR8LXdfI7l/9vCMXX4mk ... -----END PUBLIC KEY-----
Copy the public and private key files to a local directory for storage. Record the path to the files.
Note that the private key is stored using the PKCS#8 (Public Key Cryptography Standards) format and is encrypted using the passphrase you specified in the previous step; however, the file should still be protected from unauthorized access using the file permission mechanism provided by your operating system. It is your responsibility to secure the file when it is not being used.
Assign the public key to the integration object using ALTER SECURITY INTEGRATION. For example:
ALTER SECURITY INTEGRATION myint SET OAUTH_CLIENT_RSA_PUBLIC_KEY='MIIBIjANBgkqh...';
Note
Only account administrators can execute the ALTER SECURITY INTEGRATION command.
Exclude the public key header and footer in the command.
Verify the public key fingerprint using DESCRIBE INTEGRATION:
DESC SECURITY INTEGRATION myint; +----------------------------------+---------------+----------------------------------------------------------------------+------------------+ | property | property_type | property_value | property_default | |----------------------------------+---------------+----------------------------------------------------------------------+------------------| ... | OAUTH_CLIENT_RSA_PUBLIC_KEY_FP | String | SHA256:MRItnbO/123abc/abcdefghijklmn12345678901234= | | | OAUTH_CLIENT_RSA_PUBLIC_KEY_2_FP | String | | | ... +----------------------------------+---------------+----------------------------------------------------------------------+------------------+
Note
The
OAUTH_CLIENT_RSA_PUBLIC_KEY_2_FP
property is described in Key Rotation (in this topic).Modify and execute the sample code below. The code uses the private key to encode a JWT and then passes that token to the Snowflake authorization server:
Update the security parameters:
<private_key>
: Open thersa_key.p8
file in a text editor, and copy the lines between theBEGIN
header and theEND
footer.
Update the session parameters:
<account_identifier>
: Specifies the full name of your account (provided by Snowflake).
Update the JSON Web Token (JWT) fields:
- post body
A JSON object with the following standard fields (“claims”):
Attribute
Data Type
Required
Description
iss
String
Yes
Specifies the principal that issued the JWT in the format
client_id.public_key_fp
whereclient_id
is the client ID of the OAuth client integration andpublic_key_fp
is the fingerprint of the public key that is used during verification.sub
String
Yes
Subject of the JWT in the format
account_identifier.client_id
whereaccount_identifier
is the full name of your Snowflake account andclient_id
is the client ID of the OAuth client integration. Depending on the cloud platform (AWS or Azure) and region where your account is hosted, the full account name might require additional segments. For more information, see theaccount
variable description under Token endpoint.iat
Timestamp
No
Time when the token was issued.
exp
Timestamp
Yes
Time when the token should expire. This period should be relatively short (e.g. a few minutes).
Sample code
Note that the
private_key
value includes the-----BEGIN
header and the-----END
footer.import datetime import json import urllib import jwt import requests private_key = """ <private_key> """ public_key_fp = "SHA256:MR..." def _make_request(payload, encoded_jwt_token): token_url = "https://<account_identifier>.snowflakecomputing.com/oauth/token-request" headers = { u'Authorization': "Bearer %s" % (encoded_jwt_token), u'content-type': u'application/x-www-form-urlencoded' } r = requests.post( token_url, headers=headers, data=urllib.urlencode(payload)) return r.json() def make_request_for_access_token(oauth_az_code, encoded_jwt_token): """ Given an Authorization Code, make a request for an Access Token and a Refresh Token.""" payload = { 'grant_type': 'authorization_code', 'code': oauth_az_code } return _make_request(payload, encoded_jwt_token) def make_request_for_refresh_token(refresh_token, encoded_jwt_token): """ Given a Refresh Token, make a request for another Access Token.""" payload = { 'grant_type': 'refresh_token', 'refresh_token': refresh_token } return _make_request(payload, encoded_jwt_token) def main(): account_identifier = "<account_identifier>" client_id = "1234" # found by running DESC SECURITY INTEGRATION issuer = "{}.{}".format(client_id, public_key_fp) subject = "{}.{}".format(account_identifier, client_id) payload = { 'iss': issuer, 'sub': subject, 'iat': datetime.datetime.utcnow(), 'exp': datetime.datetime.utcnow() + datetime.timedelta(seconds=30) } encoded_jwt_token = jwt.encode( payload, private_key, algorithm='RS256') data = make_request_for_access_token(oauth_az_code, encoded_jwt_token) refresh_token = data['refresh_token'] data = make_request_for_refresh_token(refresh_token, encoded_jwt_token) access_token = data['access_token'] if __name__ == '__main__': main()
After the token is created, submit it in requests to the token endpoint. Requests require the Bearer authorization format as the authorization header instead of the basic authorization format normally used for the client ID and client secret, as follows:
"Authorization: Bearer JWT_TOKEN"
Key rotation¶
Snowflake supports multiple active keys to allow for uninterrupted rotation. Rotate and replace your public and private keys based on the expiration schedule you follow internally.
Currently, you can use the OAUTH_CLIENT_RSA_PUBLIC_KEY
and OAUTH_CLIENT_RSA_PUBLIC_KEY_2
parameters for
ALTER SECURITY INTEGRATION to associate up to 2 public keys with a single user.
To rotate your keys:
Complete the steps in Using key-pair authentication (in this topic):
Generate a new private and public key set.
Assign the public key to the integration. Set the public key value to either
OAUTH_CLIENT_RSA_PUBLIC_KEY
orOAUTH_CLIENT_RSA_PUBLIC_KEY_2
(whichever key value is not currently in use). For example:alter integration myint set oauth_client_rsa_public_key_2='JERUEHtcve...';
Update the code to connect to Snowflake. Specify the new private key.
Snowflake verifies the correct active public key for authentication based on the submitted private key.
Remove the old public key from the integration. For example:
alter integration myint unset oauth_client_rsa_public_key;
Error codes¶
See the Error codes for a list of error codes associated with OAuth, as well as errors that are returned in the JSON blob, during the authorization flow, token request or exchange, or when creating a Snowflake session after completing the OAuth flow.