Coder Social home page Coder Social logo

valence-sdk-dotnet-restsharp's Introduction

dotnet-sdk-restsharp

Authenticator class for integrating the Valence Auth SDK with

valence-sdk-dotnet-restsharp's People

Contributors

dependabot[bot] avatar j3parker avatar klhuff avatar

Stargazers

 avatar

Watchers

 avatar  avatar  avatar  avatar

valence-sdk-dotnet-restsharp's Issues

ValenceAuthenticator overwrites request.Resource

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.

Public repo has Wiki enabled

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

Public repo has Wiki enabled

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

ArgumentOutOfRangeException when using both UrlSegment and QueryString parameters

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.

Public repo has Wiki enabled

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

Public repo has Wiki enabled

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

Need a .Net Standard version of this package

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.

Public repo has Wiki enabled

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

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.