Proof Key for Code Exchange (PKCE) is an authorization code flow extension to OAuth which is necessary for mobile authentication but works well for web flows because the added security is transparent to the user. Specifically, PKCE prevents the following types of attacks:
-
Authorization code interception
-
Authorization code injection
This gem is an implementation of the RFC 7636 specification so you can leverage PKCE in your own code.
Features
-
Implements the RFC 7636 specification.
-
Provides a simple object API for obtaining a challenge and verify code.
-
Provides max length security by default.
-
Answers a monad result.
Setup
To install with security, run:
# 💡 Skip this line if you already have the public certificate installed.
gem cert --add <(curl --compressed --location https://alchemists.io/gems.pem)
gem install pkce --trust-policy HighSecurity
To install without security, run:
gem install pkce
You can also add the gem directly to your project:
bundle add pkce
Once the gem is installed, you only need to require it:
require "pkce"
Usage
The object API is simple to work with as you only need to interact with the PKCE
constant. Example:
You can also pass in a custom length (default is maximum):
In case of a failure, you’ll get a proper error message:
Due to the fact that PKCE answers back a monad, you have all of the power of pattern matching at your fingertips as well:
Finally, since the code answered back is a Data object that you can easily test and interact with:
Development
To contribute, run:
git clone https://github.com/bkuhlmann/pkce
cd pkce
bin/setup
You can also use the IRB console for direct access to all objects:
bin/console
Architecture
The following documents the workflow used to process and build authorization codes.
Tests
To test, run:
bin/rake
Credits
-
Built with Gemsmith.
-
Engineered by Brooke Kuhlmann.