예제 #1
0
        public async Task <FormUrlResult> GenerateFormUrl(FormUrlOptions parameters)
        {
            if (parameters == null)
            {
                throw new Exception("parameters not provided");
            }
            if (parameters.formsAppId == null)
            {
                Form form = await oneBlinkApiClient.GetRequest <Form>($"/forms/{parameters.formId}");

                parameters.formsAppId = form.formsAppIds[0];
            }

            if (parameters.formsAppId == null)
            {
                throw new Exception("This form has not been added to a forms app yet.");
            }
            FormsListFormsApp formsApp = await oneBlinkApiClient.GetRequest <FormsListFormsApp>($"/forms-apps/{parameters.formsAppId}");

            string preFillFormDataId = null;

            if (parameters.preFillData != null)
            {
                PrefillClient prefillClient = new PrefillClient(oneBlinkApiClient);
                PreFillMeta   preFillMeta   = await prefillClient.GetPreFillMeta((int)parameters.formId);

                string preFillMetaId = await prefillClient.PutPreFillData <dynamic>(parameters.preFillData, preFillMeta);

                preFillFormDataId = preFillMeta.preFillFormDataId;
            }

            string userToken = null;

            if (parameters.username != null)
            {
                AesUserToken aesUserToken = new AesUserToken(parameters.secret);
                userToken = aesUserToken.encrypt(parameters.username);
            }

            // Default expiry for token is 8 hours
            int    jwtExpiry = parameters.expiryInSeconds ?? 28800;
            string token     = Token.GenerateJSONWebToken(accessKey: oneBlinkApiClient.accessKey, oneBlinkApiClient.secretKey, jwtExpiry);

            string formUrl = _generateFormUrl(
                formId: parameters.formId,
                formsApp: formsApp,
                token: token,
                externalId: parameters.externalId,
                preFillFormDataId: preFillFormDataId,
                userToken: userToken,
                previousFormSubmissionApprovalId: parameters.previousFormSubmissionApprovalId
                );
            string expiry = DateTime.UtcNow.AddSeconds(jwtExpiry).ToString("o");

            return(new FormUrlResult()
            {
                formUrl = formUrl,
                expiry = expiry
            });
        }
예제 #2
0
        public static string DecryptUserToken(string userToken, string secret)
        {
            if (String.IsNullOrEmpty(userToken) || String.IsNullOrEmpty(secret))
            {
                throw new Exception("Must pass a valid userToken and secret");
            }
            AesUserToken aesUserToken = new AesUserToken(secret);

            return(aesUserToken.decrypt(userToken));
        }