public async Task <IActionResult> PostFacebook(string token) { var facebook = new FacebookToken(); var user = await facebook.GetUserFromFacebookAsync(token); return(Ok(user)); }
internal FacebookOAuthData(FacebookToken token, FacebookUser user) { AuthenticatedAt = EssentialsTime.UtcNow; AccessToken = token.AccessToken; ExpiresIn = token.ExpiresIn; User = user; }
public async Task <Guid> RegisterUserFromFacebookAsync(FacebookToken facebookTokenResource) { if (string.IsNullOrEmpty(facebookTokenResource.Token)) { throw new ValidationException(); } var facebookUser = await _facebookService.GetUserFromFacebookAsync(facebookTokenResource.Token); if (facebookUser == null) { throw new RemoteEntityNotFoundException($"User not found with FacebookToken: {facebookTokenResource.Token}"); } var user = await _userRepository.GetByFacebookIdAsync(facebookUser.FacebookId); if (user != null) { throw new EntityExistsException($"User with FacebookToken: {facebookTokenResource.Token} already exists."); } return(await _userRepository.AddAsync(new User { Email = facebookUser.Email, FirstName = facebookUser.FirstName, LastName = facebookUser.LastName, FacebookId = facebookUser.FacebookId, Birthday = facebookUser.Birthdate })); }
public async Task <IActionResult> FacebookLogin([FromBody] FacebookToken facebookToken) { var httpClient = httpClientFactory.CreateClient(); httpClient.BaseAddress = new Uri("https://graph.facebook.com/v4.0/"); var response = await httpClient.GetAsync($"me?access_token={facebookToken.Token}&fields=id,email,first_name,last_name"); if (!response.IsSuccessStatusCode) { logger.Error("Error. Response was not success!"); return(Conflict()); } ; var result = await response.Content.ReadAsStringAsync(); var facebookAccount = JsonConvert.DeserializeObject <FacebookAccount>(result); var user = authenticateService.FacebookLogin(facebookAccount); if (user == null) { var model = mapper.Map <RegisterModel>(facebookAccount); var userRegister = mapper.Map <User>(model); authenticateService.RegisterFacebook(userRegister); user = authenticateService.FacebookLogin(facebookAccount); } return(UserResult(user)); }
/// <inheritdoc /> /// <summary> /// Link facebook account Links the current user account to a facebook account, using the acccess token from facebook. Can also be used to update the access token after it has expired. <br><br><b>Permissions Needed:</b> Non-facebook user token /// </summary> /// <param name="facebookToken">The token from facebook</param> public void LinkAccounts(FacebookToken facebookToken) { mWebCallEvent.WebPath = "/social/facebook/users"; if (!string.IsNullOrEmpty(mWebCallEvent.WebPath)) { mWebCallEvent.WebPath = mWebCallEvent.WebPath.Replace("{format}", "json"); } mWebCallEvent.HeaderParams.Clear(); mWebCallEvent.QueryParams.Clear(); mWebCallEvent.AuthSettings.Clear(); mWebCallEvent.PostBody = null; mWebCallEvent.PostBody = KnetikClient.Serialize(facebookToken); // http body (model) parameter // authentication settings mWebCallEvent.AuthSettings.Add("oauth2_client_credentials_grant"); // authentication settings mWebCallEvent.AuthSettings.Add("oauth2_password_grant"); // make the HTTP request mLinkAccountsStartTime = DateTime.Now; mWebCallEvent.Context = mLinkAccountsResponseContext; mWebCallEvent.RequestType = KnetikRequestType.POST; KnetikLogger.LogRequest(mLinkAccountsStartTime, "LinkAccounts", "Sending server request..."); KnetikGlobalEventSystem.Publish(mWebCallEvent); }
public HttpResponseMessage Post(FacebookTokenModel model) { string token; try { token = _facebookService.ExtendToken(model.Token); } catch (BadRequestException e) { ModelState.AddModelError("Token", e.Message); return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, ModelState)); } var response = new FacebookToken { Token = token }; return(Request.CreateResponse(HttpStatusCode.OK, response)); }
public async Task RegisterUserFromFacebookAsync_WhenFacebookUserExists_ShouldAddUserToDatabase() { //Arrange var facebookToken = new FacebookToken { Token = "abc" }; var facebookUser = new FacebookUser { FacebookId = "123", Birthdate = DateTime.Now, Email = "email", FirstName = "Jan", LastName = "Kowalski", }; var userId = Guid.NewGuid(); _facebookServiceMock.Setup(x => x.GetUserFromFacebookAsync(facebookToken.Token)).ReturnsAsync(facebookUser); _userRepositoryMock.Setup(x => x.GetByFacebookIdAsync(facebookUser.FacebookId)).ReturnsAsync((User)null); _userRepositoryMock.Setup(x => x.AddAsync(It.IsAny <User>())).ReturnsAsync(userId); //Act var result = await _sut.RegisterUserFromFacebookAsync(facebookToken); //Assert result.Should().Be(userId); }
public async Task <IActionResult> RegisterUserByFacebookTokenAsync([FromBody] FacebookToken facebookToken) { try { var userId = await _accountService.RegisterUserFromFacebookAsync(facebookToken); return(Created("/users", userId)); } catch (RemoteEntityNotFoundException ex) { _logger.LogInformation(ex.Message); return(Unauthorized(ex.Message)); } catch (EntityExistsException ex) { _logger.LogInformation(ex.Message); return(Conflict("Entity already exists.")); } catch (Exception ex) { _logger.LogError(ex.Message, ex); return(BadRequest(ex.Message)); } }
/// <summary> /// Link facebook account Links the current user account to a facebook account, using the acccess token from facebook. Can also be used to update the access token after it has expired. <br><br><b>Permissions Needed:</b> Non-facebook user token /// </summary> /// <exception cref="com.knetikcloud.Client.ApiException">Thrown when fails to make API call</exception> /// <param name="facebookToken">The token from facebook (optional)</param> /// <returns>ApiResponse of Object(void)</returns> public ApiResponse <Object> LinkAccountsWithHttpInfo(FacebookToken facebookToken = null) { var localVarPath = "/social/facebook/users"; var localVarPathParams = new Dictionary <String, String>(); var localVarQueryParams = new List <KeyValuePair <String, String> >(); var localVarHeaderParams = new Dictionary <String, String>(Configuration.DefaultHeader); var localVarFormParams = new Dictionary <String, String>(); var localVarFileParams = new Dictionary <String, FileParameter>(); Object localVarPostBody = null; // to determine the Content-Type header String[] localVarHttpContentTypes = new String[] { "application/json" }; String localVarHttpContentType = Configuration.ApiClient.SelectHeaderContentType(localVarHttpContentTypes); // to determine the Accept header String[] localVarHttpHeaderAccepts = new String[] { "application/json" }; String localVarHttpHeaderAccept = Configuration.ApiClient.SelectHeaderAccept(localVarHttpHeaderAccepts); if (localVarHttpHeaderAccept != null) { localVarHeaderParams.Add("Accept", localVarHttpHeaderAccept); } if (facebookToken != null && facebookToken.GetType() != typeof(byte[])) { localVarPostBody = Configuration.ApiClient.Serialize(facebookToken); // http body (model) parameter } else { localVarPostBody = facebookToken; // byte array } // authentication (oauth2_client_credentials_grant) required // oauth required if (!String.IsNullOrEmpty(Configuration.AccessToken)) { localVarHeaderParams["Authorization"] = "Bearer " + Configuration.AccessToken; } // authentication (oauth2_password_grant) required // oauth required if (!String.IsNullOrEmpty(Configuration.AccessToken)) { localVarHeaderParams["Authorization"] = "Bearer " + Configuration.AccessToken; } // make the HTTP request IRestResponse localVarResponse = (IRestResponse)Configuration.ApiClient.CallApi(localVarPath, Method.POST, localVarQueryParams, localVarPostBody, localVarHeaderParams, localVarFormParams, localVarFileParams, localVarPathParams, localVarHttpContentType); int localVarStatusCode = (int)localVarResponse.StatusCode; if (ExceptionFactory != null) { Exception exception = ExceptionFactory("LinkAccounts", localVarResponse); if (exception != null) { throw exception; } } return(new ApiResponse <Object>(localVarStatusCode, localVarResponse.Headers.ToDictionary(x => x.Name, x => x.Value.ToString()), null)); }
/// <summary> /// Link facebook account Links the current user account to a facebook account, using the acccess token from facebook. Can also be used to update the access token after it has expired. <br><br><b>Permissions Needed:</b> Non-facebook user token /// </summary> /// <exception cref="com.knetikcloud.Client.ApiException">Thrown when fails to make API call</exception> /// <param name="facebookToken">The token from facebook (optional)</param> /// <returns></returns> public void LinkAccounts(FacebookToken facebookToken = null) { LinkAccountsWithHttpInfo(facebookToken); }