-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpkce
executable file
·66 lines (52 loc) · 1.66 KB
/
pkce
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#!/usr/bin/env ruby
#
# OVERVIEW
# --------
#
# This script generates a code verifier with 256-bit entropy
# (43 characters in base64url) and computes the code challenge
# corresponding to the code verifier.
#
# REFERENCES
# ----------
#
# RFC 7636 Proof Key for Code Exchange by OAuth Public Clients
# https://www.rfc-editor.org/rfc/rfc7636.html
#
require "base64"
require "digest/sha2"
require "securerandom"
#------------------------------------------------------------
# main
#------------------------------------------------------------
def main(args)
# Generate a code verifier.
verifier = base64url_encode(random_bytes(32))
# Generate a code challenge.
challenge = base64url_encode(sha256(verifier))
# Print the generated values.
puts "CODE_VERIFIER=#{verifier}"
puts "CODE_CHALLENGE=#{challenge}"
end
#------------------------------------------------------------
# Generate a random bytes of the specified size.
#------------------------------------------------------------
def random_bytes(size)
SecureRandom.random_bytes(size)
end
#------------------------------------------------------------
# Compute the SHA-256 digest of the input data.
#------------------------------------------------------------
def sha256(input)
Digest::SHA256.digest(input)
end
#------------------------------------------------------------
# Base64url-encode the input data.
#------------------------------------------------------------
def base64url_encode(input)
Base64.urlsafe_encode64(input, padding: false)
end
#------------------------------------------------------------
# Entry Point
#------------------------------------------------------------
main(ARGV)