Skip to content

Smaller implementation #8

@stoivo

Description

@stoivo

Hi, I wanted to really understand how PKCE worked. While doing it I changed the code. I want to submit my proposal here. I don't think you want to change the projectet as it seam "old". Just want to leave it here in case someone want a newer take on it.

Thanks for the initial project. Great inspiration

# https://github.com/samuelralak/pkce-challenge-ruby/
  module PkceChallenge
    # Generate code challenge and verfiier
    #
    # Example:
    #   >> PkceChallenge.challenge
    #   => #<PkceChallenge::Challenge:0x00007f894f810378 @char_length=48, @code_verifier="QbS08cDO9pce~HVCKe9-UDiJoBMG8xwql4FI.Y3CIdpyJtPU", @code_challenge="HT90mmypkXgneRUVK-Ja009VvnoL-flydbEgRcTp5Yw">
    #
    # == Parameters:
    # options::
    #   A Hash containing optional arguments. Supported options
    #   can include `:length`
    #
    # == Returns:
    # An instance of PkceChallenge::Challenge
    #
    def self.challenge(options = {})
      PkceChallenge::Challenge.new(options)
    end

    module Types
      include Dry::Types()
    end

    class Challenge
      attr_reader :verifier, :code_challenge

      Schema = Types::Hash.schema(length?: Dry::Types['integer'].constrained(gteq: 43, lteq: 128).default(48))

      def initialize(options = {})
        @options = Schema.call(options)

        @verifier ||= SecureRandom.alphanumeric(@options[:length]).length
        @code_challenge ||= Digest::SHA256.digest(@verifier).then {Base64.urlsafe_encode64(_1, padding: false)}
      end
    end
  end

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions