Skip to content
Open
Show file tree
Hide file tree
Changes from 2 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
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
using System;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add the license

// ------------------------------------------------------------------------------
//  Copyright (c) Microsoft Corporation.  All Rights Reserved.  Licensed under the MIT License.  See License in the project root for license information.
// ------------------------------------------------------------------------------

using System.Threading.Tasks;
using Microsoft.Graph;
using Microsoft.IdentityModel.Clients.ActiveDirectory;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Globalization;
using System.Threading;

namespace Microsoft.OneDrive.Sdk.Authentication.Business
{
public class AdalDaemonAuthenticationProvider : IAuthenticationProvider
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a reason this doesn't extend AdalAuthenticationProviderBase?

{
public AccountSession CurrentAccountSession { get; internal set; }
string clientId;
string clientKey;

public AuthenticationContext authContext;
ClientCredential clientCredential;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

add access modifier, even if it doesn't need to be explicit.


// 'applicationId' : Your Application ID
// 'applicationKey' : Your Application Key
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please use typical class comments like this example in AuthenticationContextWrapper

        /// <summary>
        /// Authenticates the user silently using <see cref="AuthenticationContext.AcquireTokenSilentAsync(string, string, UserIdentifier)"/>.
        /// </summary>
        /// <param name="resource">The resource to authenticate against.</param>
        /// <param name="clientId">The client ID of the application.</param>
        /// <param name="userIdentifier">The <see cref="UserIdentifier"/> of the user.</param>

// 'tenant' : is usually a domain name for your Office365 service. Like 'yourcompany.onmicrosoft.com'
public AdalDaemonAuthenticationProvider(
string applicationId,
string applicationKey,
string tenant)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

string applicationId,
string applicationKey,

Please use clientId and clientSecret like the rest of the library. Change throughout this file.

{
clientId = applicationId;
clientKey = applicationKey;

string authority = String.Format(CultureInfo.InvariantCulture, "https://login.microsoftonline.com/{0}", tenant);
authContext = new AuthenticationContext(authority);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

clientCredential = new ClientCredential(clientId, clientKey);
}



Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Only 1 extra line


public async Task AuthenticateUserAsync(string serviceResourceId)
{
AuthenticationResult result = null;
result = null;
int retryCount = 0;
bool retry = false;

do
{
retry = false;
try
{
// ADAL includes an in memory cache, so this call will only send a message to the server if the cached token is expired.
result = await authContext.AcquireTokenAsync(serviceResourceId, clientCredential);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Again, AuthenticationContextWrapper

}
catch (AdalException ex)
{
if (ex.ErrorCode == "temporarily_unavailable")
{
retry = true;
retryCount++;
Thread.Sleep(3000);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

await Task.Delay(3000);

}

Console.WriteLine(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't be writing to the console in the library. Remove this. If you want to log this stuff, you can log it at the client layer.

String.Format("An error occurred while acquiring a token\nTime: {0}\nError: {1}\nRetry: {2}\n",
DateTime.Now.ToString(),
ex.ToString(),
retry.ToString()));
}

} while ((retry == true) && (retryCount < 3));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

retryCount < 3

Make this a constant at the top of the file instead of an embedded literal.



Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Extra whitespace

this.CurrentAccountSession = this.ConvertAuthenticationResultToAccountSession(result);
}

public async Task AuthenticateRequestAsync(HttpRequestMessage request)
{
if (this.CurrentAccountSession == null)
{
throw new ServiceException(
new Error
{
Code = OAuthConstants.ErrorCodes.AuthenticationFailure,
Message = "Please call one of the AuthenticateUserAsync...() methods to authenticate the user before trying to authenticate a request.",
});
}

if (this.CurrentAccountSession.IsExpiring)
{
throw new ServiceException(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Refresh the token, similar to this.

new Error
{
Code = OAuthConstants.ErrorCodes.AuthenticationFailure,
Message = ""
});
}

var accessTokenType = string.IsNullOrEmpty(this.CurrentAccountSession.AccessTokenType)
? OAuthConstants.Headers.Bearer
: this.CurrentAccountSession.AccessTokenType;

var uri = new UriBuilder(request.RequestUri);
if (string.IsNullOrEmpty(uri.Query))
uri.Query = string.Format("client_secret={0}", clientKey);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add braces around if and else statement blocks

else
uri.Query = uri.Query.TrimStart('?') + string.Format("&client_secret={0}", clientKey);
request.RequestUri = uri.Uri;


Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Extra whitespace

request.Headers.Authorization = new AuthenticationHeaderValue(
accessTokenType,
this.CurrentAccountSession.AccessToken);
}

protected AccountSession ConvertAuthenticationResultToAccountSession(AuthenticationResult authenticationResult)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use the existing implementation in AdalAuthenticationProviderBase

{
if (authenticationResult == null)
{
return null;
}

return new AccountSession
{
AccessToken = authenticationResult.AccessToken,
AccessTokenType = authenticationResult.AccessTokenType,
ClientId = this.clientId,
ExpiresOnUtc = authenticationResult.ExpiresOn,
RefreshToken = authenticationResult.RefreshToken,
UserId = authenticationResult.UserInfo == null ? null : authenticationResult.UserInfo.UniqueId,
};
}
}
}
Loading