Skip to content

Commit

Permalink
Merge pull request #25 from omniauth/fix-id_token-parsing
Browse files Browse the repository at this point in the history
Refactor access token rewrite and id_token
  • Loading branch information
BobbyMcWho authored Apr 18, 2022
2 parents 22aead3 + 450a4eb commit b7d530a
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 24 deletions.
2 changes: 1 addition & 1 deletion lib/omniauth-okta/version.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@

module OmniAuth
module Okta
VERSION = '1.0.0'.freeze
VERSION = '2.0.0'.freeze
end
end
24 changes: 9 additions & 15 deletions lib/omniauth/strategies/okta.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,10 @@ class Okta < OmniAuth::Strategies::OAuth2
h[:raw_info] = raw_info unless skip_info?

if access_token
h[:id_token] = access_token.token
h[:id_token] = id_token

if !options[:skip_jwt] && !access_token.token.nil?
h[:id_info] = validated_token(access_token.token)
if !options[:skip_jwt] && !id_token.nil?
h[:id_info] = validated_token(id_token)
end
end
end
Expand All @@ -53,18 +53,6 @@ def client_options
options.fetch(:client_options)
end

alias :oauth2_access_token :access_token

def access_token
if oauth2_access_token
::OAuth2::AccessToken.new(client, oauth2_access_token.token, {
refresh_token: oauth2_access_token.refresh_token,
expires_in: oauth2_access_token.expires_in,
expires_at: oauth2_access_token.expires_at
})
end
end

def raw_info
@_raw_info ||= access_token.get(client_options.fetch(:user_info_url)).parsed || {}
rescue ::Errno::ETIMEDOUT
Expand All @@ -75,6 +63,12 @@ def callback_url
options[:redirect_uri] || (full_host + callback_path)
end

def id_token
return if access_token.nil?

access_token['id_token']
end

# Returns the qualified URL for the authorization server
#
# This is necessary in the case where there is a custom authorization server.
Expand Down
75 changes: 67 additions & 8 deletions spec/omniauth/strategies/okta_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,40 @@

let(:base_url) { 'https://your-org.okta.com' }
let(:strategy) { described_class }
let(:access_token_string) do
"eyJhbGciOiJSUzI1NiJ9.eyJ2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dGh" \
"fdGltZSI6MTQ0OTYyNDAyNiwiYXRfaGFzaCI6ImNwcUtmZFFBNWVIODkxRmY1b0pyX1EifQ.Btw6bUbZhRa89DsBb8KmL9rfhku" \
"--_mbNC2pgC8yu8obJnwO12nFBepui9KzbpJhGM91PqJwi_AylE6rp-ehamfnUAO4JL14PkemF45Pn3u_6KKwxJnxcWxLvMuuis" \
"nvIs7NScKpOAab6ayZU0VL8W6XAijQmnYTtMWQfSuaaR8rYOaWHrffh3OypvDdrQuYacbkT0csxdrayXfBG3UF5-ZAlhfch1fhF" \
"T3yZFdWwzkSDc0BGygfiFyNhCezfyT454wbciSZgrA9ROeHkfPCaX7KCFO8GgQEkGRoQntFBNjluFhNLJIUkEFovEDlfuB4tv_M" \
"8BM75celdy3jkpOurg"
end

let(:okta_response) {
{
"access_token" => access_token_string,
"token_type" => "Bearer",
"expires_in" => 3600,
"scope" => "openid email",
"refresh_token" => "a9VpZDRCeFh3Nkk2VdY",
"id_token" => id_token_string
}
}

let(:access_token) { ::OAuth2::AccessToken.from_hash(subject.client, okta_response) }

describe '#client' do
it 'has default site' do
Expand Down Expand Up @@ -70,21 +104,46 @@
end

describe 'extra' do
before :each do
before do
allow(subject).to receive(:raw_info) { { :foo => 'bar' } }
allow(subject).to receive(:access_token)
.and_return(
instance_double(::OAuth2::AccessToken,
token: nil,
refresh_token: 'token',
expires_in: 0,
expires_at: 0))
allow(subject).to receive(:access_token).and_return(access_token)
end

it { expect(subject.extra[:raw_info]).to eq({ :foo => 'bar' }) }
it { expect(subject.extra[:id_token]).to eq(id_token_string) }
it { expect(subject.extra[:id_info]).to eq(
{
"sub" => "00uid4BxXw6I6TV4m0g3",
"email" => "[email protected]",
"email_verified" => true,
"ver" => 1,
"iss" => "http://rain.okta1.com:1802",
"login" => "[email protected]",
"aud" => "uAaunofWkaDJxukCFeBx",
"iat" => 1449624026,
"exp" => 1449627626,
"amr" => ["pwd"],
"jti" => "4eAWJOCMB3SX8XewDfVR",
"auth_time" => 1449624026,
"at_hash" => "cpqKfdQA5eH891Ff5oJr_Q"}
)}
it { expect(subject.access_token).to_not eq(subject.extra[:id_token]) }
end

describe 'id_token' do
before do
allow(subject).to receive(:access_token).and_return(access_token)
end

it { expect(subject.id_token).to eq(id_token_string) }

context 'when access token is nil' do
before do
allow(subject).to receive(:access_token).and_return(nil)
end

it { expect(subject.id_token).to be_nil }
end
end

describe 'raw_info' do
Expand Down

0 comments on commit b7d530a

Please sign in to comment.