Home Arrow Icon Knowledge base Arrow Icon Global Arrow Icon What libraries can help with generating code_verifier and code_challenge in a Figma plugin


What libraries can help with generating code_verifier and code_challenge in a Figma plugin


here are some libraries that can help with generating the `code_verifier` and `code_challenge` for the PKCE flow in a Figma plugin:

1. crypto-js: This is a collection of standard and secure cryptographic algorithms implemented in JavaScript. You can use it to generate a random string for the `code_verifier` and then create the `code_challenge` by hashing and base64url encoding it.

Example:

javascript
const crypto = require('crypto-js');

// Generate code_verifier
const codeVerifier = crypto.lib.WordArray.random(32).toString();

// Generate code_challenge
const codeChallenge = crypto.SHA256(codeVerifier).toString(crypto.enc.Base64)
  .replace(/\+/g, '-')
  .replace(/\//g, '_')
  .replace(/=+$/, '');

2. pkce-challenge: This is a small library specifically designed for generating the `code_verifier` and `code_challenge` for the PKCE flow. It handles the base64url encoding for you.

Example:

javascript
const pkceChallenge = require('pkce-challenge');

const { code_verifier, code_challenge } = pkceChallenge();

Both of these libraries should work well for generating the necessary PKCE values in a Figma plugin. The `crypto-js` library provides more flexibility if you need to perform other cryptographic operations, while `pkce-challenge` is a more specialized and lightweight option.

The key steps are:

1. Generate a random string for the `code_verifier`
2. Create the `code_challenge` by hashing and base64url encoding the `code_verifier`
3. Use these values when initiating the OAuth flow in your Figma plugin

Citations:
[1] https://www.figma.com/plugin-docs/libraries-and-bundling/
[2] https://evilmartians.com/chronicles/how-to-make-next-level-figma-plugins-auth-routing-storage-and-more
[3] https://www.figma.com/developers/api
[4] https://www.figma.com/plugin-docs/oauth-with-plugins/
[5] https://www.figma.com/community/tag/libraries/plugins