public void Matches_Returns_False_When_Missing_Scopes() { var required = new[] { "user", "repo", "gist", "write:public_key" }; var target = new ScopesCollection(new[] { "user", "repo", "write:public_key" }); Assert.False(target.Matches(required)); }
public void Returns_True_When_Admin_Scope_Returned_Instead_Of_Write() { var required = new[] { "user", "repo", "gist", "write:public_key" }; var target = new ScopesCollection(new[] { "user", "repo", "gist", "foo", "admin:public_key" }); Assert.True(target.Matches(required)); }
async Task <LoginResult> GetUserAndCheckScopes(IGitHubClient client) { var response = await client.Connection.Get <User>( UserEndpoint, null, null).ConfigureAwait(false); if (response.HttpResponse.Headers.ContainsKey(ScopesHeader)) { var returnedScopes = new ScopesCollection(response.HttpResponse.Headers[ScopesHeader] .Split(',') .Select(x => x.Trim()) .ToArray()); if (returnedScopes.Matches(minimumScopes)) { return(new LoginResult(response.Body, returnedScopes)); } else { log.Error("Incorrect API scopes: require {RequiredScopes} but got {Scopes}", minimumScopes, returnedScopes); } } else { log.Error("Error reading scopes: /user succeeded but scopes header was not present"); } throw new IncorrectScopesException( "Incorrect API scopes. Required: " + string.Join(",", minimumScopes)); }
public void Returns_True_When_Extra_Scopes_Returned() { var required = new[] { "user", "repo", "gist", "write:public_key" }; var target = new ScopesCollection(new[] { "user", "repo", "gist", "foo", "write:public_key" }); Assert.True(target.Matches(required)); }
async Task <LoginResult> GetUserAndCheckScopes(IGitHubClient client) { var response = await client.Connection.Get <User>( UserEndpoint, null, null).ConfigureAwait(false); var scopes = response.HttpResponse.Headers .Where(h => string.Equals(h.Key, ScopesHeader, StringComparison.OrdinalIgnoreCase)) .Select(h => h.Value) .FirstOrDefault(); if (scopes != null) { var returnedScopes = new ScopesCollection(scopes .Split(',') .Select(x => x.Trim()) .ToArray()); if (returnedScopes.Matches(minimumScopes)) { return(new LoginResult(response.Body, returnedScopes)); } else { log.Error("Incorrect API scopes: require {RequiredScopes} but got {Scopes}", minimumScopes, returnedScopes); } } else { log.Error("Error reading scopes: /user succeeded but scopes header was not present"); } throw new IncorrectScopesException( "Incorrect API scopes. Required: " + string.Join(",", minimumScopes)); }