Skip to content

Commit 558c559

Browse files
committed
2 parents 75a9bb2 + a7efd2e commit 558c559

File tree

1 file changed

+220
-17
lines changed

1 file changed

+220
-17
lines changed

shared.go

Lines changed: 220 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -19467,8 +19467,20 @@ func HandleOpenId(resp http.ResponseWriter, request *http.Request) {
1946719467

1946819468
redirectUrl := "https://shuffler.io/workflows"
1946919469

19470-
if len(os.Getenv("SSO_REDIRECT_URL")) > 0 {
19471-
redirectUrl = os.Getenv("SSO_REDIRECT_URL")
19470+
if project.Environment != "cloud" {
19471+
redirectUrl = "http://localhost:3001/workflows"
19472+
if len(os.Getenv("SSO_REDIRECT_URL")) > 0 {
19473+
baseUrl := os.Getenv("SSO_REDIRECT_URL")
19474+
// Check if URL contains /api/v1/login_openid and replace with /workflows
19475+
if strings.Contains(baseUrl, "/api/v1/login_openid") {
19476+
redirectUrl = strings.Replace(baseUrl, "/api/v1/login_openid", "/workflows", 1)
19477+
} else if !strings.HasSuffix(baseUrl, "/workflows") {
19478+
// If URL doesn't end with /workflows, append it
19479+
redirectUrl = fmt.Sprintf("%s/workflows", baseUrl)
19480+
} else {
19481+
redirectUrl = baseUrl
19482+
}
19483+
}
1947219484
}
1947319485

1947419486
if len(userName) == 0 {
@@ -19483,7 +19495,40 @@ func HandleOpenId(resp http.ResponseWriter, request *http.Request) {
1948319495
for _, user := range users {
1948419496
log.Printf("%s - %s", user.GeneratedUsername, userName)
1948519497
if user.GeneratedUsername == userName {
19486-
log.Printf("[AUDIT] Found user %s (%s) which matches SSO info for %s. Redirecting to login!", user.Username, user.Id, userName)
19498+
foundOrgInUser := false
19499+
for _, userOrg := range user.Orgs {
19500+
if userOrg == org.Id {
19501+
foundOrgInUser = true
19502+
break
19503+
}
19504+
}
19505+
19506+
// check whether user is in org or not
19507+
foundUserInOrg := false
19508+
var usr User
19509+
for _, usr = range org.Users {
19510+
if usr.Id == user.Id {
19511+
foundUserInOrg = true
19512+
break
19513+
}
19514+
}
19515+
19516+
if (!foundOrgInUser || !foundUserInOrg) && org.SSOConfig.AutoProvision {
19517+
log.Printf("[WARNING] User %s (%s) is not in org %s (%s). Please contact the administrator - (1)", user.Username, user.Id, org.Name, org.Id)
19518+
resp.WriteHeader(401)
19519+
resp.Write([]byte(fmt.Sprintf(`{"success": false, "reason": "User not found in the org. Autoprovisioning is disabled. Please contact the admin of the org to allow auto-provisioning of user."}`)))
19520+
return
19521+
} else if !foundOrgInUser || !foundUserInOrg {
19522+
log.Printf("[INFO] User %s (%s) is not in org %s (%s). Auto-provisioning is enabled. Adding user to org - (1)", user.Username, user.Id, org.Name, org.Id)
19523+
if !foundOrgInUser {
19524+
user.Orgs = append(user.Orgs, org.Id)
19525+
}
19526+
if !foundUserInOrg {
19527+
org.Users = append(org.Users, user)
19528+
}
19529+
} else {
19530+
log.Printf("[AUDIT] Found user %s (%s) which matches SSO info for %s. Redirecting to login! - (1)", user.Username, user.Id, userName)
19531+
}
1948719532

1948819533
//log.Printf("SESSION: %s", user.Session)
1948919534
user.ActiveOrg = OrgMini{
@@ -19570,6 +19615,16 @@ func HandleOpenId(resp http.ResponseWriter, request *http.Request) {
1957019615
return
1957119616
}
1957219617

19618+
if !foundUserInOrg {
19619+
err = SetOrg(ctx, *org, org.Id)
19620+
if err != nil {
19621+
log.Printf("[WARNING] Failed updating org when setting user: %s", err)
19622+
resp.WriteHeader(401)
19623+
resp.Write([]byte(`{"success": false, "reason": "Failed org update during user storage (2)"}`))
19624+
return
19625+
}
19626+
}
19627+
1957319628
//redirectUrl = fmt.Sprintf("%s?source=SSO&id=%s", redirectUrl, session)
1957419629
http.Redirect(resp, request, redirectUrl, http.StatusSeeOther)
1957519630
return
@@ -19582,8 +19637,41 @@ func HandleOpenId(resp http.ResponseWriter, request *http.Request) {
1958219637
if err == nil && len(users) > 0 {
1958319638
for _, user := range users {
1958419639
if user.Username == userName {
19585-
log.Printf("[AUDIT] Found user %s (%s) which matches SSO info for %s. Redirecting to login %s!", user.Username, user.Id, userName, redirectUrl)
19640+
// Checking whether the user is in the org
19641+
foundOrgInUser := false
19642+
for _, userOrg := range user.Orgs {
19643+
if userOrg == org.Id {
19644+
foundOrgInUser = true
19645+
break
19646+
}
19647+
}
1958619648

19649+
// check whether user is in org or not
19650+
foundUserInOrg := false
19651+
var usr User
19652+
for _, usr = range org.Users {
19653+
if usr.Id == user.Id {
19654+
foundUserInOrg = true
19655+
break
19656+
}
19657+
}
19658+
19659+
if (!foundOrgInUser || !foundUserInOrg) && org.SSOConfig.AutoProvision {
19660+
log.Printf("[WARNING] User %s (%s) is not in org %s (%s). Please contact the administrator - (2)", user.Username, user.Id, org.Name, org.Id)
19661+
resp.WriteHeader(401)
19662+
resp.Write([]byte(fmt.Sprintf(`{"success": false, "reason": "User not found in the org. Autoprovisioning is disabled. Please contact the admin of the org to allow auto-provisioning of user."}`)))
19663+
return
19664+
} else if !foundOrgInUser || !foundUserInOrg {
19665+
log.Printf("[INFO] User %s (%s) is not in org %s (%s). Auto-provisioning is enabled. Adding user to org - (2)", user.Username, user.Id, org.Name, org.Id)
19666+
if !foundOrgInUser {
19667+
user.Orgs = append(user.Orgs, org.Id)
19668+
}
19669+
if !foundUserInOrg {
19670+
org.Users = append(org.Users, user)
19671+
}
19672+
} else {
19673+
log.Printf("[AUDIT] Found user %s (%s) which matches SSO info for %s. Redirecting to login!- (2)", user.Username, user.Id, userName)
19674+
}
1958719675
//log.Printf("SESSION: %s", user.Session)
1958819676
user.ActiveOrg = OrgMini{
1958919677
Name: org.Name,
@@ -19668,6 +19756,16 @@ func HandleOpenId(resp http.ResponseWriter, request *http.Request) {
1966819756
return
1966919757
}
1967019758

19759+
if !foundUserInOrg {
19760+
err = SetOrg(ctx, *org, org.Id)
19761+
if err != nil {
19762+
log.Printf("[WARNING] Failed updating org when setting session: %s", err)
19763+
resp.WriteHeader(401)
19764+
resp.Write([]byte(`{"success": false, "reason": "Failed org update during session storage (2)"}`))
19765+
return
19766+
}
19767+
}
19768+
1967119769
//redirectUrl = fmt.Sprintf("%s?source=SSO&id=%s", redirectUrl, session)
1967219770
http.Redirect(resp, request, redirectUrl, http.StatusSeeOther)
1967319771
return
@@ -19700,6 +19798,13 @@ func HandleOpenId(resp http.ResponseWriter, request *http.Request) {
1970019798
return
1970119799
}
1970219800

19801+
if org.SSOConfig.AutoProvision {
19802+
log.Printf("[INFO] Auto-provisioning user is not allow for org %s (%s) - can not add new user %s - (3)", org.Name, org.Id, userName)
19803+
resp.WriteHeader(401)
19804+
resp.Write([]byte(fmt.Sprintf(`{"success": false, "reason": "User not found in the org. Autoprovisioning is disabled. Please contact the admin of the org to allow auto-provisioning of user."}`)))
19805+
return
19806+
}
19807+
1970319808
log.Printf("[AUDIT] Adding user %s to org %s (%s) through single sign-on", userName, org.Name, org.Id)
1970419809
newUser := new(User)
1970519810
// Random password to ensure its not empty
@@ -19789,17 +19894,20 @@ func HandleSSO(resp http.ResponseWriter, request *http.Request) {
1978919894
redirectUrl := "http://localhost:3001/workflows"
1979019895
backendUrl := os.Getenv("SSO_REDIRECT_URL")
1979119896

19792-
if len(backendUrl) == 0 && project.Environment == "onprem" {
19793-
backendUrl = "http://localhost:3000"
19794-
}
19795-
19796-
if len(backendUrl) == 0 && len(os.Getenv("BASE_URL")) > 0 {
19797-
backendUrl = os.Getenv("BASE_URL")
19798-
}
19799-
19800-
if len(backendUrl) > 0 {
19801-
//we don't need to add /workflow path in backend url as backend url is SSO_REDIRECT_URL and it is already pointing to /workflow by default.
19802-
redirectUrl = backendUrl
19897+
if project.Environment != "cloud" {
19898+
if len(os.Getenv("SSO_REDIRECT_URL")) > 0 {
19899+
baseUrl := os.Getenv("SSO_REDIRECT_URL")
19900+
19901+
// Check if URL contains /api/v1/login_sso and replace with /workflows
19902+
if strings.Contains(baseUrl, "/api/v1/login_sso") {
19903+
redirectUrl = strings.Replace(baseUrl, "/api/v1/login_sso", "/workflows", 1)
19904+
} else if !strings.HasSuffix(baseUrl, "/workflows") {
19905+
// If URL doesn't end with /workflows, append it
19906+
redirectUrl = fmt.Sprintf("%s/workflows", baseUrl)
19907+
} else {
19908+
redirectUrl = baseUrl
19909+
}
19910+
}
1980319911
}
1980419912

1980519913
if project.Environment == "cloud" {
@@ -19992,7 +20100,40 @@ func HandleSSO(resp http.ResponseWriter, request *http.Request) {
1999220100
for _, user := range users {
1999320101
log.Printf("%s - %s", user.GeneratedUsername, userName)
1999420102
if user.GeneratedUsername == userName {
19995-
log.Printf("[AUDIT] Found user %s (%s) which matches SSO info for %s. Redirecting to login!", user.Username, user.Id, userName)
20103+
foundOrgInUser := false
20104+
for _, userOrg := range user.Orgs {
20105+
if userOrg == foundOrg.Id {
20106+
foundOrgInUser = true
20107+
break
20108+
}
20109+
}
20110+
20111+
// check whether user is in org or not
20112+
foundUserInOrg := false
20113+
var usr User
20114+
for _, usr = range foundOrg.Users {
20115+
if usr.Id == user.Id {
20116+
foundUserInOrg = true
20117+
break
20118+
}
20119+
}
20120+
20121+
if (!foundOrgInUser || !foundUserInOrg) && foundOrg.SSOConfig.AutoProvision {
20122+
log.Printf("[WARNING] User %s (%s) is not in org %s (%s). Autoprovisioning of user is disable. Please contact the administrator - (1)", user.Username, user.Id, foundOrg.Name, foundOrg.Id)
20123+
resp.WriteHeader(401)
20124+
resp.Write([]byte(fmt.Sprintf(`{"success": false, "reason": "User not found in the org. Autoprovisioning is disabled. Please contact the admin of the org to allow auto-provisioning of user."}`)))
20125+
return
20126+
} else if !foundOrgInUser || !foundUserInOrg {
20127+
log.Printf("[INFO] User %s (%s) is not in org %s (%s). Auto-provisioning is enabled. Adding user to org - (1)", user.Username, user.Id, foundOrg.Name, foundOrg.Id)
20128+
if !foundOrgInUser {
20129+
user.Orgs = append(user.Orgs, foundOrg.Id)
20130+
}
20131+
if !foundUserInOrg {
20132+
foundOrg.Users = append(foundOrg.Users, user)
20133+
}
20134+
} else {
20135+
log.Printf("[AUDIT] Found user %s (%s) which matches SSO info for %s. Redirecting to login! - (1)", user.Username, user.Id, userName)
20136+
}
1999620137

1999720138
if project.Environment == "cloud" {
1999820139
// user.ActiveOrg.Id = matchingOrgs[0].Id
@@ -20093,6 +20234,16 @@ func HandleSSO(resp http.ResponseWriter, request *http.Request) {
2009320234
return
2009420235
}
2009520236

20237+
if !foundUserInOrg {
20238+
err = SetOrg(ctx, foundOrg, foundOrg.Id)
20239+
if err != nil {
20240+
log.Printf("[WARNING] Failed updating org when setting user: %s", err)
20241+
resp.WriteHeader(401)
20242+
resp.Write([]byte(`{"success": false, "reason": "Failed org update during user storage (2)"}`))
20243+
return
20244+
}
20245+
}
20246+
2009620247
//redirectUrl = fmt.Sprintf("%s?source=SSO&id=%s", redirectUrl, session)
2009720248
http.Redirect(resp, request, redirectUrl, http.StatusSeeOther)
2009820249
return
@@ -20105,7 +20256,42 @@ func HandleSSO(resp http.ResponseWriter, request *http.Request) {
2010520256
if err == nil && len(users) > 0 {
2010620257
for _, user := range users {
2010720258
if user.Username == userName {
20108-
log.Printf("[AUDIT] Found user %s (%s) which matches SSO info for %s. Redirecting to login %s!", user.Username, user.Id, userName, redirectUrl)
20259+
20260+
// Checking whether the user is in the org
20261+
foundOrgInUser := false
20262+
for _, userOrg := range user.Orgs {
20263+
if userOrg == foundOrg.Id {
20264+
foundOrgInUser = true
20265+
break
20266+
}
20267+
}
20268+
20269+
// check whether user is in org or not
20270+
foundUserInOrg := false
20271+
var usr User
20272+
for _, usr = range foundOrg.Users {
20273+
if usr.Id == user.Id {
20274+
foundUserInOrg = true
20275+
break
20276+
}
20277+
}
20278+
20279+
if (!foundOrgInUser || !foundUserInOrg) && foundOrg.SSOConfig.AutoProvision {
20280+
log.Printf("[WARNING] User %s (%s) is not in org %s (%s). Autoprovisioning user is not allow in org - (2)", user.Username, user.Id, foundOrg.Name, foundOrg.Id)
20281+
resp.WriteHeader(401)
20282+
resp.Write([]byte(fmt.Sprintf(`{"success": false, "reason": "User not found in the org. Autoprovisioning is disabled. Please contact the admin of the org to allow auto-provisioning of user."}`)))
20283+
return
20284+
} else if !foundOrgInUser || !foundUserInOrg {
20285+
log.Printf("[INFO] User %s (%s) is not in org %s (%s). Auto-provisioning is enabled. Adding user to org - (2)", user.Username, user.Id, foundOrg.Name, foundOrg.Id)
20286+
if !foundOrgInUser {
20287+
user.Orgs = append(user.Orgs, foundOrg.Id)
20288+
}
20289+
if !foundUserInOrg {
20290+
foundOrg.Users = append(foundOrg.Users, user)
20291+
}
20292+
} else {
20293+
log.Printf("[AUDIT] Found user %s (%s) which matches SSO info for %s. Redirecting to login! - (2)", user.Username, user.Id, userName)
20294+
}
2010920295

2011020296
//log.Printf("SESSION: %s", user.Session)
2011120297
// if project.Environment == "cloud" {
@@ -20197,6 +20383,16 @@ func HandleSSO(resp http.ResponseWriter, request *http.Request) {
2019720383
return
2019820384
}
2019920385

20386+
if !foundUserInOrg {
20387+
err = SetOrg(ctx, foundOrg, foundOrg.Id)
20388+
if err != nil {
20389+
log.Printf("[WARNING] Failed updating org when setting session: %s", err)
20390+
resp.WriteHeader(401)
20391+
resp.Write([]byte(`{"success": false, "reason": "Failed org update during session storage (2)"}`))
20392+
return
20393+
}
20394+
}
20395+
2020020396
//redirectUrl = fmt.Sprintf("%s?source=SSO&id=%s", redirectUrl, session)
2020120397
http.Redirect(resp, request, redirectUrl, http.StatusSeeOther)
2020220398
return
@@ -20229,6 +20425,13 @@ func HandleSSO(resp http.ResponseWriter, request *http.Request) {
2022920425
return
2023020426
}
2023120427

20428+
if foundOrg.SSOConfig.AutoProvision {
20429+
log.Printf("[INFO] Auto-provisioning user is not allow for org %s (%s) - can not add new user %s", foundOrg.Name, foundOrg.Id, userName)
20430+
resp.WriteHeader(401)
20431+
resp.Write([]byte(fmt.Sprintf(`{"success": false, "reason": "User not found in the org. Autoprovisioning is disabled. Please contact the admin of the org to allow auto-provisioning of user."}`)))
20432+
return
20433+
}
20434+
2023220435
log.Printf("[AUDIT] Adding user %s to org %s (%s) through single sign-on", userName, foundOrg.Name, foundOrg.Id)
2023320436
newUser := new(User)
2023420437
// Random password to ensure its not empty

0 commit comments

Comments
 (0)