Ejemplo n.º 1
0
        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));
        }
Ejemplo n.º 2
0
        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));
        }
Ejemplo n.º 3
0
        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));
        }
Ejemplo n.º 4
0
        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));
        }
Ejemplo n.º 5
0
        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));
        }
Ejemplo n.º 6
0
 public Connection(
     HostAddress hostAddress,
     Octokit.User user,
     ScopesCollection scopes)
 {
     HostAddress = hostAddress;
     this.user   = user;
     this.scopes = scopes;
     isLoggedIn  = true;
 }
Ejemplo n.º 7
0
 /// <summary>
 /// Initializes a new instance of the <see cref="LoginResult"/> class.
 /// </summary>
 /// <param name="user">The logged-in user.</param>
 /// <param name="scopes">The login scopes.</param>
 public LoginResult(User user, ScopesCollection scopes)
 {
     User   = user;
     Scopes = scopes;
 }