Esempio n. 1
0
        public static HttpResponseMessage Run([HttpTrigger(AuthorizationLevel.Anonymous, "post", Route = "TrackEventFeatureFlag")] HttpRequestMessage req, ExecutionContext context, TraceWriter log)
        {
            try
            {
                telemetry.Context.Operation.Id   = context.InvocationId.ToString();
                telemetry.Context.Operation.Name = "TrackEventFeatureFlag";
                var startTime = DateTime.Now;
                var timer     = System.Diagnostics.Stopwatch.StartNew();

                var data       = req.Content.ReadAsStringAsync().Result; //Gettings parameters in Body request
                var formValues = data.Split('&')
                                 .Select(value => value.Split('='))
                                 .ToDictionary(pair => Uri.UnescapeDataString(pair[0]).Replace("+", " "),
                                               pair => Uri.UnescapeDataString(pair[1]).Replace("+", " "));

                #region display log for debug
                log.Info(data); //for debug
                #endregion

                var account            = formValues["account"];
                var appSettingExtCert  = formValues["appsettingextcert"]; //"RollUpBoard_ExtensionCertificate"
                var launchDarklySDKkey = formValues["ldkey"];
                var customEvent        = formValues["customevent"];

                //get the token passed in the header request
                string issuedToken = Helpers.GetUserTokenInRequest(req);

                #region display log for debug
                log.Info(issuedToken);
                #endregion

                string extcert     = Helpers.GetExtCertificatEnvName(appSettingExtCert, Helpers.GetHeaderValue(req, "api-version"));
                var    tokenuserId = CheckVSTSToken.checkTokenValidity(issuedToken, extcert); //Check the token, and compare with the VSTS UserId
                if (tokenuserId != null)
                {
                    LdClient ldClient = new LdClient(launchDarklySDKkey);
                    User     user     = new User(tokenuserId + ":" + account);
                    ldClient.Track(customEvent, user, string.Empty);
                    return(req.CreateResponse(HttpStatusCode.OK, "The custom event had be successfuly tracked"));
                }
                else
                {
                    telemetry.TrackTrace("The token is not valid");
                    return(req.CreateResponse(HttpStatusCode.Unauthorized, "The token is not valid"));
                }
            }
            catch (Exception ex)
            {
                telemetry.TrackException(ex);
                return(req.CreateResponse(HttpStatusCode.InternalServerError, ex));
            }
        }
Esempio n. 2
0
        public static HttpResponseMessage Run([HttpTrigger(AuthorizationLevel.Anonymous, "post", Route = "GetHashKey")] HttpRequestMessage req, ExecutionContext context, TraceWriter log)
        {
            try
            {
                telemetry.Context.Operation.Id   = context.InvocationId.ToString();
                telemetry.Context.Operation.Name = "GetHashKey";
                int apiversion = Helpers.GetHeaderValue(req, "api-version");


                var data       = req.Content.ReadAsStringAsync().Result; //Gettings parameters in Body request
                var formValues = data.Split('&')
                                 .Select(value => value.Split('='))
                                 .ToDictionary(pair => Uri.UnescapeDataString(pair[0]).Replace("+", " "),
                                               pair => Uri.UnescapeDataString(pair[1]).Replace("+", " "));

                #region display log for debug
                log.Info(data); //for debug
                #endregion

                var account           = formValues["account"];
                var appSettingExtCert = (apiversion >= 3) ? formValues["appsettingextcert"] : string.Empty;

                string issuedToken = Helpers.GetUserTokenInRequest(req);

                #region display log for debug
                log.Info(issuedToken);
                #endregion
                string extcert     = Helpers.GetExtCertificatEnvName(appSettingExtCert, apiversion, 3);
                var    tokenuserId = CheckVSTSToken.checkTokenValidity(issuedToken, extcert); //Check the token, and compare with the VSTS UserId
                if (tokenuserId != null)
                {
                    string hash = LaunchDarklyServices.GetHashKey(tokenuserId + ":" + account); //hash the User Key
                    return(req.CreateResponse(HttpStatusCode.OK, hash));                        //return the hash key
                }
                else
                {
                    telemetry.TrackTrace("The token is not valid");
                    return(req.CreateResponse(HttpStatusCode.Unauthorized, "The token is not valid"));
                }
            }
            catch (Exception ex)
            {
                telemetry.TrackException(ex);
                return(req.CreateResponse(HttpStatusCode.InternalServerError, ex));
            }
        }
        public static string TokenIsValid(HttpRequestMessage req, bool useKeyVault, string appSettingExtCert, string ExtCertKey, TraceWriter log)
        {
            string issuedToken = Helpers.GetUserTokenInRequest(req);

            var tokenuserId = string.Empty;

            if (useKeyVault)
            {
                string extCert = Helpers.GetKeyVaultSecretValue(ExtCertKey, log);
                tokenuserId = CheckVSTSToken.checkTokenValidityV2(issuedToken, extCert);
            }
            else
            {
                string extcert = appSettingExtCert;
                tokenuserId = CheckVSTSToken.checkTokenValidity(issuedToken, extcert);
            }

            return(tokenuserId);
        }
        public static async Task <HttpResponseMessage> Run([HttpTrigger(AuthorizationLevel.Anonymous, "post", Route = "GetUserFeatureFlag")] HttpRequestMessage req, ExecutionContext context, TraceWriter log)
        {
            try
            {
                telemetry.Context.Operation.Id   = context.InvocationId.ToString();
                telemetry.Context.Operation.Name = "GetUserFeatureFlag";
                var startTime = DateTime.Now;
                var timer     = System.Diagnostics.Stopwatch.StartNew();

                int apiversion = Helpers.GetHeaderValue(req, "api-version");

                var data = await req.Content.ReadAsStringAsync(); //Gettings parameters in Body request

                var formValues = data.Split('&')
                                 .Select(value => value.Split('='))
                                 .ToDictionary(pair => Uri.UnescapeDataString(pair[0]).Replace("+", " "),
                                               pair => Uri.UnescapeDataString(pair[1]).Replace("+", " "));

                #region display log for debug
                log.Info(data); //for debug
                #endregion

                var    account            = formValues["account"];
                var    appSettingExtCert  = formValues["appsettingextcert"]; //"RollUpBoard_ExtensionCertificate"
                string launchDarklySDKkey = (apiversion == 1) ? formValues["ldkey"] : string.Empty;
                string LDproject          = (apiversion >= 2) ? formValues["ldproject"] : "roll-up-board";
                string LDenv = (apiversion >= 2) ? formValues["ldenv"] : "production";



                //get the token passed in the header request
                string issuedToken = Helpers.GetUserTokenInRequest(req);


                string extcert     = Helpers.GetExtCertificatEnvName(appSettingExtCert, apiversion);
                var    tokenuserId = CheckVSTSToken.checkTokenValidity(issuedToken, extcert); //Check the token, and compare with the VSTS UserId
                if (tokenuserId != null)
                {
                    var userkey = LaunchDarklyServices.FormatUserKey(tokenuserId, account);
                    Dictionary <string, bool> userFlags = new Dictionary <string, bool>();

                    // LD SDK performance review
                    if (apiversion == 2)
                    {
                        LaunchDarklyServices.GetUserFeatureFlags(_ldclient, userkey, ref userFlags);
                    }
                    else
                    {
                        userFlags = await LaunchDarklyServices.GetUserFeatureFlagsv1(LDproject, LDenv, userkey);
                    }
                    if (userFlags != null)
                    {
                        return(req.CreateResponse(HttpStatusCode.OK, userFlags)); //return the users flags
                    }
                    else
                    {
                        telemetry.TrackTrace("flags is null");
                        return(req.CreateResponse(HttpStatusCode.InternalServerError, "User flags is null"));
                    }
                }
                else
                {
                    telemetry.TrackTrace("The token is not valid");
                    return(req.CreateResponse(HttpStatusCode.Unauthorized, "The token is not valid"));
                }
            }
            catch (Exception ex)
            {
                telemetry.TrackException(ex);
                return(req.CreateResponse(HttpStatusCode.InternalServerError, ex));
            }
        }