-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(Authentication): Update auth methods
BREAKING CHANGE: Auth methods were updated and old authentication methods were removed
- Loading branch information
Showing
35 changed files
with
2,144 additions
and
1,415 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
50 changes: 0 additions & 50 deletions
50
src/IBM.Cloud.SDK.Core/Authentication/AuthenticatorFactory.cs
This file was deleted.
Oops, something went wrong.
53 changes: 0 additions & 53 deletions
53
src/IBM.Cloud.SDK.Core/Authentication/BasicAuth/BasicAuthConfig.cs
This file was deleted.
Oops, something went wrong.
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
92 changes: 92 additions & 0 deletions
92
src/IBM.Cloud.SDK.Core/Authentication/Bearer/BearerTokenAuthenticator.cs
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,92 @@ | ||
/** | ||
* Copyright 2019 IBM Corp. All Rights Reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
* | ||
*/ | ||
|
||
using IBM.Cloud.SDK.Core.Http; | ||
using IBM.Cloud.SDK.Core.Util; | ||
using System; | ||
using System.Collections.Generic; | ||
|
||
namespace IBM.Cloud.SDK.Core.Authentication.Bearer | ||
{ | ||
/// <summary> | ||
/// This class implements support for Bearer Token Authentication. The main purpose of this authenticator is to construct the | ||
/// Authorization header and then add it to each outgoing REST API request. | ||
/// </summary> | ||
public class BearerTokenAuthenticator : Authenticator | ||
{ | ||
/// <summary> | ||
/// The access token configured for this authenticator | ||
/// </summary> | ||
public string BearerToken { get; set; } | ||
|
||
/// <summary> | ||
/// Construct a BearerTokenAuthenticator instance with the specified access token. | ||
/// The token value will be used to construct an Authorization header that will be included | ||
/// in outgoing REST API requests. | ||
/// </summary> | ||
/// <param name="bearerToken">The access token value</param> | ||
public BearerTokenAuthenticator(string bearerToken) | ||
{ | ||
Init(bearerToken); | ||
} | ||
|
||
/// <summary> | ||
/// Construct a BearerTokenAuthenticator using properties retrieved from the specified Map. | ||
/// </summary> | ||
/// <param name="config">Config a map containing the access token value</param> | ||
public BearerTokenAuthenticator(Dictionary<string, string> config) | ||
{ | ||
config.TryGetValue(PropnameBearerToken, out string bearerToken); | ||
Init(bearerToken); | ||
} | ||
|
||
private void Init(string bearerToken) | ||
{ | ||
BearerToken = bearerToken; | ||
|
||
Validate(); | ||
} | ||
|
||
public override string AuthenticationType | ||
{ | ||
get { return AuthtypeBearer; } | ||
} | ||
|
||
/// <summary> | ||
/// This method is called to authenticate an outgoing REST API request. | ||
/// Here, we'll just set the Authorization header to provide the necessary authentication info. | ||
/// </summary> | ||
/// <param name="client"></param> | ||
public override void Authenticate(IClient client) | ||
{ | ||
client.WithAuthentication(BearerToken); | ||
} | ||
|
||
public void Validate() | ||
{ | ||
if (string.IsNullOrEmpty(BearerToken)) | ||
{ | ||
throw new ArgumentNullException(string.Format(ErrormsgPropMissing, "BearerToken")); | ||
} | ||
|
||
if (CredentialUtils.HasBadStartOrEndChar(BearerToken)) | ||
{ | ||
throw new ArgumentException(string.Format(ErrormsgPropInvalid, "BearerToken")); | ||
} | ||
} | ||
} | ||
} |
Oops, something went wrong.