Authenticating to the server¶
This topic describes how to authenticate to the server when using the Snowflake SQL API.
When you send a request, the request must include authentication information. The next sections explain how to add this information to the request:
Using OAuth¶
To use OAuth, follow these steps:
Set up OAuth for authentication.
See Introduction to OAuth for details on how to set up OAuth and get an OAuth token.
Use Snowflake CLI to verify that you can use a generated OAuth token to connect to Snowflake:
For Linux and MacOS systems
For Windows systems
In each API request you send, set the following headers:
Authorization: Bearer oauth_tokenwhere
oauth_tokenis the generated OAuth token.(Optional)
X-Snowflake-Authorization-Token-Type: OAUTHIf you omit the
X-Snowflake-Authorization-Token-Typeheader, Snowflake determines the token type by examining the token.Even though this header is optional, you can choose to specify this header. You can set the header to one of the following values:
KEYPAIR_JWT(for key-pair authentication)OAUTH(for OAuth)PROGRAMMATIC_ACCESS_TOKEN(for programmatic access tokens)
Using key-pair authentication¶
To use key pair authentication, follow these steps:
Set up key-pair authentication.
As part of this process, you must:
Generate a public-private key pair. The generated private key should be in a file (e.g. named
rsa_key.p8).Assign the public key to your Snowflake user. After you assign the key to the user, run the DESCRIBE USER command. In the output, the
RSA_PUBLIC_KEY_FPproperty should be set to the fingerprint of the public key assigned to the user.
For instructions on how to generate the key pair and assign a key to a user, see Key-pair authentication and key-pair rotation. For language-specific examples of creating a fingerprint and generating a JWT token, see the following:
Use Snowflake CLI to verify that you can use the generated private key to connect to Snowflake:
The command prompts you for a private key passphrase to complete the connection. You can avoid the prompt by providing the passphrase in the
PRIVATE_KEY_PASSPHRASEenvironment variable.In your application code:
Generate the fingerprint (a SHA-256 hash) of the public key for the user. Prefix the fingerprint with
SHA256:. For example:SHA256:hashYou can also execute the SQL DESCRIBE USER command to get the value from the RSA_PUBLIC_KEY_FP property.
Generate a JSON Web Token (JWT) with the following fields in the payload:
Field
Description
Example
issIssuer of the JWT. Set it to the following value:
account_identifier.user.SHA256:public_key_fingerprintwhere:
account_identifieris your Snowflake account identifier.If you are using the account locator, exclude any region information from the account locator.
useris your Snowflake user name.SHA256:public_key_fingerprintis the fingerprint that you generated in the previous step.
Note
The
account_identifieranduservalues must use all uppercase characters. If your account ID contains periods (.), you must replace them with hyphens (-), as periods in an account identifier cause the JWT to be invalid.MYORGANIZATION-MYACCOUNT.MYUSER.SHA256:public_key_fingerprintsubSubject for the JWT. Set it to the following value:
account_identifier.userMYORGANIZATION-MYACCOUNT.MYUSERiatIssue time for the JWT in UTC. Set the value to the current time value as either seconds or milliseconds.
1615370644(seconds) .1615370644000(milliseconds)expExpiration time for the JWT in UTC. You can specify the value as either seconds or milliseconds.
Note
The JWT is valid for at most one hour after the token is issued, even if you specify a longer expiration time.
1615374184(seconds) .1615374184000(milliseconds)In each API request that you send, set the following headers:
Authorization: Bearer JWTwhere
JWTis the token that you generated.(Optional)
X-Snowflake-Authorization-Token-Type: KEYPAIR_JWTIf you omit the
X-Snowflake-Authorization-Token-Typeheader, Snowflake determines the token type by examining the token.Even though this header is optional, you can choose to specify this header. You can set the header to one of the following values:
KEYPAIR_JWT(for key-pair authentication)OAUTH(for OAuth)PROGRAMMATIC_ACCESS_TOKEN(for programmatic access tokens)
Python example¶
The following sections describe how to generate a JWT and fingerprint using Python.
For an example of generating a JWT in Python, see sql-api-generate-jwt.py. The
sql-api-generate-jwt.py example uses the PyJWT module, which you can install by running:
Generating a JWT in Python¶
The following sections of code demonstrate how to generate a JWT. For a full example,
see sql-api-generate-jwt.py.
Note
This example is intended for use as a reference only. Do not use this code in production applications or environments.
Generating a fingerprint in Python¶
The following sections of code demonstrate how to generate the fingerprint. For a full example, see
sql-api-generate-jwt.py.
Snowflake CLI example¶
You can use the Snowflake CLI snow connection generate-jwt command to generate a JWT for key-pair authentication. For more information, see snow connection generate-jwt.
This example generates a token for account TEST and user JDOE, using the private key from rsa_key.p8:
The command prompts you for a private key passphrase to complete the connection. You can avoid the prompt by providing the passphrase in the PRIVATE_KEY_PASSPHRASE environment variable.
Java example¶
For an example of generating a JWT in Java, see
SimpleStatementsApi.java.
Note
This example is intended for use as a reference only. Do not use this code in production applications or environments.
This example uses the following third-party libraries:
Swagger Codegen: an open source library useful in developing REST APIs and applications.
Auth0: provides Java APIs for authentication and generating JWT tokens.
Node.js example¶
For an example of generating a JWT in Node.js, see
sql-api-generate-jwt.js.
Note
This example is intended for use as a reference only. Do not use this code in production applications or environments.