From 12acada4a131c5306b16b142b1c3074d3b7da2f8 Mon Sep 17 00:00:00 2001 From: abhishek9686 Date: Tue, 2 Jul 2024 16:14:22 +0530 Subject: [PATCH] add invited user via oauth signup automatically --- pro/auth/azure-ad.go | 40 ++++++++++++++++++++++++++++++++-------- pro/auth/github.go | 39 +++++++++++++++++++++++++++++++-------- pro/auth/google.go | 40 ++++++++++++++++++++++++++++++++-------- pro/auth/oidc.go | 39 +++++++++++++++++++++++++++++++-------- 4 files changed, 126 insertions(+), 32 deletions(-) diff --git a/pro/auth/azure-ad.go b/pro/auth/azure-ad.go index 62ecedc2..c53b98e5 100644 --- a/pro/auth/azure-ad.go +++ b/pro/auth/azure-ad.go @@ -71,23 +71,47 @@ func handleAzureCallback(w http.ResponseWriter, r *http.Request) { handleOauthUserNotAllowedToSignUp(w) return } + var inviteExists bool + // check if invite exists for User + _, err = logic.GetUserInvite(content.UserPrincipalName) + if err == nil { + inviteExists = true + } // check if user approval is already pending - if logic.IsPendingUser(content.UserPrincipalName) { + if !inviteExists && logic.IsPendingUser(content.UserPrincipalName) { handleOauthUserSignUpApprovalPending(w) return } + _, err = logic.GetUser(content.UserPrincipalName) if err != nil { if database.IsEmptyRecord(err) { // user must not exist, so try to make one - err = logic.InsertPendingUser(&models.User{ - UserName: content.UserPrincipalName, - }) - if err != nil { - handleSomethingWentWrong(w) + if inviteExists { + // create user + var newPass, fetchErr = auth.FetchPassValue("") + if fetchErr != nil { + logic.ReturnErrorResponse(w, r, logic.FormatError(fetchErr, "internal")) + return + } + if err = logic.CreateUser(&models.User{ + UserName: content.UserPrincipalName, + Password: newPass, + }); err != nil { + handleSomethingWentWrong(w) + return + } + logic.DeletePendingUser(content.UserPrincipalName) + } else { + err = logic.InsertPendingUser(&models.User{ + UserName: content.UserPrincipalName, + }) + if err != nil { + handleSomethingWentWrong(w) + return + } + handleFirstTimeOauthUserSignUp(w) return } - handleFirstTimeOauthUserSignUp(w) - return } else { handleSomethingWentWrong(w) return diff --git a/pro/auth/github.go b/pro/auth/github.go index 67223de4..a8434b1e 100644 --- a/pro/auth/github.go +++ b/pro/auth/github.go @@ -71,23 +71,46 @@ func handleGithubCallback(w http.ResponseWriter, r *http.Request) { handleOauthUserNotAllowedToSignUp(w) return } + var inviteExists bool + // check if invite exists for User + _, err = logic.GetUserInvite(content.Login) + if err == nil { + inviteExists = true + } // check if user approval is already pending - if logic.IsPendingUser(content.Login) { + if !inviteExists && logic.IsPendingUser(content.Login) { handleOauthUserSignUpApprovalPending(w) return } _, err = logic.GetUser(content.Login) if err != nil { if database.IsEmptyRecord(err) { // user must not exist, so try to make one - err = logic.InsertPendingUser(&models.User{ - UserName: content.Login, - }) - if err != nil { - handleSomethingWentWrong(w) + if inviteExists { + // create user + var newPass, fetchErr = auth.FetchPassValue("") + if fetchErr != nil { + logic.ReturnErrorResponse(w, r, logic.FormatError(fetchErr, "internal")) + return + } + if err = logic.CreateUser(&models.User{ + UserName: content.Login, + Password: newPass, + }); err != nil { + handleSomethingWentWrong(w) + return + } + logic.DeletePendingUser(content.Login) + } else { + err = logic.InsertPendingUser(&models.User{ + UserName: content.Login, + }) + if err != nil { + handleSomethingWentWrong(w) + return + } + handleFirstTimeOauthUserSignUp(w) return } - handleFirstTimeOauthUserSignUp(w) - return } else { handleSomethingWentWrong(w) return diff --git a/pro/auth/google.go b/pro/auth/google.go index 64d503e7..3d5cfe4b 100644 --- a/pro/auth/google.go +++ b/pro/auth/google.go @@ -73,23 +73,47 @@ func handleGoogleCallback(w http.ResponseWriter, r *http.Request) { handleOauthUserNotAllowedToSignUp(w) return } + var inviteExists bool + // check if invite exists for User + _, err = logic.GetUserInvite(content.Email) + if err == nil { + inviteExists = true + } // check if user approval is already pending - if logic.IsPendingUser(content.Email) { + if !inviteExists && logic.IsPendingUser(content.Email) { handleOauthUserSignUpApprovalPending(w) return } _, err = logic.GetUser(content.Email) if err != nil { if database.IsEmptyRecord(err) { // user must not exist, so try to make one - err = logic.InsertPendingUser(&models.User{ - UserName: content.Email, - }) - if err != nil { - handleSomethingWentWrong(w) + if inviteExists { + // create user + var newPass, fetchErr = auth.FetchPassValue("") + if fetchErr != nil { + logic.ReturnErrorResponse(w, r, logic.FormatError(fetchErr, "internal")) + return + } + if err = logic.CreateUser(&models.User{ + UserName: content.Email, + Password: newPass, + }); err != nil { + handleSomethingWentWrong(w) + return + } + logic.DeletePendingUser(content.Email) + } else { + err = logic.InsertPendingUser(&models.User{ + UserName: content.Email, + }) + if err != nil { + handleSomethingWentWrong(w) + return + } + handleFirstTimeOauthUserSignUp(w) return } - handleFirstTimeOauthUserSignUp(w) - return + } else { handleSomethingWentWrong(w) return diff --git a/pro/auth/oidc.go b/pro/auth/oidc.go index a2bf5739..6c0dd498 100644 --- a/pro/auth/oidc.go +++ b/pro/auth/oidc.go @@ -84,23 +84,46 @@ func handleOIDCCallback(w http.ResponseWriter, r *http.Request) { handleOauthUserNotAllowedToSignUp(w) return } + var inviteExists bool + // check if invite exists for User + _, err = logic.GetUserInvite(content.Login) + if err == nil { + inviteExists = true + } // check if user approval is already pending - if logic.IsPendingUser(content.Email) { + if !inviteExists && logic.IsPendingUser(content.Email) { handleOauthUserSignUpApprovalPending(w) return } _, err = logic.GetUser(content.Email) if err != nil { if database.IsEmptyRecord(err) { // user must not exist, so try to make one - err = logic.InsertPendingUser(&models.User{ - UserName: content.Email, - }) - if err != nil { - handleSomethingWentWrong(w) + if inviteExists { + // create user + var newPass, fetchErr = auth.FetchPassValue("") + if fetchErr != nil { + logic.ReturnErrorResponse(w, r, logic.FormatError(fetchErr, "internal")) + return + } + if err = logic.CreateUser(&models.User{ + UserName: content.Email, + Password: newPass, + }); err != nil { + handleSomethingWentWrong(w) + return + } + logic.DeletePendingUser(content.Email) + } else { + err = logic.InsertPendingUser(&models.User{ + UserName: content.Email, + }) + if err != nil { + handleSomethingWentWrong(w) + return + } + handleFirstTimeOauthUserSignUp(w) return } - handleFirstTimeOauthUserSignUp(w) - return } else { handleSomethingWentWrong(w) return