Skip to content
This repository has been archived by the owner on Jun 17, 2024. It is now read-only.

Modified AuthenticationContext #65

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 16 additions & 12 deletions lib/adal/authentication_context.rb
Original file line number Diff line number Diff line change
Expand Up @@ -72,10 +72,11 @@ def initialize(authority_host = Authority::WORLD_WIDE_AUTHORITY,
# @param ClientCredential|ClientAssertion|ClientAssertionCertificate
# An object that validates the client application by adding
# #request_params to the OAuth request.
# @optional String client_secret
# @return TokenResponse
def acquire_token_for_client(resource, client_cred)
def acquire_token_for_client(resource, client_cred, options = {})
fail_if_arguments_nil(resource, client_cred)
token_request_for(client_cred).get_for_client(resource)
token_request_for(client_cred, options).get_for_client(resource)
end

##
Expand All @@ -91,11 +92,12 @@ def acquire_token_for_client(resource, client_cred)
# #request_params to the OAuth request.
# @optional String resource
# The resource being requested.
# @optional String client_secret
# @return TokenResponse
def acquire_token_with_authorization_code(
auth_code, redirect_uri, client_cred, resource = nil)
auth_code, redirect_uri, client_cred, resource = nil, options = {})
fail_if_arguments_nil(auth_code, redirect_uri, client_cred)
token_request_for(client_cred)
token_request_for(client_cred, options)
.get_with_authorization_code(auth_code, redirect_uri, resource)
end

Expand All @@ -109,11 +111,12 @@ def acquire_token_with_authorization_code(
# depending on the OAuth flow. This object must support #request_params.
# @optional String resource
# The resource being requested.
# @optional String client_secret
# @return TokenResponse
def acquire_token_with_refresh_token(
refresh_token, client_cred, resource = nil)
refresh_token, client_cred, resource = nil, options = {})
fail_if_arguments_nil(refresh_token, client_cred)
token_request_for(client_cred)
token_request_for(client_cred, options)
.get_with_refresh_token(refresh_token, resource)
end

Expand Down Expand Up @@ -143,10 +146,11 @@ def acquire_token_with_refresh_token(
# @param UserAssertion|UserCredential|UserIdentifier
# An object that validates the client that the requested access token is
# for. See the description above of the various flows.
# @optional String client_secret
# @return TokenResponse
def acquire_token_for_user(resource, client_cred, user)
def acquire_token_for_user(resource, client_cred, user, options = {})
fail_if_arguments_nil(resource, client_cred, user)
token_request_for(client_cred)
token_request_for(client_cred, options)
.get_with_user_credential(user, resource)
end

Expand Down Expand Up @@ -187,13 +191,13 @@ def correlation_id=(value)

# Helper function for creating token requests based on client credentials
# and the current authentication context.
def token_request_for(client_cred)
TokenRequest.new(@authority, wrap_client_cred(client_cred), @token_cache)
def token_request_for(client_cred, options = {})
TokenRequest.new(@authority, wrap_client_cred(client_cred, options), @token_cache)
end

def wrap_client_cred(client_cred)
def wrap_client_cred(client_cred, options = {})
if client_cred.is_a? String
ClientCredential.new(client_cred)
ClientCredential.new(client_cred, options[:client_secret])
else
client_cred
end
Expand Down
5 changes: 3 additions & 2 deletions samples/user_credentials_example/app.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@
AUTHORITY_HOST = ADAL::Authority::WORLD_WIDE_AUTHORITY
CLIENT_ID = 'your clientid here'
RESOURCE = 'https://graph.windows.net'
TENANT = 'your tenant here.onmicrosoft.com'
TENANT = 'your tenant here'
CLIENT_SECRET = 'your clientsecret here'

def prompt(*args)
print(*args)
Expand All @@ -40,7 +41,7 @@ def prompt(*args)

user_cred = ADAL::UserCredential.new(username, password)
ctx = ADAL::AuthenticationContext.new(AUTHORITY_HOST, TENANT)
result = ctx.acquire_token_for_user(RESOURCE, CLIENT_ID, user_cred)
result = ctx.acquire_token_for_user(RESOURCE, CLIENT_ID, user_cred, { ADAL::RequestParameters::CLIENT_SECRET => CLIENT_SECRET })

case result
when ADAL::SuccessResponse
Expand Down