Ejemplo n.º 1
0
        /// <summary>
        /// Create and Update a new userProfile and return the AuthToken that can be used for subsequent web api calls for that userProfile
        /// The passed in http client will be updated with relevant AuthToken (same as being returned)
        /// </summary>
        /// <param name="client"></param>
        /// <param name="callerMemberName"></param>
        /// <param name="assemblyFilePath"></param>
        /// <returns></returns>
        public async Task <string> CreateDevUserProfile(HttpClient client, [CallerMemberName] string callerMemberName = null, [CallerFilePath] string assemblyFilePath = null)
        {
            //Add clientKey token to header
            BaseWebApiTest.SetAuthToken(client, ClientKey, false);

            //Create Dev user
            var newEmail = $"Test-{assemblyFilePath.ExGetFileNameFromAssemblyPath()}.{callerMemberName}@lifecouple.net";
            var userProfileRequestInfo = new UserProfileRequestInfo {
                Email = newEmail
            };
            var postCreateUserResponse = await client.PostAsync($"api/userprofiles/devuser", BaseWebApiTest.GetJsonContent(userProfileRequestInfo));

            postCreateUserResponse.EnsureSuccessStatusCode();

            //Create Token
            var tokenRequestInfo = new TokenRequestInfo {
                Email = newEmail, Password = newEmail.Split('@')[0] + "@"
            };
            var postResponse = await client.PostAsync($"api/tokens", BaseWebApiTest.GetJsonContent(tokenRequestInfo));

            postResponse.EnsureSuccessStatusCode();
            var tokenResponseInfo = BaseWebApiTest.Deserialize <TokenResponseInfo>(await postResponse.Content.ReadAsStringAsync());

            //Add token to header
            BaseWebApiTest.SetAuthToken(client, tokenResponseInfo.Token, true);

            // Set User profile
            var reqPayload = new UserProfileRegAboutYouRequestInfo
            {
                DateOfBirth        = DateTime.Now.AddYears(-20),
                FirstName          = "Tester",
                LastName           = "Testlastname",
                Gender             = "m",
                MobilePhone        = "7605006125",
                NotificationOption = "True"
            };

            postResponse = await client.PutAsync(ApiEndpoints.userprofiles_me_registration_aboutyou, BaseWebApiTest.GetJsonContent(reqPayload));

            postResponse.EnsureSuccessStatusCode();

            return(tokenResponseInfo.Token);
        }
Ejemplo n.º 2
0
        public async Task <IActionResult> CreateToken([FromBody] TokenRequestInfo tokenInfo)
        {
            if (ModelState.IsValid)
            {
                //Authenticate credentials...
                if (string.IsNullOrWhiteSpace(tokenInfo?.Email) ||
                    string.IsNullOrWhiteSpace(tokenInfo?.Password) ||
                    (tokenInfo.Email?.Substring(0, tokenInfo.Email.IndexOf("@") + 1) != tokenInfo.Password))    //e.g. [email protected] and per@ are ok as email and password
                {
                    return(this.ApiErrorMessage404NotFound("Unable to create token. Invalid credentials."));
                }

                var r = await _bl.FindUserProfiles_byEmailAsync(tokenInfo.Email, true);

                if (r == null || r.Count == 0)
                {
                    return(this.ApiErrorMessage404NotFound($"Unable to create token. No devTest user with email '{tokenInfo.Email}' found, create user first."));
                }
                if (r.Count != 1)
                {
                    return(this.ApiErrorMessage404NotFound($"Unable to create token. Found more than one devTest user with '{tokenInfo.Email}'."));
                }

                var userProfile = r.First();


                var jwtToken = _jwtHandler.Create(userProfile.ExternalRefId, userProfile.PrimaryEmail, userProfile.FirstName, userProfile.LastName);
                var results  = new TokenResponseInfo
                {
                    Token      = jwtToken.Token,
                    Expiration = jwtToken.Expires
                };

                return(Created("", results));
            }
            //    }
            //}

            return(BadRequest());
        }