Skip to content

Commit 8db3d11

Browse files
authored
feature: add jwt bearer support (#105)
1 parent 85077a4 commit 8db3d11

File tree

2 files changed

+58
-0
lines changed

2 files changed

+58
-0
lines changed

lib/uaa/token_issuer.rb

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -328,6 +328,15 @@ def refresh_token_grant(refresh_token, scope = nil)
328328
request_token(grant_type: 'refresh_token', refresh_token: refresh_token, scope: scope)
329329
end
330330

331+
# Gets an access token with the user assertion used for authentication
332+
# via the jwt bearer authorization grant.
333+
# See {http://tools.ietf.org/html/rfc7523#section-2.1}.
334+
# @param assertion should be an id_token from a previous IdP token request
335+
# @return [TokenInfo]
336+
def jwt_bearer_grant(assertion, scope = nil, client_assertion = nil)
337+
request_token(grant_type: 'urn:ietf:params:oauth:grant-type:jwt-bearer', assertion: assertion, scope: scope, client_assertion: client_assertion)
338+
end
339+
331340
end
332341

333342
end

spec/token_issuer_spec.rb

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -470,6 +470,55 @@ module CF::UAA
470470
end
471471
end
472472

473+
context 'with jwt bearer grant' do
474+
475+
it 'gets a token with jwt bearer' do
476+
subject.set_request_handler do |url, method, body, headers|
477+
headers['content-type'].should =~ /application\/x-www-form-urlencoded/
478+
headers['accept'].should =~ /application\/json/
479+
headers['X-CF-ENCODED-CREDENTIALS'].should == 'true'
480+
headers['authorization'].should == 'Basic dGVzdF9jbGllbnQ6dGVzdCUyMXNlY3JldA=='
481+
url.should == 'http://test.uaa.target/oauth/token'
482+
method.should == :post
483+
reply = {access_token: 'test_access_token', token_type: 'BEARER',
484+
scope: 'openid', expires_in: 98765}
485+
[200, Util.json(reply), {'content-type' => 'application/json'}]
486+
end
487+
token = subject.jwt_bearer_grant('assertion', 'openid')
488+
token.should be_an_instance_of TokenInfo
489+
token.info['access_token'].should == 'test_access_token'
490+
token.info['token_type'].should =~ /^bearer$/i
491+
token.info['scope'].should == 'openid'
492+
token.info['expires_in'].should == 98765
493+
end
494+
495+
context "when client & client secret are nil" do
496+
let(:client_id) { nil }
497+
let(:client_secret) { nil }
498+
499+
it 'does not error' do
500+
subject.set_request_handler do |url, method, body, headers|
501+
headers['content-type'].should =~ /application\/x-www-form-urlencoded/
502+
headers['accept'].should =~ /application\/json/
503+
headers['X-CF-ENCODED-CREDENTIALS'].should == 'true'
504+
headers['authorization'].should == 'Basic Og=='
505+
url.should == 'http://test.uaa.target/oauth/token'
506+
method.should == :post
507+
reply = {access_token: 'test_access_token', token_type: 'BEARER',
508+
scope: 'openid', expires_in: 98765}
509+
[200, Util.json(reply), {'content-type' => 'application/json'}]
510+
end
511+
token = subject.jwt_bearer_grant('assertion', 'openid')
512+
token.should be_an_instance_of TokenInfo
513+
token.info['access_token'].should == 'test_access_token'
514+
token.info['token_type'].should =~ /^bearer$/i
515+
token.info['scope'].should == 'openid'
516+
token.info['expires_in'].should == 98765
517+
end
518+
end
519+
520+
end
521+
473522
end
474523

475524
end

0 commit comments

Comments
 (0)