pkce_challenge 0.2.0 → 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: b24f70ed4dc880603fc1bb11a17a5c2c576d8810ec0abb9b1b7ae36fbeef46e2
4
- data.tar.gz: 75b4d4b4e347b0ead4b2acfc4190b90f5767cafcd16c87186170758764749a74
3
+ metadata.gz: 7ecf2db55010f4dbfc0a506b8a28a6f79e61a3417b6b68d183c52a20379857a3
4
+ data.tar.gz: 01ea88cbc4da0fa3d07b037ad890ae634d9b3a91650f9709fcbff8cbd48fb130
5
5
  SHA512:
6
- metadata.gz: ee179537017917cc647efb5f683200d66319e783ad73a3575ff3071cf6278ee3286641f6f7896d4eedd0615bda64b0d370c672bc86e0e35f3f162966027049f9
7
- data.tar.gz: 663f440155dede9a1a406b6cd96ed7f4de900413b94c5008f759a8c3ddbbe1016e8f621884bd0e55f6aeae52dcf32765dba07f3d5a95c6d465e533ea674f24fb
6
+ metadata.gz: ac7c704a12cfc3df964510a305f7977ccc915b3b7ccbac061d4dbd29355a1b313fe12879aefa823bed8e4e0c57851fb4ffd88682550477c2eaae6958c1f9eab8
7
+ data.tar.gz: e7b70a852a3431520befeadeec22b682401ed5492bf5da69a887da02744e9a45d8fc49485874c77d976125693a8e4e9401ab71666710d8ca9e9f5061f4252055
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- pkce_challenge (0.1.0)
4
+ pkce_challenge (1.0.0)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
data/README.md CHANGED
@@ -1,8 +1,6 @@
1
- # PkceChallenge
1
+ # PKCE Code challenge generator
2
2
 
3
- Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/pkce_challenge`. To experiment with that code, run `bin/console` for an interactive prompt.
4
-
5
- TODO: Delete this and the text above, and describe your gem
3
+ PKCE flow code verifier and code challenge **generator**
6
4
 
7
5
  ## Installation
8
6
 
@@ -22,29 +20,30 @@ Or install it yourself as:
22
20
 
23
21
  ## Usage
24
22
 
23
+ To generate the code verifier and challenge simply run:
24
+
25
25
  ```ruby
26
- 2.6.0 :001 > require "pkce_challenge"
26
+ PkceChallenge.challenge
27
27
  ```
28
28
 
29
+ Or with the char length option:
30
+
29
31
  ```ruby
30
- 2.6.0 :001 > PkceChallenge.challenge
32
+ PkceChallenge.challenge(char_length: 128)
31
33
  ```
32
34
 
33
- Output:
35
+ Additionally you can run:
34
36
  ```ruby
35
- => #<PkceChallenge::Challenge:0x00007f894f810378 @char_length=48, @code_verifier="QbS08cDO9pce~HVCKe9-UDiJoBMG8xwql4FI.Y3CIdpyJtPU", @code_challenge="HT90mmypkXgneRUVK-Ja009VvnoL-flydbEgRcTp5Yw">
37
+ pkce_challenge = PkceChallenge.challenge
38
+ pkce_challenge = PkceChallenge.challenge(char_length: 128) # or with char_length option
36
39
  ```
37
40
 
38
- Generating a code challenge with the `char_length` option:
41
+ Returned value is an instance of PkceChallenge::Challenge class which will have the following methods:
39
42
  ```ruby
40
- # Accepts valid length between 43 and 128
41
-
42
- 2.6.0 :003 > PkceChallenge.challenge(char_length: 50)
43
-
44
- => #<PkceChallenge::Challenge:0x00007f894f20b9a0 @char_length=50, @code_verifier="0St0oCRzGDFj_iIxB8PCwaMPvGLk8vQxDfJIoC_zU56XwwQM21", @code_challenge="01g9RYPZOjpVFB-BBQhd6OC70jlgPFzJ6ie5YkSC7rI">
43
+ pkce_challenge.code_verifier # a dynamically created cryptographically random key
44
+ pkce_challenge.code_challenge # a BASE64-URL-encoded string of the SHA256 hash of the code verifier
45
45
  ```
46
46
 
47
-
48
47
  ## Development
49
48
 
50
49
  After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
@@ -53,7 +52,7 @@ To install this gem onto your local machine, run `bundle exec rake install`. To
53
52
 
54
53
  ## Contributing
55
54
 
56
- Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/pkce_challenge. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
55
+ Bug reports and pull requests are welcome on GitHub at https://github.com/samuelralak/pkce_challenge. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
57
56
 
58
57
  ## License
59
58
 
@@ -1,34 +1,20 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require "digest"
4
+ require "securerandom"
4
5
 
5
6
  module PkceChallenge
6
7
  class Challenge
7
8
  attr_accessor :code_verifier, :code_challenge
8
9
 
9
10
  def initialize(options = {})
10
- @char_length = (options[:char_length] || CHAR_LENGTH[:default]).to_i
11
-
12
- unless @char_length.between?(CHAR_LENGTH[:min], CHAR_LENGTH[:max])
13
- raise PkceChallenge::LengthOutOfRangeError
14
- end
15
-
11
+ @options = options
16
12
  @code_verifier = generate_code_verifier
17
13
  @code_challenge = generate_pkce_challenge
18
14
  end
19
15
 
20
16
  # constants definition
21
17
 
22
- CHAR_RANGE = [
23
- *"A".."Z",
24
- *"a".."z",
25
- *"0".."9",
26
- "-",
27
- ".",
28
- "_",
29
- "~",
30
- ].freeze
31
-
32
18
  CHAR_LENGTH = {
33
19
  default: 48,
34
20
  max: 128,
@@ -38,11 +24,21 @@ module PkceChallenge
38
24
  private
39
25
 
40
26
  def generate_code_verifier
41
- Array.new(@char_length) { CHAR_RANGE[rand(CHAR_RANGE.length)] }.join
27
+ length = (@options[:char_length] || CHAR_LENGTH[:default]).to_i
28
+
29
+ unless length.between?(CHAR_LENGTH[:min], CHAR_LENGTH[:max])
30
+ raise PkceChallenge::LengthOutOfRangeError
31
+ end
32
+
33
+ urlsafe_base64(SecureRandom.base64((length * 3) / 4))
42
34
  end
43
35
 
44
36
  def generate_pkce_challenge
45
- Digest::SHA256.base64digest(@code_verifier).tr("+/", "-_").tr("=", "")
37
+ urlsafe_base64(Digest::SHA256.base64digest(@code_verifier))
38
+ end
39
+
40
+ def urlsafe_base64(base64_str)
41
+ base64_str.tr("+/", "-_").tr("=", "")
46
42
  end
47
43
  end
48
44
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module PkceChallenge
4
- VERSION = "0.2.0"
4
+ VERSION = "1.0.0"
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: pkce_challenge
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 1.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Samuel Ralak
@@ -86,6 +86,8 @@ files:
86
86
  - bin/console
87
87
  - bin/setup
88
88
  - gemfiles/pkce_challenge-0.1.0.gem
89
+ - gemfiles/pkce_challenge-0.2.0.gem
90
+ - gemfiles/pkce_challenge-1.0.0.gem
89
91
  - lib/pkce_challenge.rb
90
92
  - lib/pkce_challenge/challenge.rb
91
93
  - lib/pkce_challenge/version.rb