-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[DI-1383] - Adds basic authentication. (#132)
* Adds basic authentication. * Trying to fix tests. * code simplification and renames
- Loading branch information
1 parent
32f4a8b
commit 9f8c5a1
Showing
21 changed files
with
199 additions
and
98 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
// SPDX-License-Identifier: Apache-2.0 | ||
// Licensed to the Ed-Fi Alliance under one or more agreements. | ||
// The Ed-Fi Alliance licenses this file to you under the Apache License, Version 2.0. | ||
// See the LICENSE and NOTICES files in the project root for more information. | ||
|
||
using System; | ||
using System.Net; | ||
using System.Security.Authentication; | ||
using DataImport.Models; | ||
using RestSharp; | ||
using static DataImport.Common.Encryption; | ||
|
||
namespace DataImport.Common.Helpers | ||
{ | ||
public abstract class AuthRequestWrapper | ||
{ | ||
public virtual RestClientOptions GetOptions(Uri tokenUrl) | ||
{ | ||
RestClientOptions options; | ||
|
||
if (ScriptExtensions.IgnoresCertificateErrors()) | ||
{ | ||
#pragma warning disable S4830 | ||
options = new RestClientOptions(tokenUrl.GetLeftPart(UriPartial.Authority)) | ||
{ | ||
RemoteCertificateValidationCallback = (sender, certificate, chain, sslPolicyErrors) => true | ||
}; | ||
#pragma warning restore S4830 | ||
} | ||
else | ||
{ | ||
options = new RestClientOptions(tokenUrl.GetLeftPart(UriPartial.Authority)); | ||
} | ||
|
||
return options; | ||
} | ||
|
||
public virtual string GetAccessCode(ApiServer apiServer, string encryptionKey) | ||
{ | ||
var authUrl = new Uri(apiServer.AuthUrl); | ||
var authClient = new RestClient(authUrl.GetLeftPart(UriPartial.Authority)); | ||
|
||
var accessCodeRequest = new RestRequest(authUrl.AbsolutePath, Method.Post); | ||
var apiServerKey = !string.IsNullOrEmpty(encryptionKey) | ||
? Decrypt(apiServer.Key, encryptionKey) | ||
: apiServer.Key; | ||
accessCodeRequest.AddParameter("Client_id", apiServerKey); | ||
accessCodeRequest.AddParameter("Response_type", "code"); | ||
|
||
var accessCodeResponse = authClient.Execute<AccessCodeResponse>(accessCodeRequest); | ||
|
||
if (accessCodeResponse.StatusCode != HttpStatusCode.OK) | ||
throw new AuthenticationException("Unable to retrieve an authorization code. Error message: " + | ||
accessCodeResponse.ErrorMessage); | ||
if (accessCodeResponse.Data.Error != null) | ||
throw new AuthenticationException( | ||
"Unable to retrieve an authorization code. Please verify that your application key is correct. Alternately, the service address may not be correct: " + | ||
authUrl); | ||
|
||
return accessCodeResponse.Data.Code; | ||
} | ||
|
||
public virtual string GetToken(RestRequest tokenRequest, RestClient oauthClient) | ||
{ | ||
var tokenResponse = oauthClient.Execute<BearerTokenResponse>(tokenRequest); | ||
if (tokenResponse.StatusCode != HttpStatusCode.OK) | ||
throw new AuthenticationException("Unable to retrieve an access token. Error message: " + | ||
tokenResponse.ErrorMessage); | ||
|
||
if (tokenResponse.Data.Error != null || tokenResponse.Data.TokenType != "bearer") | ||
throw new AuthenticationException( | ||
"Unable to retrieve an access token. Please verify that your application secret is correct."); | ||
|
||
return tokenResponse.Data.AccessToken; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
// SPDX-License-Identifier: Apache-2.0 | ||
// Licensed to the Ed-Fi Alliance under one or more agreements. | ||
// The Ed-Fi Alliance licenses this file to you under the Apache License, Version 2.0. | ||
// See the LICENSE and NOTICES files in the project root for more information. | ||
|
||
using System; | ||
using System.Text; | ||
using DataImport.Models; | ||
using RestSharp; | ||
using static DataImport.Common.Encryption; | ||
|
||
namespace DataImport.Common.Helpers | ||
{ | ||
public class BasicAuthRequestWrapper : AuthRequestWrapper, IAuthRequestWrapper | ||
{ | ||
public string GetToken(ApiServer apiServer, string encryptionKey) | ||
{ | ||
return GetToken(apiServer, encryptionKey, null); | ||
} | ||
|
||
public string GetToken(ApiServer apiServer, string encryptionKey, string accessCode) | ||
{ | ||
var tokenUrl = new Uri(apiServer.TokenUrl); | ||
RestClientOptions options = GetOptions(tokenUrl); | ||
|
||
var authClient = new RestClient(options); | ||
|
||
var tokenRequest = new RestRequest(tokenUrl.AbsolutePath, Method.Post); | ||
|
||
var apiServerKey = !string.IsNullOrEmpty(encryptionKey) | ||
? Decrypt(apiServer.Key, encryptionKey) | ||
: apiServer.Key; | ||
var apiServerSecret = !string.IsNullOrEmpty(encryptionKey) | ||
? Decrypt(apiServer.Secret, encryptionKey) | ||
: apiServer.Secret; | ||
|
||
var keySecretBytes = Encoding.UTF8.GetBytes($"{apiServerKey}:{apiServerSecret}"); | ||
tokenRequest.AddHeader("Authorization", $"Basic {Convert.ToBase64String(keySecretBytes)}"); | ||
|
||
if (accessCode != null) | ||
{ | ||
tokenRequest.AddParameter("code", accessCode); | ||
tokenRequest.AddParameter("grant_type", "authorization_code"); | ||
} | ||
else | ||
{ | ||
tokenRequest.AddParameter("grant_type", "client_credentials"); | ||
} | ||
|
||
return GetToken(tokenRequest, authClient); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
// SPDX-License-Identifier: Apache-2.0 | ||
// Licensed to the Ed-Fi Alliance under one or more agreements. | ||
// The Ed-Fi Alliance licenses this file to you under the Apache License, Version 2.0. | ||
// See the LICENSE and NOTICES files in the project root for more information. | ||
|
||
using DataImport.Models; | ||
|
||
namespace DataImport.Common.Helpers | ||
{ | ||
public interface IAuthRequestWrapper | ||
{ | ||
string GetAccessCode(ApiServer apiServer, string encryptionKey); | ||
|
||
string GetToken(ApiServer apiServer, string encryptionKey, string accessCode); | ||
|
||
string GetToken(ApiServer apiServer, string encryptionKey); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.