Path: blob/master/spec/integration/msfmcpd/messagepack_auth_flow_spec.rb
70330 views
# frozen_string_literal: true12require 'msf/core/mcp'3require 'webmock/rspec'45RSpec.describe 'MessagePack Authentication Flow Integration' do6# Disable real HTTP connections for integration tests7before(:all) do8WebMock.disable_net_connect!(allow_localhost: false)9end1011after(:all) do12WebMock.allow_net_connect!13end1415let(:host) { 'localhost' }16let(:port) { 55553 }17let(:endpoint) { '/api/' }18let(:user) { 'test_user' }19let(:password) { 'test_password' }20let(:api_url) { "https://#{host}:#{port}#{endpoint}" }2122describe 'Successful Authentication' do23it 'authenticates with username and password' do24# Stub authentication endpoint25stub_request(:post, api_url)26.with(body: ['auth.login', user, password].to_msgpack)27.to_return(28status: 200,29body: { 'result' => 'success', 'token' => 'test_token_12345' }.to_msgpack,30headers: { 'Content-Type' => 'binary/message-pack' }31)3233client = Msf::MCP::Metasploit::MessagePackClient.new(34host: host,35port: port,36endpoint: endpoint37)3839token = client.authenticate(user, password)40expect(token).to eq('test_token_12345')41end42end4344describe 'Token Reuse' do45it 'stores token for subsequent API calls' do46# Stub authentication endpoint47stub_request(:post, api_url)48.with(body: ['auth.login', user, password].to_msgpack)49.to_return(50status: 200,51body: { 'result' => 'success', 'token' => 'test_token_12345' }.to_msgpack,52headers: { 'Content-Type' => 'binary/message-pack' }53)5455# Stub subsequent API call with token56stub_request(:post, api_url)57.with(body: ['module.search', 'test_token_12345', 'smb'].to_msgpack)58.to_return(59status: 200,60body: [].to_msgpack,61headers: { 'Content-Type' => 'binary/message-pack' }62)6364client = Msf::MCP::Metasploit::MessagePackClient.new(65host: host,66port: port,67endpoint: endpoint68)6970stored_token = client.authenticate(user, password)7172# Subsequent request should use the stored token73client.call_api('module.search', ['smb'])7475# Token should still be the same76expect(client.instance_variable_get(:@token)).to eq(stored_token)77expect(stored_token).to eq('test_token_12345')78end79end80end818283