private async Task <ActionResult <TokenResult> > ExternalLoginAsync <TClient>(ExternalAuthInfo authInfo, string appId, string appSecret, Func <IRequestFactory, IClientConfiguration, TClient> createClient) where TClient : OAuth2Client { using (_logger.BeginScope(new ExceptionlessState().Tag("External Login").Property("Auth Info", authInfo).SetHttpContext(HttpContext))) { if (String.IsNullOrEmpty(authInfo?.Code)) { _logger.LogError("External login failed: Unable to get auth info."); return(NotFound()); } if (String.IsNullOrEmpty(appId) || String.IsNullOrEmpty(appSecret)) { return(NotFound()); } var client = createClient(new RequestFactory(), new OAuth2.Configuration.ClientConfiguration { ClientId = appId, ClientSecret = appSecret, RedirectUri = authInfo.RedirectUri }); UserInfo userInfo; try { userInfo = await client.GetUserInfoAsync(authInfo.Code, authInfo.RedirectUri); } catch (Exception ex) { _logger.LogCritical(ex, "External login failed: {Message}", ex.Message); return(BadRequest("Unable to get user info.")); } User user; try { user = await FromExternalLoginAsync(userInfo); } catch (ApplicationException ex) { _logger.LogCritical(ex, "External login failed for {EmailAddress}: {Message}", userInfo.Email, ex.Message); return(BadRequest("Account Creation is currently disabled.")); } catch (Exception ex) { _logger.LogCritical(ex, "External login failed for {EmailAddress}: {Message}", userInfo.Email, ex.Message); return(BadRequest("An error occurred while processing user info.")); } if (user == null) { _logger.LogCritical("External login failed for {EmailAddress}: Unable to process user info.", userInfo.Email); return(BadRequest("Unable to process user info.")); } if (!String.IsNullOrWhiteSpace(authInfo.InviteToken)) { await AddInvitedUserToOrganizationAsync(authInfo.InviteToken, user); } _logger.UserLoggedIn(user.EmailAddress); return(Ok(new TokenResult { Token = await GetOrCreateAccessTokenAsync(user) })); } }
private async Task <IHttpActionResult> ExternalLoginAsync <TClient>(ExternalAuthInfo authInfo, string appId, string appSecret, Func <IRequestFactory, IClientConfiguration, TClient> createClient) where TClient : OAuth2Client { if (String.IsNullOrEmpty(authInfo?.Code)) { _logger.Error().Message("External login failed: Unable to get auth info.").Tag("External Login").Property("Auth Info", authInfo).SetActionContext(ActionContext).Write(); return(NotFound()); } if (String.IsNullOrEmpty(appId) || String.IsNullOrEmpty(appSecret)) { return(NotFound()); } var client = createClient(new RequestFactory(), new RuntimeClientConfiguration { ClientId = appId, ClientSecret = appSecret, RedirectUri = authInfo.RedirectUri }); UserInfo userInfo; try { userInfo = client.GetUserInfo(authInfo.Code); } catch (Exception ex) { _logger.Error().Exception(ex).Critical().Message("External login failed: {0}", ex.Message).Tag("External Login", client.Name).Property("Auth Info", authInfo).SetActionContext(ActionContext).Write(); return(BadRequest("Unable to get user info.")); } User user; try { user = await FromExternalLoginAsync(userInfo); } catch (ApplicationException ex) { _logger.Error().Exception(ex).Critical().Message("External login failed for \"{0}\": {1}", userInfo.Email, ex.Message).Tag("External Login", client.Name).Property("User Info", userInfo).Property("Auth Info", authInfo).SetActionContext(ActionContext).Write(); return(BadRequest("Account Creation is currently disabled.")); } catch (Exception ex) { _logger.Error().Exception(ex).Critical().Message("External login failed for \"{0}\": {1}", userInfo.Email, ex.Message).Tag("External Login", client.Name).Property("User Info", userInfo).Property("Auth Info", authInfo).SetActionContext(ActionContext).Write(); return(BadRequest("An error occurred while processing user info.")); } if (user == null) { _logger.Error().Critical().Message("External login failed for \"{0}\": Unable to process user info.", userInfo.Email).Tag("External Login", client.Name).Property("User Info", userInfo).Property("Auth Info", authInfo).SetActionContext(ActionContext).Write(); return(BadRequest("Unable to process user info.")); } if (!String.IsNullOrWhiteSpace(authInfo.InviteToken)) { await AddInvitedUserToOrganizationAsync(authInfo.InviteToken, user); } _logger.Info().Message("\"{0}\" logged in.", user.EmailAddress).Tag("External Login", client.Name).Identity(user.EmailAddress).Property("User", user).SetActionContext(ActionContext).Write(); return(Ok(new TokenResult { Token = await GetTokenAsync(user) })); }
private async Task <IHttpActionResult> ProcessOAuthClient <TClient>(ExternalAuthInfo authInfo, string appId, string appSecret, Func <IRequestFactory, IClientConfiguration, TClient> clientGenerator) where TClient : OAuth2Client { if (String.IsNullOrEmpty(authInfo?.Code)) { return(NotFound()); } if (String.IsNullOrEmpty(appId) || String.IsNullOrEmpty(appSecret)) { return(NotFound()); } var client = clientGenerator(new RequestFactory(), new RuntimeClientConfiguration { ClientId = appId, ClientSecret = appSecret, RedirectUri = authInfo.RedirectUri, }); UserInfo userInfo; try { userInfo = client.GetUserInfo(authInfo.Code); } catch (Exception ex) { _logger.Error(ex, "Unable to get user info."); return(BadRequest("Unable to get user info.")); } LoginContext loginContext; try { loginContext = await AddExternalLogin(userInfo, authInfo.InviteToken); } catch (ApplicationException) { return(BadRequest("Account Creation is currently disabled.")); } catch (Exception ex) { _logger.Error(ex, "An error occurred while processing user info."); return(BadRequest("An error occurred while processing user info.")); } if (loginContext?.User == null) { return(BadRequest("Unable to process user info.")); } return(Ok(new TokenResponseModel { Token = await GetToken(loginContext.User, loginContext.OrganizationId) })); }
private IHttpActionResult ExternalLogin(OAuth2Client client, ExternalAuthInfo authInfo) { UserInfo userInfo; try { userInfo = client.GetUserInfo(authInfo.Code); } catch (Exception ex) { Log.Error().Exception(ex).Critical().Message("External login failed: {0}", ex.Message).Tag("External Login", client.Name).Property("Auth Info", authInfo).ContextProperty("HttpActionContext", ActionContext).Write(); return(BadRequest("Unable to get user info.")); } User user; try { user = FromExternalLogin(userInfo); } catch (ApplicationException) { return(BadRequest("Account Creation is currently disabled.")); } catch (Exception ex) { Log.Error().Exception(ex).Critical().Message("External login failed for \"{0}\": {1}", userInfo.Email, ex.Message).Tag("External Login", client.Name).Property("User Info", userInfo).Property("Auth Info", authInfo).ContextProperty("HttpActionContext", ActionContext).Write(); return(BadRequest("An error occurred while processing user info.")); } if (user == null) { Log.Error().Critical().Message("External login failed for \"{0}\": Unable to process user info.", userInfo.Email).Tag("External Login", client.Name).Property("User Info", userInfo).Property("Auth Info", authInfo).ContextProperty("HttpActionContext", ActionContext).Write(); return(BadRequest("Unable to process user info.")); } if (!String.IsNullOrWhiteSpace(authInfo.InviteToken)) { AddInvitedUserToOrganization(authInfo.InviteToken, user); } Log.Info().Message("\"{0}\" logged in.", user.EmailAddress).Tag("External Login", client.Name).Property("User", user).ContextProperty("HttpActionContext", ActionContext).Write(); return(Ok(new TokenResult { Token = GetToken(user) })); }
public Task <IHttpActionResult> Google(ExternalAuthInfo value) { return(ProcessOAuthClient(value, Settings.Current.GoogleAppId, Settings.Current.GoogleAppSecret, (f, c) => new GoogleClient(f, c))); }