Skip to content

Commit e53f8ec

Browse files
committed
Add okta auth method and tests
1 parent 02f0532 commit e53f8ec

2 files changed

Lines changed: 33 additions & 1 deletion

File tree

lib/vault/api/auth.rb

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -267,6 +267,28 @@ def gcp(role, jwt, path = 'gcp')
267267
return secret
268268
end
269269

270+
# Authenticate via the okta authentication method. If authentication
271+
# is successful, the resulting token will be stored on the client and used
272+
# for future requests.
273+
#
274+
# @example
275+
# Vault.auth.okta("sethvargo", "s3kr3t") #=> #<Vault::Secret lease_id="">
276+
#
277+
# @param [String] username
278+
# @param [String] password
279+
# @param [Hash] options
280+
# additional options to pass to the authentication call, such as a custom
281+
# mount point
282+
#
283+
# @return [Secret]
284+
def okta(username, password, options = {})
285+
payload = { password: password }.merge(options)
286+
json = client.post("/v1/auth/okta/login/#{encode_path(username)}", JSON.fast_generate(payload))
287+
secret = Secret.decode(json)
288+
client.token = secret.auth.client_token
289+
return secret
290+
end
291+
270292
# Authenticate via a TLS authentication method. If authentication is
271293
# successful, the resulting token will be stored on the client and used
272294
# for future requests.

spec/unit/auth_spec.rb

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,17 @@
22

33
module Vault
44
describe Authenticate do
5-
let(:auth) { Authenticate.new(client: nil) }
5+
let(:client) { double('client') }
6+
let(:auth) { Authenticate.new(client) }
7+
8+
describe '#okta' do
9+
it 'authenticates with Okta auth method' do
10+
allow(client).to receive(:post).with('/v1/auth/okta/login/user1', {password: 'secure'}.to_json) { {auth: {client_token: 'abcd-1234'}} }
11+
allow(client).to receive(:token=)
12+
expect(auth.okta('user1', 'secure').auth.client_token).to eq('abcd-1234')
13+
end
14+
end
15+
616
describe "#region_from_sts_endpoint" do
717
subject { auth.send(:region_from_sts_endpoint, sts_endpoint) }
818

0 commit comments

Comments
 (0)