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 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289
|
# frozen_string_literal: true
require "openssl"
require "jwt"
RSpec.describe OAuth2::Strategy::Assertion do
let(:client_assertion) { client.assertion }
let(:client) do
cli = OAuth2::Client.new("abc", "def", site: "http://api.example.com", auth_scheme: auth_scheme)
cli.connection = Faraday.new(cli.site, cli.options[:connection_opts]) do |b|
b.request :url_encoded
b.adapter :test do |stub|
stub.post("/oauth/token") do |token_request|
@request_body = Rack::Utils.parse_nested_query(token_request.body).transform_keys(&:to_sym)
case @response_format
when "formencoded"
[200, {"Content-Type" => "application/x-www-form-urlencoded"}, "expires_in=600&access_token=salmon&refresh_token=trout"]
when "json"
[200, {"Content-Type" => "application/json"}, '{"expires_in":600,"access_token":"salmon","refresh_token":"trout"}']
else
raise "Please define @response_format to choose a response content type!"
end
end
end
end
cli
end
let(:auth_scheme) { :request_body }
describe "#authorize_url" do
it "raises NotImplementedError" do
expect { client_assertion.authorize_url }.to raise_error(NotImplementedError)
end
end
describe "#get_token" do
let(:algorithm) { "HS256" }
let(:key) { "arowana" }
let(:timestamp) { Time.now.to_i }
let(:claims) do
{
iss: "carp@example.com",
scope: "https://oauth.example.com/auth/flounder",
aud: "https://sturgeon.example.com/oauth2/token",
exp: timestamp + 3600,
iat: timestamp,
sub: "12345",
custom_claim: "ling cod",
}
end
before do
@response_format = "json"
end
describe "assembling a JWT assertion" do
let(:jwt) do
payload, header = JWT.decode(@request_body[:assertion], key, true, algorithm: algorithm)
{payload: payload, header: header}
end
let(:payload) { jwt[:payload] }
let(:header) { jwt[:header] }
shared_examples_for "encodes the JWT" do
it "indicates algorithm in the header" do
expect(header).not_to be_nil
expect(header["alg"]).to eq(algorithm)
end
it "has claims" do
expect(payload).not_to be_nil
expect(payload.keys).to match_array(%w[iss scope aud exp iat sub custom_claim])
payload.each do |key, claim|
expect(claims[key.to_sym]).to eq(claim)
end
end
end
context "when encoding as HS256" do
let(:algorithm) { "HS256" }
let(:key) { "super_secret!" }
before do
client_assertion.get_token(claims, algorithm: algorithm, key: key)
raise "No request made!" if @request_body.nil?
end
it_behaves_like "encodes the JWT"
context "with real key" do
let(:key) { "1883be842495c3b58f68ca71fbf1397fbb9ed2fdf8990f8404a25d0a1b995943" }
it_behaves_like "encodes the JWT"
end
end
context "when encoding as RS256" do
let(:algorithm) { "RS256" }
let(:key) { OpenSSL::PKey::RSA.new(2048) }
before do
client_assertion.get_token(claims, algorithm: algorithm, key: key)
raise "No request made!" if @request_body.nil?
end
it_behaves_like "encodes the JWT"
context "with private key" do
let(:private_key_file) { "spec/fixtures/RS256/jwtRS256.key" }
let(:password) { "" }
let(:key) { OpenSSL::PKey::RSA.new(File.read(private_key_file), password) }
it_behaves_like "encodes the JWT"
end
end
context "with bad encoding params" do
let(:encoding_opts) { {algorithm: algorithm, key: key} }
describe "non-supported algorithms" do
let(:algorithm) { "the blockchain" }
let(:key) { "machine learning" }
it "raises JWT::EncodeError" do
# this behavior is handled by the JWT gem, but this should make sure it is consistent
# On old Ruby (versions 2.4 and below) the error raised was different because
# an old version (< v2.4) of the jwt gem gets installed.
if defined?(JWT::VERSION::STRING) && Gem::Version.create(JWT::VERSION::STRING) >= Gem::Version.create("2.4")
expect { client_assertion.get_token(claims, encoding_opts) }.to raise_error(JWT::EncodeError, "Unsupported signing method")
else
expect { client_assertion.get_token(claims, encoding_opts) }.to raise_error(NotImplementedError)
end
end
end
describe "of a wrong object type" do
let(:encoding_opts) { "the cloud" }
it "raises ArgumentError" do
expect { client_assertion.get_token(claims, encoding_opts) }.to raise_error(ArgumentError, /encoding_opts/)
end
end
describe "missing encoding_opts[:algorithm]" do
before do
encoding_opts.delete(:algorithm)
end
it "raises ArgumentError" do
expect { client_assertion.get_token(claims, encoding_opts) }.to raise_error(ArgumentError, /encoding_opts/)
end
end
describe "missing encoding_opts[:key]" do
before do
encoding_opts.delete(:key)
end
it "raises ArgumentError" do
expect { client_assertion.get_token(claims, encoding_opts) }.to raise_error(ArgumentError, /encoding_opts/)
end
end
context "when including a Key ID (kid)" do
let(:algorithm) { "HS256" }
let(:key) { "new_secret_key" }
let(:kid) { "my_super_secure_key_id_123" }
before do
client_assertion.get_token(claims, algorithm: algorithm, key: key, kid: kid)
raise "No request made!" if @request_body.nil?
end
it_behaves_like "encodes the JWT"
it "includes the kid in the JWT header" do
expect(header).not_to be_nil
expect(header["kid"]).to eq(kid)
end
end
end
end
describe "POST request parameters" do
context "when using :auth_scheme => :request_body" do
let(:auth_scheme) { :request_body }
it "includes assertion and grant_type, along with the client parameters" do
client_assertion.get_token(claims, algorithm: algorithm, key: key)
expect(@request_body).not_to be_nil
expect(@request_body.keys).to match_array(%i[assertion grant_type client_id client_secret])
expect(@request_body[:grant_type]).to eq("urn:ietf:params:oauth:grant-type:jwt-bearer")
expect(@request_body[:assertion]).to be_a(String)
expect(@request_body[:client_id]).to eq("abc")
expect(@request_body[:client_secret]).to eq("def")
end
it "includes other params via request_options" do
client_assertion.get_token(claims, {algorithm: algorithm, key: key}, {scope: "dover sole"})
expect(@request_body).not_to be_nil
expect(@request_body.keys).to match_array(%i[assertion grant_type scope client_id client_secret])
expect(@request_body[:grant_type]).to eq("urn:ietf:params:oauth:grant-type:jwt-bearer")
expect(@request_body[:assertion]).to be_a(String)
expect(@request_body[:scope]).to eq("dover sole")
expect(@request_body[:client_id]).to eq("abc")
expect(@request_body[:client_secret]).to eq("def")
end
end
context "when using :auth_scheme => :basic_auth" do
let(:auth_scheme) { :basic_auth }
it "includes assertion and grant_type by default" do
client_assertion.get_token(claims, algorithm: algorithm, key: key)
expect(@request_body).not_to be_nil
expect(@request_body.keys).to match_array(%i[assertion grant_type])
expect(@request_body[:grant_type]).to eq("urn:ietf:params:oauth:grant-type:jwt-bearer")
expect(@request_body[:assertion]).to be_a(String)
end
it "includes other params via request_options" do
client_assertion.get_token(claims, {algorithm: algorithm, key: key}, {scope: "dover sole"})
expect(@request_body).not_to be_nil
expect(@request_body.keys).to match_array(%i[assertion grant_type scope])
expect(@request_body[:grant_type]).to eq("urn:ietf:params:oauth:grant-type:jwt-bearer")
expect(@request_body[:assertion]).to be_a(String)
expect(@request_body[:scope]).to eq("dover sole")
end
end
end
describe "returning the response" do
let(:access_token) { client_assertion.get_token(claims, {algorithm: algorithm, key: key}, {}, response_opts) }
let(:response_opts) { {} }
%w[json formencoded].each do |mode|
context "when the content type is #{mode}" do
before do
@response_format = mode
end
it "returns an AccessToken" do
expect(access_token).to be_an(OAuth2::AccessToken)
end
it "returns AccessToken with same Client" do
expect(access_token.client).to eq(client)
end
it "returns AccessToken with #token" do
expect(access_token.token).to eq("salmon")
end
it "returns AccessToken with #expires_in" do
expect(access_token.expires_in).to eq(600)
end
it "returns AccessToken with #expires_at" do
expect(access_token.expires_at).not_to be_nil
end
it "sets AccessToken#refresh_token to nil" do
expect(access_token.refresh_token).to eq("trout")
end
context "with custom response_opts" do
let(:response_opts) { {"custom_token_option" => "mackerel"} }
it "passes them into the token params" do
expect(access_token.params).to eq(response_opts)
end
end
context "when no custom opts are passed in" do
let(:response_opts) { {} }
it "does not set any params by default" do
expect(access_token.params).to eq({})
end
end
end
end
end
end
end
|