brightspace / valence-sdk-dotnet-restsharp Goto Github PK
View Code? Open in Web Editor NEWAuthenticator class for integrating the Valence Auth SDK with
License: Apache License 2.0
Authenticator class for integrating the Valence Auth SDK with
License: Apache License 2.0
We have code that relies on D2L.Extensibility.AuthSdk.Restsharp and that in turn relies on D2L.Extensibility.AuthSdk.
Is it possible to create a .Net Standard version nuget package for D2L.Extensibility.AuthSdk.Restsharp? because we also use RestSharp in a .Net Standard version and we get all kinds of concflicts.
The Wiki feature is enabled on this public repository.
This issue is informational only; the Wiki feature will be disabled and this issue will be closed momentarily.
For more information, see Brightspace/repo-policy-automation#32
in ValenceAuthenticator.Authenticate, we overwrite request.Resource with the value from the uri that is computed by RestClient.BuildUri. This means we overwrite any url arg placeholders with their values. So the following code would break:
...
var request = new RestRequest( "/foo/{bar}" );
request.AddUrlSegment( "bar", "123" );
authenticator.Authenticate( request );
// execute request...
request.AddUrlSegment( "bar", "456" ); // won't work: {bar} is gone
authenticator.Authenticate( request );
// execute this new request...
It seems like RestSharp wants to allow this functionality but it is probably uncommon.
I am getting an exception in ValenceAuthenticator.Authenticate when I attempt to make a Valence API call that uses both UrlSegment and QueryString parameters. The following code should be able to reproduce it:
public void ApiTest() {
string LMSUrl = "lms.valence.desire2learn.com";
string BaseUrl = "https://" + LMSUrl;
string AppId = "<AppId>";
string AppKey = "<AppKey>";
string UserId = "<UserId>";
string UserKey = "<UserKey>";
/// Set up the context variables
var AppContext = new D2LAppContextFactory().Create(AppId, AppKey);
var UserContext = AppContext.CreateUserContext(UserId, UserKey, new HostSpec("https", LMSUrl, 443));
/// Create the rest client populated with a valence authenticator
var Client = new RestClient(BaseUrl) { Authenticator = new ValenceAuthenticator(UserContext) };
/// Populate the version dictionary -- this works so I know I am calling the API correctly and am using the right Ids/Keys
var ProductVersions = Client.Execute<List<ProductVersion>>(new RestRequest("/d2l/api/versions/")).Data.ToDictionary(
p => p.ProductCode, p => p.LatestVersion
);
/// Perform a whoami call -- this works so I know my url segment version parameter works
var WhoamiRequest = new RestRequest("/d2l/api/lp/{version}/users/whoami");
WhoamiRequest.AddUrlSegment("version", ProductVersions["lp"]);
var WhoamiResponse = Client.Execute<WhoAmIUser>(WhoamiRequest);
/// Perform a user lookup call -- this fails with an ArgumentOutOfRangeException on the Client.Execute call
var UsernameRequest = new RestRequest("/d2l/api/lp/{version}/users/");
UsernameRequest.AddUrlSegment("version", ProductVersions["lp"]);
UsernameRequest.AddParameter("userName", "username");
var UsernameResponse = Client.Execute<UserData>(UsernameRequest);
}
public class ProductVersion {
public string ProductCode { get; set; }
public string LatestVersion { get; set; }
public List<string> SupportedVersions { get; set; }
}
public class WhoAmIUser {
public string Identifier { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string UniqueName { get; set; }
public string ProfileIdentifier { get; set; }
}
public class UserData {
public int OrgId { get; set; }
public int UserId { get; set; }
public string FirstName { get; set; }
public string MiddleName { get; set; }
public string LastName { get; set; }
public string UserName { get; set; }
public string ExternalEmail { get; set; }
public string OrgDefinedId { get; set; }
public string UniqueIdentifier { get; set; }
public UserActivationData Activation { get; set; }
}
public class UserActivationData {
public bool IsActive { get; set; }
}
The problem appears to come from the second IndexOf call in ValenceAuthenticator.Authenticate which searches for the original request.Resource in a string that has already had the UrlSegment parameter replaced. This causes the IndexOf to return a -1 since it cannot find a match, and that -1 in turn causes the Substring to throw an exception.
Here is my local fix:
public void Authenticate( IRestClient client, IRestRequest request ) {
var uri = client.BuildUri( request );
string method = AdaptMethod( request.Method );
var tokens = m_context.CreateAuthenticatedTokens(uri, method);
var authQueryParameters = CreateAuthQueryString( tokens );
string url = uri.ToString();
// manually set the resource url to work around RestSharp not letting you add query parameters
// once you've added a body to the HTTP request
bool hasQueryParameters = url.IndexOf( '?' ) != -1;
if( hasQueryParameters ) {
var resource = uri.PathAndQuery;
var index = resource.IndexOf( uri.AbsolutePath, System.StringComparison.Ordinal );
// need to trim starting resource
request.Resource = resource.Substring( index, resource.Length - index );
request.Resource += "&" + authQueryParameters;
request.Parameters.Clear();
} else {
request.Resource += "?" + authQueryParameters;
}
}
Specifically, it replaces request.Resource with uri.AbsolutePath. I don't know that this is the correct fix, but when I make the change in my test code the API calls start working again.
The Wiki feature is enabled on this public repository.
This issue is informational only; the Wiki feature will be disabled and this issue will be closed momentarily.
For more information, see Brightspace/repo-policy-automation#32
The Wiki feature is enabled on this public repository.
This issue is informational only; the Wiki feature will be disabled and this issue will be closed momentarily.
For more information, see Brightspace/repo-policy-automation#32
The Wiki feature is enabled on this public repository.
This issue is informational only; the Wiki feature will be disabled and this issue will be closed momentarily.
For more information, see Brightspace/repo-policy-automation#32
The Wiki feature is enabled on this public repository.
This issue is informational only; the Wiki feature will be disabled and this issue will be closed momentarily.
For more information, see Brightspace/repo-policy-automation#32
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.