Exemplo n.º 1
0
        public string GetAuthToken()
        {
            try
            {
                var authHead = $"{ClientId}:{ClientSecret}".EncodeToBase64();

                var token = ActionRunner.CallRestApi(
                    AuthServerUrl,
                    "POST",
                    new Dictionary <string, string>
                {
                    { "cache-control", "no-cache" },
                    { "authorization", $"Basic {authHead}" },
                    { "content-type", "application/x-www-form-urlencoded" },
                    { "taskScheduler-token", GetType().GUID.ToString() }
                },
                    null,
                    new Dictionary <string, string>
                {
                    { "application/x-www-form-urlencoded", $"grant_type={AuthGrantType}&scope={AuthScope}" }
                },
                    null);

                var tokenKey = "access_token";
                if (token?.Contains(tokenKey) != true)
                {
                    return(token);
                }
                var iAccTok = token.IndexOf("{", StringComparison.OrdinalIgnoreCase);
                token = token.Substring(iAccTok, token.Length - iAccTok);
                token = token.Trim();
                var tokenObj = (JObject)JsonConvert.DeserializeObject(token);

                return(tokenObj[tokenKey].ToString(Formatting.None).Replace("\"", ""));
            }
            catch (Exception ex)
            {
                Logger.Fatal(ex, $"Can not give any authentication token from {AuthServerUrl}");
                return(null);
            }
        }
Exemplo n.º 2
0
        public override SystemNotification Send(string receiver, string message, string subject)
        {
            try
            {
                Logger.Info($"Notifying call rest api: {receiver}");
                if (receiver.StartsWith("POST", true, CultureInfo.CurrentCulture))
                {
                    var authToken = GetAuthToken();
                    var url       = GetValidUrl(receiver);
                    var result    = ActionRunner.CallRestApi(
                        url,
                        "POST",
                        new Dictionary <string, string>
                    {
                        { "cache-control", "no-cache" },
                        { "content-type", "application/x-www-form-urlencoded" },
                        { "taskScheduler-token", GetType().GUID.ToString() }
                    },
                        new Dictionary <string, string>
                    {
                        { "message", message },
                        { "subject", subject }
                    },
                        new Dictionary <string, string>
                    {
                        { "application/x-www-form-urlencoded", $"token={authToken}" }
                    },
                        null);

                    Logger.Info($"Post rest api successfully to: {url}");
                    return(SystemNotification.SuccessfullyDone);
                }
                else // GET
                {
                    var url    = GetValidUrl(receiver);
                    var result = ActionRunner.CallRestApi(
                        url,
                        "GET",
                        new Dictionary <string, string>
                    {
                        { "cache-control", "no-cache" },
                        { "taskScheduler-token", GetType().GUID.ToString() }
                    },
                        new Dictionary <string, string>
                    {
                        { "message", message },
                        { "subject", subject }
                    },
                        null,
                        null);

                    Logger.Info($"Get rest api successfully to: {url}");
                    return(SystemNotification.SuccessfullyDone);
                }
            }
            catch (Exception ex)
            {
                Logger.Fatal(ex, $"Notify call api failed for address: {receiver}");
                return(SystemNotification.InternalError);
            }
        }