Example #1
0
 private async Task InitUserIfNeeded()
 {
     if (CurrentUser == null)
     {
         CurrentUser = await Rest.Auth.Test();
     }
 }
        private string GetTokenLabel(AuthTestResponse response)
        {
            if (!string.IsNullOrEmpty(response.BotId))
            {
                return($"Bot token (id: {response.BotId})");
            }

            return("Token");
        }
        private string BuildAuthTestResponseMessage(AuthTestResponse response)
        {
            string tokenLabel = GetTokenLabel(response);

            string isEnterprise = response.IsEnterpriseInstall
                ? "is"
                : "is not";

            return($"{tokenLabel} was authenticated to channel '{response.Url}', " +
                   $" team '{response.Team}' (id: {response.TeamId}), " +
                   $" user '{response.User}' (id: {response.UserId}). " +
                   $" The token {isEnterprise} associated with an enterprise installation.");
        }
Example #4
0
        public static SlackServer MockDefaultAuthTest(this SlackServer server, AuthTestResponse response)
        {
            server.HttpServer.Given(Request.Create().WithPath(FlurlAuthClient.AUTH_TEST_PATH)
                                    .UsingGet(WireMock.Matchers.MatchBehaviour.AcceptOnMatch))
            .RespondWith(Response.Create().WithCallback(request =>
            {
                return(new WireMock.ResponseMessage()
                {
                    StatusCode = 200,
                    BodyData = new WireMock.Util.BodyData()
                    {
                        DetectedBodyType = WireMock.Util.BodyType.Json,
                        BodyAsJson = response
                    }
                });
            }));

            return(server);
        }
        protected override string IsValidDynamicHelper(ref string fingerprintText,
                                                       ref string message)
        {
            var fingerprint = new Fingerprint(fingerprintText);

            var client = new WebClient();
            var data   = new NameValueCollection();

            data["token"] = fingerprint.Key;

            byte[] bytes = client.UploadValues("https://slack.com/api/auth.test",
                                               "POST",
                                               data);

            string json = Encoding.UTF8.GetString(bytes);

            if (json.Contains("invalid_auth"))
            {
                return(nameof(ValidationState.Unauthorized));
            }

            AuthTestResponse response = JsonSerializer.Deserialize <AuthTestResponse>(json);

            switch (response.Error)
            {
            case "token_revoked": { return(nameof(ValidationState.Expired)); }

            case "invalid_auth": { return(nameof(ValidationState.Unauthorized)); }
            }

            if (!string.IsNullOrEmpty(response.Error))
            {
                message = $"An unexpected error was observed " +
                          "attempting to validate token: '{response.Error}'";
                return(nameof(ValidationState.Unknown));
            }

            message = BuildAuthTestResponseMessage(response);
            return(nameof(ValidationState.Authorized));
        }
Example #6
0
        private async Task <string> GetUserIdBasedOnToken()
        {
            AuthTestResponse auth = await SlackClient.TestAuthAsync();

            return(auth.user_id);
        }