-
Notifications
You must be signed in to change notification settings - Fork 23
new provoder for Daemon type apps. #25
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from 2 commits
d45f189
8c8a164
0c9ddbb
b88d157
9a27511
3db43c1
51fa3a5
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,134 @@ | ||
| using System; | ||
| 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 | ||
|
||
| { | ||
| public AccountSession CurrentAccountSession { get; internal set; } | ||
| string clientId; | ||
| string clientKey; | ||
|
|
||
| public AuthenticationContext authContext; | ||
| ClientCredential clientCredential; | ||
|
||
|
|
||
| // 'applicationId' : Your Application ID | ||
| // 'applicationKey' : Your Application Key | ||
|
||
| // 'tenant' : is usually a domain name for your Office365 service. Like 'yourcompany.onmicrosoft.com' | ||
| public AdalDaemonAuthenticationProvider( | ||
| string applicationId, | ||
| string applicationKey, | ||
| string tenant) | ||
|
||
| { | ||
| clientId = applicationId; | ||
| clientKey = applicationKey; | ||
|
|
||
| string authority = String.Format(CultureInfo.InvariantCulture, "https://login.microsoftonline.com/{0}", tenant); | ||
| authContext = new AuthenticationContext(authority); | ||
|
||
| clientCredential = new ClientCredential(clientId, clientKey); | ||
| } | ||
|
|
||
|
|
||
|
|
||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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); | ||
|
||
| } | ||
| catch (AdalException ex) | ||
| { | ||
| if (ex.ErrorCode == "temporarily_unavailable") | ||
| { | ||
| retry = true; | ||
| retryCount++; | ||
| Thread.Sleep(3000); | ||
|
||
| } | ||
|
|
||
| Console.WriteLine( | ||
|
||
| 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)); | ||
|
||
|
|
||
|
|
||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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( | ||
|
||
| 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); | ||
|
||
| else | ||
| uri.Query = uri.Query.TrimStart('?') + string.Format("&client_secret={0}", clientKey); | ||
| request.RequestUri = uri.Uri; | ||
|
|
||
|
|
||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) | ||
|
||
| { | ||
| 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, | ||
| }; | ||
| } | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Add the license