-
Notifications
You must be signed in to change notification settings - Fork 0
/
generate-client-attestation-pop
executable file
·162 lines (127 loc) · 3.89 KB
/
generate-client-attestation-pop
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
#!/usr/bin/env ruby
#
# OVERVIEW
# --------
#
# This script geerates a client attestation PoP that complies with
# OAuth 2.0 Attestation-Based Client Authentication.
#
# USAGE
# -----
#
# generate-client-attestation-pop
# --as-id={AUTHORIZATION_SERVER_ID}
# --client-id={CLIENT_ID}
# --client-key={CLIENT_PRIVATE_KEY_FILE}
# --duration={DURATION_IN_SECONDS}
#
require 'bundler/inline'
gemfile do
source 'https://rubygems.org'
gem 'json-jwt'
gem 'optparse'
end
require 'securerandom'
require 'time'
#------------------------------------------------------------
# main
#------------------------------------------------------------
def main(args)
# Process the command line options.
options = Options.process(args)
# Prepare the payload of the client attestation PoP.
payload = build_payload(options)
# Generate a JWS by signing with the client's key.
attestation_pop = sign(payload, options.client_key)
# Write the client attestation PoP to the standard output.
puts attestation_pop
end
#------------------------------------------------------------
# Prepare the payload of the client attestation PoP
#------------------------------------------------------------
def build_payload(options)
# JWT ID
jti = SecureRandom.alphanumeric(16)
# The current time in seconds for time-related claims
now = Time.now.to_i
# Payload of a client attestation PoP
{
iss: options.client_id,
iat: now,
exp: now + options.duration,
jti: jti,
aud: options.as_id
}
end
#------------------------------------------------------------
# Generate a JWS by signing with the client's key.
#------------------------------------------------------------
def sign(payload, jwk)
# Prepare a JWT with the header and the payload.
jwt = JSON::JWT.new(payload)
# Sign the JWT with the key and convert it to JWS.
jwt.sign(jwk).to_s
end
#------------------------------------------------------------
# Command line options
#------------------------------------------------------------
class Options < OptionParser
DESC_AS_ID = "The identifier of the authorization server."
DESC_CLIENT_ID = "The identifier of the client application."
DESC_CLIENT_KEY = "The file containing the private key of the client application in the JWK format."
DESC_DURATION = "The duration of the client attestation PoP in seconds. The default value is 86400."
attr_reader :as_id, :client_id, :client_key, :duration
def initialize
super
@as_id = nil
@client_id = nil
@client_key = nil
@duration = 86400
self.on('--as-id=AS_ID', DESC_AS_ID) do |id|
@as_id = id
end
self.on('--client-id=CLIENT_ID', DESC_CLIENT_ID) do |id|
@client_id = id
end
self.on('--client-key=FILE', DESC_CLIENT_KEY) do |file|
@client_key = read_jwk(file)
end
self.on('--duration=DURATION', DESC_DURATION) do |duration|
@duration = to_integer(duration, '--duration')
end
end
private
def read_jwk(file)
json = File.read(file)
hash = JSON.parse(json, {symbolize_names: true})
JSON::JWK.new(hash)
end
def to_integer(value, option)
begin
return Integer(value)
rescue ArgumentError
raise OptionParser::ParseError.new "The value of the '#{option}' option is not an integer."
end
end
def error_if_missing(value, option)
if value.nil?
raise OptionParser::ParseError.new "'#{option}' is missing."
end
end
public
def verify
error_if_missing(@as_id, '--as-id=AUTHORIZATION_SERVER_ID')
error_if_missing(@client_id, '--client-id=CLIENT_ID')
error_if_missing(@client_key, '--client-key=CLIENT_KEY')
end
def self.process(args)
options = Options.new
options.parse(args)
options.verify()
return options
end
end
#------------------------------------------------------------
# Entry Point
#------------------------------------------------------------
main(ARGV)